0
import java.util.Scanner;

public class test {

    public static void main(String args[]){

        int a = 1000;
        while(a>0){

            System.out.println("Question to prevent infinite while loop");

            Scanner input = new Scanner (System.in);

            int inzet = input.nextInt();

            System.out.println(a);
            test(a);

         }  


    }

    public static void test(int a){


        System.out.println(a);
        a = a + 100;
        System.out.println(a);



        }
    }

I have a question. Why does int a not update? It resets to 1000 every time. I don't want that. Can someone please help me? If I run the program I get this:

Question to prevent infinite while loop
2
1000
1000
1100
Question to prevent infinite while loop
2
1000
1000
1100
Question to prevent infinite while loop
2
1000
1000
1100

I want to get this code:

Question to prevent infinite while loop
2
1000
1000
1100
Question to prevent infinite while loop
2
1100
1100
1200
Question to prevent infinite while loop
2
1200
1200
1300

Also, this is my first post ever. Please give me some feedback about how I can ask my question better next time.

Ulfalizer
  • 4,664
  • 1
  • 21
  • 30

4 Answers4

1

Your updated value is only visible within your test method. You can either make a a field in combination with removing your current declaration (both in your mainmethod as well as in your test arguments):

import java.util.Scanner;

public class test {

  private static int a = 1000;

  public static void main(String args[]) {

    while (a > 0) {
      System.out.println("Question to prevent infinite while loop");

      Scanner input = new Scanner(System.in);

      int inzet = input.nextInt();

      System.out.println(a);
      test();
    }
  }

  public static void test() {
    System.out.println(a);
    a = a + 100;
    System.out.println(a);
  }
}

Or you can return the updated value from your method in combination with assigning the returned value to a:

import java.util.Scanner;

public class test {

  public static void main(String args[]) {

    int a = 1000;
    while (a > 0) {
      System.out.println("Question to prevent infinite while loop");

      Scanner input = new Scanner(System.in);

      int inzet = input.nextInt();

      System.out.println(a);
      a = test(a);
    }
  }

  public static int test(int a) {
    System.out.println(a);
    a = a + 100;
    System.out.println(a);
    return a;
  }
}
Marvin
  • 13,325
  • 3
  • 51
  • 57
0

Make a class property like this:

public class test {
    static int a = 1000;
    //...
}
ljk321
  • 16,242
  • 7
  • 48
  • 60
0

In Java, int is a primitive object so when you pass it to the test function you actually passing a duplicate of this int.

You can use IntRef if you want to pass by refernce.

motcke
  • 438
  • 6
  • 17
0

Both first answers are correct

Read more about Reference vs Value here (Not specifically to Java): What's the difference between passing by reference vs. passing by value?

Specifically to Java here: Is Java "pass-by-reference" or "pass-by-value"?

The second solution (adding a static variable called a to you class) may work different. Since you example is quite simple it has the same output. But when you put the static modifier to a variable it will be shared between all instances of the class.

Community
  • 1
  • 1