0

I have a problem with my guessing number simulator, almost everything except for one last statement is done and I need to get a answer that says i should guess somewhere between 1,100. And i do, but I also get the answer that my guess is to high and I´m not suppose to have that for school... can someone please help me? here is my code.

 //introduktion  
 Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så 
 varsågod..\nskriv in ett tal");
 var str = Console.ReadLine();
 int guess = Convert.ToInt32(str);
 Random rd = new Random();
 int rand_num = rd.Next(1, 100);

 //when your guess is right
 if (guess == rand_num)
     {
         Console.WriteLine("Ditt Tal är rätt. grattis!");
     }

 //when your guess is to small
     else if (guess < rand_num)
     {
         Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
     }

 //when your guess is to high
     if (guess > rand_num)
     {
         Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal");
     }

     //and when you guess is still wrong but close
     if (Math.Abs(guess - rand_num) <= 3)
     {
         Console.WriteLine("Du är dock nära och det bränns");
     }

     // when your guess is over 100
     if (guess > 100)
     {
         Console.WriteLine("Du måste skriva in ett tal mellan 1 och 100!");
     }

 // ending line
 Console.WriteLine("Programmet är slut");
  • 2
    Alwasy first try the debugger before coming here!! – TaW Jul 02 '22 at 17:31
  • So you ask the user for a guess, then generate a random number and compare the two. And likely every time it's a different random number. So every time, the user has exactly one chance to get it right. Why tell them they were close? The first three "ifs" already cover every possible input. So one of them will always trigger. The other two are just specializations, that may or may not trigger *additionally*. Is that the logic you want? – Corak Jul 02 '22 at 17:39
  • its my teacher that want this specific answers from the program and they want a answer for when they guess over 100, but only one answer (guess somewhere between 1,100) but i also get the answer (it´s to high) something they don´t... i just started to study programming and i have been stuck on this for over one week and don´t know what to do, i have changed the "if,else if and else statements, I have changed the order on the commands, and i don´t know what wrong im doing, and i don´t understand the debug mod the fully yet so i can´t take advantage of to the fullest. – Kent Karlsson Jul 02 '22 at 17:48
  • @Corak I would indeed put correct range check first (https://stackoverflow.com/questions/3188672/how-to-elegantly-check-if-a-number-is-within-a-range - `1<=guess && guess <=100`) with text like "input must be 1-100, you **** ***", but otherwise I think "too low" and "too low. getting close" both make sense for number guessing. Hopefully OP will provide complete assignment text so we can see what is missing. – Alexei Levenkov Jul 02 '22 at 17:48
  • @KentKarlsson - So what if the guess is too high or too low, but only by a little. Do you want two answers ("too high/low" + "you're close"), or only one? -- with the debug mode, you can set a "breakpoint", for example on the line `int rand_num = rd.Next(1, 100);`. Then, when you run the thing, it will stop there, and then you can follow the flow of the program step by step (either F10 of F11). – Corak Jul 02 '22 at 17:57
  • @AlexeiLevenkov - "Hopefully OP will provide complete assignment text so we can see what is missing." - that *would* be helpful. Assuming the text is translated to english, of course. – Corak Jul 02 '22 at 17:59

1 Answers1

1

Try this:

//introduktion  
Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så 
  varsågod..\nskriv in ett tal");
var str = Console.ReadLine();
int guess = Convert.ToInt32(str);
Random rd = new Random();
int rand_num = rd.Next(1, 100);

//when your guess is right
if (guess == rand_num)
{
    Console.WriteLine("Ditt Tal är rätt. grattis!");
}
else 
{
    // when your guess is over 100
    if (guess > 100)
    {
        Console.WriteLine("Du måste skriva in ett tal mellan 1 och 100!");
    }
    else
    {
        //when your guess is to small
        if (guess < rand_num)
        {
            Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
        }

        //when your guess is to high
        else // if (guess > rand_num) - redundant check: if it's not equal or below
             //                         it must be greater
        {
            Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal");
        }
    
        //and when you guess is still wrong but close
        if (Math.Abs(guess - rand_num) <= 3)
        {
            Console.WriteLine("Du är dock nära och det bränns");
        }
    }
}
// ending line
Console.WriteLine("Programmet är slut");