Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with a simple question

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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);


    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with a simple question

    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.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a simple question

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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
    Last edited by Norm; May 28th, 2011 at 07:49 AM.

Similar Threads

  1. Simple GUI/Animation question...
    By teslatrain in forum AWT / Java Swing
    Replies: 3
    Last Post: April 29th, 2011, 04:10 PM
  2. Just a simple question
    By newbie in forum Java Theory & Questions
    Replies: 10
    Last Post: December 6th, 2010, 08:19 PM
  3. SUPER SIMPLE QUESTION!!!
    By Options in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 2nd, 2010, 09:21 PM
  4. simple question...
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2010, 07:29 AM
  5. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 03:47 AM