public class UseVariableValueAgain{
public static void main(String args[]){
int x=6;
int y=7;
int LastValue=0;// for first time
LastValue=x+y+LastValue;
System.out.println("result"+LastValue);
Scanner scan = new Scanner(System.in);
int x1=scan.nextInt();
int y1=scan.nextInt();
int LastValue1=0;
LastValue1=x1+y1+LastValue1;//for first time
System.out.println("REsult using Scanner="+LastValue1);
}
}
Ressult=13
5
6
Result using Scanner=11
when i execute this program i got 13 output by default and by using scanner i enter 5,6 and output is 11 , Now I want to use (13,11)values for next time when i re-execute the program. but it give same result
最佳答案
您的选择(按容易程度排序):
1) 下次调用程序时,在命令行传递值。
java UseVariableValueAgain 13 11
。您现在可以通过 args[] 数组访问这些值。请记住,您必须将其解析为整数,因为 args 是一个字符串数组。
2) 将这些值写入文件。 (例如使用BufferedWriter)并在程序中使用Scanner
或BufferedReader
读取它;
3) 将值写入数据库表。从程序中的表中读取值。
在所有这些选项中,您必须通过使用适当的 if-else 条件来检查是否需要从用户输入读取值或是否需要确定值来检查这是否是重新运行。 如果您正在学习 Java,我建议您按顺序尝试所有三个选项。
关于java - 关闭程序后如何重用以前的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205697/