Help with a simple question
Hey, i have a problem with my school assignment. I can't get the "nummer" to work when i try to run the program. How do i get the "nummer" to work outside the for statement? I know the for statement is not done yet and may be wrong. But i don't get the "nummer" to work.
Sorry for my english if you don't understand.
Thank you for your time.
package uppgift;
import javax.swing.*;
public class Uppgift {
public static void main(String[] args) {
String a = JOptionPane.showInputDialog("Hur många gånger vill du kasta?");
int kast = Integer.parseInt(a);
int kast1;
for (kast1 = 1; kast1 == kast; kast1 = kast1 + 1) {
int nummer = (int) (12.0 * Math.random()) + 1;
}
JOptionPane.showMessageDialog(null, nummer);
}
}
Re: Help with a simple question
Quote:
I can't get the "nummer" to work
Do you get an error message? What does "to work" mean?
Your problem is with the scope of the variable nummer. It is only known within the enclosing {} where it is defined. If you want "to see" the variable outside of the {} where it is defined, move the definition of the variable outside of the {}.
Read up on the "scope" concept.
Re: Help with a simple question
Quote:
Originally Posted by
Norm
Do you get an error message? What does "to work" mean?
Your problem is with the scope of the variable nummer. It is only known within the enclosing {} where it is defined. If you want "to see" the variable outside of the {} where it is defined, move the definition of the variable outside of the {}.
Read up on the "scope" concept.
JOptionPane.showMessageDialog(null, nummer); it don't find int nummer in the for statement, thats what i need to know how i could make it work. Hope you understand better.
Re: Help with a simple question
Please read my post. I explained what the problem was and how to solve it.
Do you know what "scope" means? If not, you should research it.
Google: variable scope