Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java enter为空时如何中断程序_Java_Input - Fatal编程技术网

Java enter为空时如何中断程序

Java enter为空时如何中断程序,java,input,Java,Input,我有一个函数,例如,逐行读取数字,并计算这些数字的总和。 用户输入数字,每行一个数字,当输入“null”时,程序中断并给出结果。 例如: >8 >5 >4 >> 结果是17 当输入为空时,如何使程序中断并给出结果 string mystr; getline(cin,mystr); if(mystr.empty()) //do stuff else //do stuff 对不起,我看到这是一个C++问题,我看到的是java版本 Scanner scanner = ne

我有一个函数,例如,逐行读取数字,并计算这些数字的总和。 用户输入数字,每行一个数字,当输入“null”时,程序中断并给出结果。 例如:

>8
>5
>4
>>
结果是17

当输入为空时,如何使程序中断并给出结果

string mystr;
    getline(cin,mystr);
    if(mystr.empty())
//do stuff
    else 
 //do stuff
对不起,我看到这是一个C++问题,我看到的是java版本

Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        if(text.isEmpty())
            //do stuff
        else 
            //do stuff

这是我想到使用scanner的代码。代码中的注释是我认为您可能在哪些方面、关键领域以及您提出的问题上搞砸了

public static void sumInputs(){
    Scanner data=new Scanner(System.in);
    ArrayList <Double> allNumbers=new ArrayList<>();
    while(true){
        System.out.print("Number:");
        try{
            //IMPORTANT: Notice I use "nextLine" and not "next", because next will wait till user inputs something not null
            //and ignours the "enter" key pressed, while "nextLine" executes when the user presses the key "enter" regardless
            //of whether there is input.
            //I would imagine this is your problem
            String number=data.nextLine();

            //The part you are looking for
            //Right here is the part you are looking for
            if(!number.isEmpty()){
                //what to do if it is not null (store the numbers
                //in an ArrayList).
                allNumbers.add(Double.parseDouble(number));
            }else{
                //add up all the numbers if it is null
                double sum = 0;
                for( double i : allNumbers) {
                    sum += i;
                }
                System.out.println("The result is "+ sum);
                System.exit(0);
            }
        }catch(InputMismatchException e){
            System.out.println("Not number");
        }
    }
}
公共静态输入(){
扫描仪数据=新扫描仪(System.in);
ArrayList allNumbers=新的ArrayList();
while(true){
系统输出打印(“编号:”);
试一试{
//重要提示:注意我使用的是“nextLine”而不是“next”,因为next将等待用户输入非null的内容
//并忽略按下的“enter”键,而不管用户是否按下“enter”键,“nextLine”都会执行
//是否有输入。
//我想这是你的问题
字符串编号=data.nextLine();
//你要找的那部分
//这就是你要找的零件
如果(!number.isEmpty()){
//如果不为空,该怎么办(存储数字
//在ArrayList中)。
add(Double.parseDouble(number));
}否则{
//如果为空,则将所有数字相加
双和=0;
for(双i:所有数字){
总和+=i;
}
System.out.println(“结果为”+sum);
系统出口(0);
}
}捕获(输入不匹配异常e){
系统输出打印项次(“非编号”);
}
}
}

应用一些流控制:通过测试用户输入是否为空。然而,纯粹的代码编写请求与堆栈溢出无关——我们希望这里的问题与特定的编程问题有关——但我们很乐意帮助您自己编写!告诉我们,你被困在哪里了。这也将帮助我们更好地回答您的问题。@Elliott Frisch谢谢您的回答。问题是我在用手机写东西,所以我没能得到一个好的帖子。对不起,这正是我需要的,谢谢!