Java int 不更新。它重置
Java int doesn't update. It resets
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);
}
}
我有一个问题。为什么 int a
不更新?每次都重置为 1000。我不想要那个。有人可以帮帮我吗?如果我 运行 我得到这个程序:
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
我要获取此代码:
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
另外,这是我第一次 post。请给我一些反馈,告诉我下次如何更好地提出我的问题。
像这样制作a
class属性:
public class test {
static int a = 1000;
//...
}
在Java中,int是一个原始对象所以当你将它传递给测试函数时,你实际上传递了这个int的副本。
如果你想通过reference,你可以使用IntRef
。
您的更新值仅在您的 test
方法中可见。您可以将 a
字段与删除当前声明相结合(在您的 main
方法和 test
参数中):
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);
}
}
或者您可以 return 从您的方法中更新值并结合将 returned 值分配给 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;
}
}
前两个答案都正确
在这里阅读更多关于参考与价值的信息(不专门针对 Java):What's the difference between passing by reference vs. passing by value?
具体到 Java 这里:Is Java "pass-by-reference" or "pass-by-value"?
第二种解决方案(为您添加一个名为 a 的静态变量 class)可能会有所不同。由于您的示例非常简单,因此它具有相同的输出。但是当你把静态修饰符放在一个变量上时,它将在 class.
的所有实例之间 共享。
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);
}
}
我有一个问题。为什么 int a
不更新?每次都重置为 1000。我不想要那个。有人可以帮帮我吗?如果我 运行 我得到这个程序:
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
我要获取此代码:
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
另外,这是我第一次 post。请给我一些反馈,告诉我下次如何更好地提出我的问题。
像这样制作a
class属性:
public class test {
static int a = 1000;
//...
}
在Java中,int是一个原始对象所以当你将它传递给测试函数时,你实际上传递了这个int的副本。
如果你想通过reference,你可以使用IntRef
。
您的更新值仅在您的 test
方法中可见。您可以将 a
字段与删除当前声明相结合(在您的 main
方法和 test
参数中):
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);
}
}
或者您可以 return 从您的方法中更新值并结合将 returned 值分配给 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;
}
}
前两个答案都正确
在这里阅读更多关于参考与价值的信息(不专门针对 Java):What's the difference between passing by reference vs. passing by value?
具体到 Java 这里:Is Java "pass-by-reference" or "pass-by-value"?
第二种解决方案(为您添加一个名为 a 的静态变量 class)可能会有所不同。由于您的示例非常简单,因此它具有相同的输出。但是当你把静态修饰符放在一个变量上时,它将在 class.
的所有实例之间 共享。