Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.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输入字符串之前的数字总和_Java_If Statement_While Loop_Sum - Fatal编程技术网

Java输入字符串之前的数字总和

Java输入字符串之前的数字总和,java,if-statement,while-loop,sum,Java,If Statement,While Loop,Sum,我刚刚开始java编程,想知道如何处理或解决我面临的这个问题 我必须编写一个程序,要求用户输入一个数字,并不断地对输入的数字求和,然后打印结果。 当用户输入“结束”时,此程序停止 我只是想不出一个解决这个问题的办法,在这个问题上的任何帮助或指导都将非常感谢,并将真正帮助我理解这样的问题。这是我能做的最好的了 public static void main(String[] args) { Scanner scan = new Scanner(System.in); while (tru

我刚刚开始java编程,想知道如何处理或解决我面临的这个问题

我必须编写一个程序,要求用户输入一个数字,并不断地对输入的数字求和,然后打印结果。 当用户输入“结束”时,此程序停止

我只是想不出一个解决这个问题的办法,在这个问题上的任何帮助或指导都将非常感谢,并将真正帮助我理解这样的问题。这是我能做的最好的了

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

while (true) {
    System.out.print("Enter a number: ");
    int x = scan.nextInt();
    System.out.print("Enter a number: ");
    int y = scan.nextInt();

    int sum = x + y;


    System.out.println("Sum is now: " + sum);   

}   


}
    }   
输出应该如下所示:

public static void main(String[] args) throws Exception {
    int sum = 0;
    Scanner scan = new Scanner(System.in);

    while (scan.hasNext()) {
        System.out.print("Enter a number: ");

        if (scan.hasNextInt())
            sum += scan.nextInt();
        else
            break;


        System.out.println("Sum is now: " + sum);
    }

    System.out.print("END");
}
输入一个数字:5

现在的总数是:5

输入一个数字:10

现在的总数是:15

输入一个数字:END

按如下方式操作:

public static void main(String[] args) throws Exception {
    int sum = 0;
    Scanner scan = new Scanner(System.in);

    while (scan.hasNext()) {
        System.out.print("Enter a number: ");

        if (scan.hasNextInt())
            sum += scan.nextInt();
        else
            break;


        System.out.println("Sum is now: " + sum);
    }

    System.out.print("END");
}
如果输入不是数字(
int
),则此操作将结束

正如注释中指出的,如果您希望在用户特别输入“END”时停止程序,请将
else
-语句更改为:

else if (scanner.next().equals("END"))
    break;
这样做:

public static void main(String[] args) throws Exception {
    int sum = 0;
    Scanner scan = new Scanner(System.in);

    while (scan.hasNext()) {
        System.out.print("Enter a number: ");

        if (scan.hasNextInt())
            sum += scan.nextInt();
        else
            break;


        System.out.println("Sum is now: " + sum);
    }

    System.out.print("END");
}
如果输入不是数字(
int
),则此操作将结束

正如注释中指出的,如果您希望在用户特别输入“END”时停止程序,请将
else
-语句更改为:

else if (scanner.next().equals("END"))
    break;

一种解决方案是根本不使用该方法,而是使用该方法,并使用该方法和一个小的(“\d+”)正则表达式确认数值输入。此表达式检查整个字符串是否只包含数字。如果是,则matches()方法返回true,否则返回false

Scanner scan = new Scanner(System.in);
int sum = 0; 
String val = "";
while (val.equals("")) {
    System.out.print("Enter a number (END to quit): ");
    val = scan.nextLine();
    // Was the word 'end' in any letter case supplied?
    if (val.equalsIgnoreCase("end")) {
        // Yes, so break out of loop.
        break;
    }
    // Was a string representation of a 
    // integer numerical value supplied?  
    else if (val.matches("\\-?\\+?\\d+")) {
        // Yes, convert the string to integer and sum it. 
        sum += Integer.parseInt(val);
        System.out.println("Sum is now: " + sum);  // Display Sum
    }
    // No, inform User of Invalid entry
    else {
        System.err.println("Invalid number supplied! Try again...");
    }
    val = "";  // Clear val to continue looping
}

// Broken out of loop with the entry of 'End"
System.out.println("Application ENDED"); 
编辑:基于评论:


因为一个整数可以是有符号的(即:-20)或无符号的(即:20),而且一个整数的前缀可以是与无符号的(即:+20)相同的,上面的代码片段考虑到了这一点。

一个解决方案是根本不使用该方法,而是使用该方法,并使用该方法以及一个小的(“\d+”)值(RegEx)来确认数字条目的输入。此表达式检查整个字符串是否只包含数字。如果是,则matches()方法返回true,否则返回false

Scanner scan = new Scanner(System.in);
int sum = 0; 
String val = "";
while (val.equals("")) {
    System.out.print("Enter a number (END to quit): ");
    val = scan.nextLine();
    // Was the word 'end' in any letter case supplied?
    if (val.equalsIgnoreCase("end")) {
        // Yes, so break out of loop.
        break;
    }
    // Was a string representation of a 
    // integer numerical value supplied?  
    else if (val.matches("\\-?\\+?\\d+")) {
        // Yes, convert the string to integer and sum it. 
        sum += Integer.parseInt(val);
        System.out.println("Sum is now: " + sum);  // Display Sum
    }
    // No, inform User of Invalid entry
    else {
        System.err.println("Invalid number supplied! Try again...");
    }
    val = "";  // Clear val to continue looping
}

// Broken out of loop with the entry of 'End"
System.out.println("Application ENDED"); 
编辑:基于评论:


因为一个整数可以是有符号的(即:-20)或无符号的(即:20),而且一个整数的前缀可以是与无符号的(即:+20)相同的,上面的代码段考虑到了这一点。

如果程序只应在用户明确输入“END”时停止,请将else更改为this else If:
else If(scan.hasNext()&&“END”.equals(scan.next()){
感谢您的添加!如果程序只应在用户明确输入“END”时停止,将else更改为此else if:
else if(scan.hasNext()&&“END.equals(scan.next())){
谢谢你的添加!非常感谢你的加入,这确实有助于理解这个问题。除了负数之外,它工作得非常好。如果我想在求和中包含负数,我不知道它为什么会这样做,但是当第一个数的值为-10时,它返回一个空的新行。这是因为求和line为正或相等?因此负int返回空值?任何清除都将非常好,否则非常感谢您的帮助:)@DevilsHnd@utnicho-我看到…将
else if(val.matches(\\d+){
更改为:
else if(val.matches(\\-?\\d+)){
。这意味着提供的值可能会被签名,也可能不会被签名。非常感谢,这确实有助于理解问题。除了负数之外,它工作得非常好。如果我想在求和中包含负数,我不知道它为什么会这样做,但是当第一个数字为i-10时,它返回一个空new行。这是因为求和行是加号还是相等?因此负整数返回空值?任何清除都会很好,否则非常感谢您的帮助:)@DevilsHnd@utnicho-我看到…将
else if(val.matches(\\d+){
更改为:
else if(val.matches(\\-?\\d+)){
。这意味着提供的值可能有签名,也可能没有签名。