1

So I am in an intro class to Java. I'm trying to write my first program. It's a simple program that asks for a number(X) which equals the number of rounds to be completed. Next you run a while loop that adds the total score until X number of rounds have been completed. The textbook doesnt really answer my question. I'm declaring 3 integers. Do I declare these in the main method, or inside the while loop. We have not started using a Java editor yet, so i'm not sure how to check this. He wants us to write out the code, but i'm not sure where to declare these integers, and how its supposed to be properly written. Any help would be great, thanks!

public class Question2
{
    public static void main (String [] args )
    {
        Keyboard kbd;
        Kbd = new keyboard();
        int n, tot, x;
        System.out.println( “How many?”);
        x = kbd.readInt();
    }
    while ( n < x )
    {
        System.out.println( “Enter a score”);
        s = kbd.readInt();
        tot = (tot + s);
        n = (n + 1);
    }
}
5
  • 2
    First, your while loop needs to exist inside an actual method, not just in a random place in the class declaration. Commented Jul 15, 2014 at 16:53
  • You need to understand "variable scope". (Look it up.) Java variables can be static scope, instance scope, method scope, or block scope (though the difference between method and block is not great). In general you should use the "closest" definition that leaves your variable "visible" to all parts of the code that might need to reference it. Commented Jul 15, 2014 at 16:58
  • By the way, you can use any old text editor (eg, Notepad) to create a .java file, then compile it with the java command, to see whether it compiles without error or not. You don't need a fancy IDE. Commented Jul 15, 2014 at 16:59
  • Move while inside the method, declare what is 's'. You can declare this as method level or class level variables. Later when you study about object oriented part, you will know when to make a variable class level and when at method level. You generally make a variable class-level when you are going to use it throughout the class and would want to save the state of it. Commented Jul 15, 2014 at 17:00
  • What is this Keyboard class? Pretty sure it doesn't exist in the standard library. Are you sure you don't want to use Scanner ? Commented Jul 15, 2014 at 17:05

5 Answers 5

1

You can do this in a lot of ways in a simple program you can declare your int s which is the one missing a declaration in the code you posted together with the others int(n, tot and x)

The problem in your code is the } you closed the main method and then tried to do a while. This wont work since while is not another method so you need to put the while inside the first } and delete the extra } in the end.

Sign up to request clarification or add additional context in comments.

Comments

1

first off, you probably want your while loop to be inside your main method

public static void main (String [] args )
{
    //other stuff
    while ( n < x )
    {
        //content
    }
}

Secondly, you should declare your variables based on whether or not you need them to persist across iterations of the while loop.

  • If you need the variable to keep it's value throughout all iterations of the loop then declare it in the main method.

  • If you're going to throw away the value after each iteration of the loop, then declare it inside the while loop.

In your case, it looks like tot and n should be declared outside of the loop, while s can be declared inside the loop

Comments

1

Here is an example of a while loop. Notice where the int variable is declared.The problem is not exactly like yours, but the concepts are similar.

public class foo {
    public static void main(String[] args) {
        int myInt = 7;
        boolean result = true;
        if (result == true)
            do
                System.out.println(myInt) ;
        while (myInt > 10);
    }
}

Comments

1

You need to learn about scope in object-oriented programming. The concept of scope allows or restricts access to variables based on where and how they are declared. See this article about declaring variables in Java. You should probably also familiarize yourself with the "Hello World" application that Oracle provides.

Basically, a Java class will look something like:

public class MyClass
{
    ...public or private variables accessible from any of the code below go here...
    public static void main(String[] args) {
        ...declare variables only accessible by the main method here...
        ...do things here...
    }

    ...declare other methods here...
    ...private methods can only be called by code in this class...
    ...public methods can be used by external code that uses this class...
    public int Foo()
    {
         ...declare some variables only accessible by Foo() code here...
         ...do some stuff...
    }
    private boolean Bar()
    {
         ...declare some variables only accessible by Bar() code here...
         ...do some stuff...
    }
}

You can't just throw the while loop out in the netherlands like that. It needs to go in the main method or another method that you create, perhaps one called:

public int CountScores(int numTimes)
{
     ...do some stuff...not sure exactly what you're trying to do 
}

I'd also probably use BufferedReader and InputStreamReader instead of Keyboard. See this example.

1 Comment

Sure @hitztp ... count the other response as the answer even though I gave a bunch of links and by far the most in-depth explanation. I see how it is...
0

I would organize your code like so:

public class Question2 {
    int n, tot, x; //declaring variables
    public Question2() //Class constructor {
    //define the class here (i.e., initialize variables(x = whatever), and write the
    // while loop
    }
    public static void main(String[] args) {
        Question2 q2 = new Question2() //Creates an instance of the class to compile
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.