简体   繁体   English

我想显示在我的 while 循环中输入的每个数字。所以我该怎么做

[英]i want to display every number entered in my while loop.so how can i do this

must create a java application that will determine and display sum of numbers as entered by the user.The summation must take place so long the user wants to.when program ends the summation must be displayed as follows eg say the user enters 3 numbers 10 + 12+ 3=25必须创建一个 java 应用程序,该应用程序将确定并显示用户输入的数字总和。总和必须在用户想要的时间内进行。当程序结束时,总和必须显示如下,例如,假设用户输入 3 个数字 10 + 12+ 3=25

and you must use a while loop你必须使用一个 while 循环

Here's a function to do just that.这是一个 function 来做到这一点。 Just call the function whenever you need.只需随时拨打 function。

Ex: System.out.println(parseSum("10 + 12+ 3"))25例如: System.out.println(parseSum("10 + 12+ 3"))25

public static int parseSum(String input) {
  // Removes spaces
  input = input.replace(" ", "");
  
  int total = 0;
  String num = "";
  int letter = 0;

  // Loop through each letter of input
  while (letter < input.length()) {
    // Checks if letter is a number
    if (input.substring(letter, letter+1).matches(".*[0-9].*")) {
      // Adds that character to String
      num += input.charAt(letter);
    } else {
      // If the character is not a number, it turns the String to an integer and adds it to the total
      total += Integer.valueOf(num);
      num = "";
    }
    letter++;
  }
  total += Integer.valueOf(num);
  return total;
}

The while loop is essentially a for loop though. while 循环本质上是一个 for 循环。 Is there a specific reason why you needed it to be a while loop?是否有特定原因需要它成为 while 循环?

There is a lot of ways to achieve this.有很多方法可以实现这一点。 Here an example of code that could be improve (for example by catching an InputMismatchException if the user doesn't enter a number).这是一个可以改进的代码示例(例如,如果用户未输入数字,则捕获 InputMismatchException)。 Please for the next time, post what you have tried and where you stuck on.下次请发布您尝试过的内容以及坚持的地方。

public static void main (String[] args) {
    boolean playAgain = true;
    while(playAgain) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the first number : ");
        int nb1 = sc.nextInt();
        System.out.println("Ok! I got it! Please enter the second number : ");
        int nb2 = sc.nextInt();
        System.out.println("Great! Please enter the third and last number : ");
        int nb3 = sc.nextInt();
        int sum = nb1+nb2+nb3;
        System.out.println("result==>"+nb1+"+"+nb2+"+"+nb3+"="+sum);
        boolean validResponse = false;
        while(!validResponse) {
            System.out.println("Do you want to continue ? y/n");
            String response = sc.next();
            if(response.equals("n")) {
                System.out.println("Thank you! see you next time :)");
                playAgain = false;
                validResponse = true;
            } else if(response.equals("y")) {
                playAgain = true;
                validResponse = true;
            } else {
                System.out.println("Sorry, I didn't get it!");
            }
        }
    }
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何使我的while循环按我希望的方式工作? - How can I make my do while loop work the way I want it to? 我想在DoubleClick事件中显示一些文本怎么办? 我已经附上我的源代码 - I want to display some text onDoubleClick event how to do so ? i have attached my sourcecode Along 如何根据输入的数字显示星号? - How do I display the asterisks according to the number entered? 如何在循环时修复我的随机数? - How do I fix my random number while loop? 如何让我的 println 以我想要的方式在这个 for 循环中用多行打印轮廓/标签? - How do make it so my println outlines/tabs the way I want in this for loop with multiple lines to print? 如果输入了非 Int 值,如何退出 while 循环? - How can I exit while loop if a non Int value is entered? 我可以在 for 循环中运行 while 循环吗? [Java] 如果是这样,在这种情况下我该怎么做? - Can I run a while loop inside a for loop? [Java] If so, how would I do it in this scenario? 如何使用表示 split() 的 while 循环和 for 循环来为用户显示选择编号? - How can I use while loop that represents split() and also for loop to display selection number for user? 我想获取联系人姓名和电话号码,但是我的代码只是打印联系人姓名,所以我怎么也可以获取电话号码 - I want to get the contact name and number but my code just prints the contact name so how can I also get the number 如何在代码中添加do while循环? - How can I add a do while loop to my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM