Java的数字总和,直到输入字符串 [英] Java Sum of numbers until string is entered

查看:57
本文介绍了Java的数字总和,直到输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始进行Java编程,并且想知道如何解决或解决我遇到的这个问题.

i've just started java programming and was wondering on how to approach or solve this problem i'm faced with.

我必须编写一个程序,要求用户输入数字并不断求和输入的数字并打印结果.当用户输入"END"时该程序将停止

I have to write a program that asks a user for a number and continually sums the numbers inputted and print the result. This program stops when the user enters "END"

我似乎无法想到该问题的解决方案,整个问题的任何帮助或指导都将不胜感激,并且确实可以帮助我理解此类问题.这是我能做的最好的事

I just can't seem to think of a solution to this problem, any help or guidance throughout this problem would be much appreciated and would really help me understand problems like this. This is the best i could do

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);   

}   


}
    }   

输出应该看起来像这样:

The output is supposed to look like this:

输入数字:5

现在的总和是:5

输入数字:10

现在的总和是:15

输入数字:END

推荐答案

一种解决方案是不使用 Scanner#nextLine()方法,并使用字符串确认数字条目的输入#matches()方法以及一个小的正则表达式(正则表达式)的"\ d +" .该表达式检查整个字符串是否仅包含数字.如果是,则 matches()方法返回true,否则返回false.

One solution would be to not use the Scanner#nextInt() method at all but instead utilize the Scanner#nextLine() method and confirm the entry of the numerical entry with the String#matches() method along with a small Regular Expression (RegEx) of "\d+". This expression checks to see if the entire string contains nothing but numerical digits. If it does then the matches() method returns true otherwise it returns 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)与 unsigned 20相同,上面的代码段已将此考虑在内.

Since since an integer can be signed (ie: -20) or unsigned (ie: 20) and the fact that an Integer can be prefixed with a + (ie: +20) which is the same as unsigned 20, the code snippet above takes this into consideration.

这篇关于Java的数字总和,直到输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆