hitztp
hitztp

Reputation: 41

declaring data type of a variable in java

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

Upvotes: 1

Views: 664

Answers (5)

newsomedenzell
newsomedenzell

Reputation: 24

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
    }
} 

Upvotes: 0

jreancsu
jreancsu

Reputation: 421

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.

Upvotes: 1

Michael
Michael

Reputation: 63

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

Upvotes: 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

Upvotes: 1

Oscar Picchi
Oscar Picchi

Reputation: 174

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.

Upvotes: 1

Related Questions