user2965711
user2965711

Reputation: 77

how to increase the value of the variable

Hi guys i am new new to java i have my home work to be done where i declare the global variable but my variabale is changing .

Program :

main()
{

    public static final double j =20;   
    public static final double l =5;

    if (l=5)
    {
        for (; j<=50  ; j+=2)
        {
            System.out.printf("value of j is %d\n",j);
        }

        for (; j>=4; j-=2) // i want here the value j to be 20 ... 
        {
            System.out.printf("value of decrement is %d\n",j);
        }

    }
}

its working when i intialze once again j=20 in decreament for loop ... but i want j to start from 20

Upvotes: 0

Views: 874

Answers (4)

Aman Arora
Aman Arora

Reputation: 1252

To start with , there is nothing called Global Variable in JAVA. Plus you have so many compilation errors in the code: I will list out some:

  1. return type of main() function is missing.

    void main() { //code here }

  2. static and public modifiers are not for local variables(method variables)

    public static final double j =20; // this is wrong inside a method.
    Reason: Inside the methods variables have local scope. There is no public/private scope for method variables. So it should be :

    final double j =20; //final means 'j' behaves as a constant

  3. You are trying to assign 5 to l inside if :

    if (l=5) // it will not compile because first l will become 5 and inside if the expression should be boolean. it should be if(l==5).

  4. for (; j<=50 ; j+=2) will not compile because j is declared as a final variable.

Fix can be : for (int jNew=0;jNew<=50;jNew++)

So the overall code can be :

void main()
{
      final double j =20; 

      final double l =5;


    if (l==5)
    {
    for (int j3=0; j3<=50  ; j3+=2)
    {
        System.out.println(j);

    }
    for (int j4=0; j4>=4; j4-=2) // i want here the value j to be 20 ... 

    {
        System.out.println(j);

    }


}
}

Go through the basics of Java here.

Upvotes: 4

atripathi
atripathi

Reputation: 908

The reason you are getting an unexpected value (52) for j is the statement j+=2.

On each iteration, this overwrites the value of j with the incremented value.

After a few iterations of the loop, the value of j becomes 52 which causes the loop to exit since the condition j<=50 isn't satisfied.

Hence, before the start of the second loop, you need to reinitialize j with the value 20.

Note:

If you want i & j to be public static, declare them outside the method but within the class.

Since you do not need decimal numbers, use int instead of double .

Upvotes: 0

Bolesław Denk
Bolesław Denk

Reputation: 573

Not to the question itself but please have in mind that in Java main method should be declared like this:

public static void main(String[] args) { ... }

Reason for that can be found here: Why is the Java main method static?

Upvotes: 0

Sachin
Sachin

Reputation: 40990

You can simply declare the variable in with the loop itself

for(j=20; j>=4; j-=2) // i want here the value j to be 20 ... 
{
   System.out.printf("value of decrement is %d\n",j);
}

Upvotes: 1

Related Questions