Jump to content

Please help a java noob

Manos
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");

}

}

Here is the class i am working with.. the problem is that the last if statement never works. it seems as the x never registers the value of 5 i type in.

Untitled.png

Link to comment
Share on other sites

Link to post
Share on other sites

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
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
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
Share on other sites

Link to post
Share on other sites

why did you declare x twice and you should put public or private before the one in the beginning 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Tb428 said:

why did you declare x twice and you should put public or private before the one in the beginning 

Did I miss where he declared x twice?

Link to comment
Share on other sites

Link to post
Share on other sites

thnx guys for the input yup i need to comment the code more and be carefull more for the declaration of values

thnx again

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Undertaker225 said:

Did I miss where he declared x twice?

he declared it in the loop and in the beginning of the class

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Tb428 said:

he declared it in the loop and in the beginning of the class

He assigned it from the output from Sc of type Scanner.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Tb428 said:

he declared it in the loop and in the beginning of the class

Sorry, I get what you are saying. My bad.

Link to comment
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

×