Forum

> > Off Topic > Java beginner, code assist please
Forums overviewOff Topic overviewLog in to reply

English Java beginner, code assist please

5 replies
To the start Previous 1 Next To the start

old Java beginner, code assist please

Data458
User Off Offline

Quote
So I've just started learning Java and wanting to creating a basic 'Guess number' game. I'm trying yet i'm stuck. I've sorta learnt everything at once and now need a small break #brainoverload. This is also my first thread, so .. please tell me if i'm doing something wrong hehe. My code as is:

import java.util.Scanner;
import java.util.Random;

class Main1{
     public static void main (String args[]){
          Scanner sc = new Scanner (System.in);
          Random rand = new Random();
          int anwser, numbertoguess=rand.nextInt(1000);
          
          System.out.print("What number am I thinking of? ");
          anwser=sc.nextInt();
          
          while (anwser != numbertoguess){
               if (anwser > numbertoguess){
                    System.out.print("Lower. ");
                    anwser=sc.nextInt();
               }else if (anwser < numbertoguess){
                    System.out.print("Higher. ");
                    anwser=sc.nextInt();
                    
               }else System.out.println("You are correct!! :)");
                         break;               
          }
     }
}

old Re: Java beginner, code assist please

Hador
User Off Offline

Quote
user Data458 has written
               }else System.out.println("You are correct!! :)");
                         break;               


That should probably be

1
2
3
4
} else {
	System.ouit.println("You are correct!! :) ");
	break;
}

The way you put it the "break;" is not inside the scope of the else, so that the while-loop breaks no matter what the if-statements before did

old Re: Java beginner, code assist please

ohaz
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
import java.util.Random;

class Main1{
     public static void main (String args[]){
          Scanner sc = new Scanner (System.in);
          Random rand = new Random();
          int anwser, numbertoguess=rand.nextInt(1000);
          
          System.out.print("What number am I thinking of? ");
          anwser=sc.nextInt();
          
          while (anwser != numbertoguess){
               if (anwser > numbertoguess){
                    System.out.print("Lower. ");
                    anwser=sc.nextInt();
               }else if (anwser < numbertoguess){
                    System.out.print("Higher. ");
                    anwser=sc.nextInt();
               }           
          }
	     System.out.println("You are correct!! :)");
     }
}
The code did never print the "You are correct" because the while loop already ended as soon as you enter the correct number. Just put the print behind the loop, everythings perfect

old Re: Java beginner, code assist please

Data458
User Off Offline

Quote
Tyvm for the replies hehe, i'm so happy to make my first game / decent program. Just at the bottom there Ohaz
just had to add "if (a==b)" because I was being spammed "You are correct" Nevertheless thanks heaps for your input.

Hador, you are correct. My brain was overloading I couldn't really think straight haha

I'm now trying to add option to see how many attempts we're done. Where abouts would I add (new var) tries=tries++, or is that wrong?
edited 1×, last 15.06.14 11:31:01 am

old Re: Java beginner, code assist please

ohaz
User Off Offline

Quote
If you put the print outside of the while {}, it should only post that once.

To print out the attempts, just create a new int before the while and increment it at the end of each while loop. similar to this:
1
2
3
4
5
6
int tries = 0;
while () {
	[yourstuff]
	tries++;
}
System.out.println("You've won! It took you "+tries+" tries");

old Re: Java beginner, code assist please

Data458
User Off Offline

Quote
You're right hehe thanks again. How would I add an explanation to use whole numbers, in case integers or strings are used "accidentally".
edited 1×, last 15.06.14 12:01:17 pm
To the start Previous 1 Next To the start
Log in to replyOff Topic overviewForums overview