加法数字 Java,并且通过输入 0 停止。

huangapple go评论99阅读模式
英文:

Addition numbers java and stops by entering 0

问题

  1. 我想进行加法运算,如果用户输入数字0,则将所有数字相乘,程序停止运行。我已经进行了搜索并不断尝试,但仍然无法弄清楚。
  2. ```java
  3. import java.util.Scanner;
  4. public class counting {
  5. public static void main (String[] args) {
  6. int number = 0, stop = 0;
  7. Scanner kb = new Scanner (System.in);
  8. while(true) {
  9. System.out.print("请输入一个数字(输入0停止):");
  10. number += kb.nextInt();
  11. if (number == stop) {
  12. System.out.println("所有数字的乘积:" + number);
  13. return;
  14. }
  15. }
  16. }
  17. }
  1. <details>
  2. <summary>英文:</summary>
  3. I want to make an addition and if the user enters the digit 0 he multiplies all numbers together and the program stops. I&#39;ve already been searching and keep trying, but I still can&#39;t figure it out.

import java.util.Scanner;

public class counting {
public static void main (String[] args) {
int number = 0, stop = 0;

  1. Scanner kb = new Scanner (System.in);
  2. while(true) {
  3. System.out.print(&quot;Enter a number (stop with 0): &quot;);
  4. number += kb.nextInt();
  5. if (number == stop) {
  6. System.out.println(&quot;Outcome of the numbers &quot; + number);
  7. return;
  8. }
  9. }
  10. }

}

  1. </details>
  2. # 答案1
  3. **得分**: 1
  4. 你需要将输入与 ``stop`` 进行比较,而不是与总 ``number`` 进行比较:
  5. ```java
  6. int number = 0, stop = 0;
  7. Scanner kb = new Scanner(System.in);
  8. while (true) {
  9. System.out.print("输入一个数字(输入 0 停止):");
  10. int input = kb.nextInt();
  11. number += input;
  12. if (input == stop) {
  13. System.out.println("数字的总和为 " + number);
  14. return;
  15. }
  16. }

示例输入/输出:

  1. 输入一个数字(输入 0 停止):1
  2. 输入一个数字(输入 0 停止):2
  3. 输入一个数字(输入 0 停止):3
  4. 输入一个数字(输入 0 停止):0
  5. 数字的总和为 6
英文:

You need to compare the input with the stop, not the total number:

  1. int number = 0, stop = 0;
  2. Scanner kb = new Scanner (System.in);
  3. while(true) {
  4. System.out.print(&quot;Enter a number (stop with 0): &quot;);
  5. int input = kb.nextInt();
  6. number += input;
  7. if (input == stop) {
  8. System.out.println(&quot;Outcome of the numbers &quot; + number);
  9. return;
  10. }
  11. }

Sample input/output:

  1. Enter a number (stop with 0): 1
  2. Enter a number (stop with 0): 2
  3. Enter a number (stop with 0): 3
  4. Enter a number (stop with 0): 0
  5. Outcome of the numbers 6

huangapple
  • 本文由 发表于 2020年10月4日 03:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64188168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定