Jump to content

Please help a java noob

Go to solution Solved by has246,

hey

try this:

 

public void chooseloacation() {

sc = new Scanner(System.in);

while (true) {

x = sc.nextInt();

if (x > 0 && x < 7) 

    break;

System.out.println("please select a number only from 1 to 6");

}

if (x == 5) {

System.out.println("poutses");

}

}

hey

try this:

 

public void chooseloacation() {

sc = new Scanner(System.in);

while (true) {

x = sc.nextInt();

if (x > 0 && x < 7) 

    break;

System.out.println("please select a number only from 1 to 6");

}

if (x == 5) {

System.out.println("poutses");

}

}

Link to comment
https://linustechtips.com/topic/728377-please-help-a-java-noob/#findComment-9260879
Share on other sites

Link to post
Share on other sites

Firstly, it is helpful to comment out your code more for the end of each bracket. Secondly, try this.x instead of just x. Basically what the former does is say "The variable most in scope". This SHOULD fix the issue, but let me know if I overlooked anything.

Link to comment
https://linustechtips.com/topic/728377-please-help-a-java-noob/#findComment-9260880
Share on other sites

Link to post
Share on other sites

to explain. 

if you declare a variable inside a loop, its scope will be limited to this loop. Also, if you declared it for the class. just use it inside any method, no need for redeclaring it.Otherwise it will be counted as 2 separate variables. 

define it before the loop then you can use it inside the loop and after it in the same method. I advise you to read about variable scope ;)

Link to comment
https://linustechtips.com/topic/728377-please-help-a-java-noob/#findComment-9260885
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×