Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:37:19+00:00 2026-06-05T18:37:19+00:00

There is something wrong with my code. I am teaching myself c# and one

  • 0

There is something wrong with my code. I am teaching myself c# and one of the challenges in this chapter was to prompt the user for 10 numbers, store them in an array, than ask for 1 additional number. Then the program would say whether the additional number matched one of the numbers in the array. Now what I have below does work, but only if I enter in a comparison number that is less than 10 which is the size of the array.

I’m not sure how to fix it. I am not sure how to do the comparison. I tried a FOR loop first which kind of worked, but ran through the loop and displayed the comparison against all 10 numbers so you would get 9 lines of No! and 1 line of Yes!. I put in a break; which stopped it counting all 10 but if I entered the number 5 for comparison, then I would get 4 lines of No! and 1 of Yes!. The below has been the only way I could get it to work reliably but only as long as the number isn’t out of the bounds of the array.

I can see why I get the error when the number is above 10, I just don’t know what to use to compare it but still allow the user to enter in any valid integer. Any assistance would be great!

        int[] myNum = new int[10];
        Console.WriteLine("Starting program ...");
        Console.WriteLine("Please enter 10 numbers.");

        for (int i = 0; i <= 9; ++i)
        {
            Console.Write("Number {0}: ", i + 1);
            myNum[i] = Int32.Parse(Console.ReadLine());
        }

        Console.WriteLine("Thank you.  You entered the numbers ");
        foreach (int i in myNum)
        {
            Console.Write("{0} ", i);
        }

        Console.WriteLine("");
        Console.Write("Please enter 1 additional number: ");
        int myChoice = Int32.Parse(Console.ReadLine());
        Console.WriteLine("Thank you.  You entered the number {0}.", myChoice);

        int compareArray = myNum[myChoice - 1];

        if (compareArray == myChoice)
        {
            Console.WriteLine("Yes!  The number {0} is equal to one of the numbers you previously entered.", myChoice);
        }
        else
        {
            Console.WriteLine("No!  The number {0} is not equal to any of the entered numbers.", myChoice);
        }

        Console.WriteLine("End program ...");

        Console.ReadLine();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:37:21+00:00Added an answer on June 5, 2026 at 6:37 pm

    If you include the System.Linq namespace (or if you change the type of myNum to be something that implements ICollection<T>, like List<T>), you can use myNum.Contains(myChoice) to see if the value myChoice matches one of the values in myNum. array.Contains returns a boolean that is true if the specified value is found in the array and false if it is not.

    You can update your code to use this like so:

       //int compareArray = myNum[myChoice - 1]; // This line is no longer needed
    
        if (myNum.Contains(myChoice))
        {
            Console.WriteLine("Yes!  The number {0} is equal to one of the numbers you previously entered.", myChoice);
        }
        else
        {
            Console.WriteLine("No!  The number {0} is not equal to any of the entered numbers.", myChoice);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something wrong with this code because when i press the button to
Is there something wrong with this code? I'm trying to set up some buttons
Is there something wrong with this code? Sometimes I get an unhandled Invalid transaction
My program/process exited abnormally? I doubt something is wrong with the following code: There
There is something wrong with this trigger. But what? CREATE TRIGGER MYCOOLTRIGGER AFTER INSERT
is there something wrong with the code? Some reason it won't be played? import
Is there something wrong with the code below it just won't work, no errors?
is there something wrong in the code? its not actually updating all textinput but
Can anyone see if there is something wrong in my matlab code? My objective
I may be a bit burned out, but is there something wrong with where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.