java - 关闭程序后如何重用以前的变量值

标签 java

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)并在程序中使用ScannerBufferedReader读取它;

3) 将值写入数据库表。从程序中的表中读取值。

在所有这些选项中,您必须通过使用适当的 if-else 条件来检查是否需要从用户输入读取值或是否需要确定值来检查这是否是重新运行。 如果您正在学习 Java,我建议您按顺序尝试所有三个选项。

关于java - 关闭程序后如何重用以前的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205697/

相关文章:

java - 调试时找不到源

java - libjvm.so : cannot open shared object file: No such file or directory

java - 将 .key 和 .pem 文件导入 jks 文件并在 Java/Spring 中使用

java - butterknife 中的方法 "onClick"不起作用?

java - 如何检查 spring-integration SI flow 中的任何 channel 是否处于处理状态?

java - 如何使用 Java 获取 Parquet 文件大小和行数?

java - 胖客户端 session 管理和安全

java - 使用mockito-inline 模拟特定方法不完整的类

java - Android数据绑定(bind): Missing return statement in generated code when calling custom binding adapter more than once

java - Tomcat 忽略 post 参数请求