简体   繁体   English

我怎么能用最多 100 个扫描仪编号循环并且它们的总和也不会超过 100

[英]How can i do while loop with scanner numbers up to 100 and their sum also will not be over 100

Scanner scanner = new Scanner(System.in);
System.out.println("Enter number");

int input = 0;
int sum = 0;
while (input != 100) {
    input  = scanner.nextInt();
    if (input + sum > 100)
        break;

    sum += input;
}

System.out.println("Sum of Numbers : " + sum);

I have the following task:我有以下任务:

  • Write a program that asks the user to enter a number.编写一个程序,要求用户输入一个数字。

  • If the number is less than 100, then ask user to enter another number and sum both of them.如果数字小于 100,则要求用户输入另一个数字并将它们相加。

  • Keep asking the user to enter numbers until the sum of all entered numbers is at least 100.不断要求用户输入数字,直到所有输入数字的总和至少为 100。

If the first number entered by the user is more than or equal to 100, print message “This number is already more than 100” and do not ask the user to enter any other numbers.如果用户输入的第一个数字大于或等于100,则打印“这个数字已经大于100”,并且不要求用户输入任何其他数字。

I can print the sum of numbers entered by user but I just can't stop it.我可以打印用户输入的数字总和,但我无法阻止它。 Even if I use break , it breaks after the 100 number.即使我使用break ,它也会在 100 号之后中断。

This problem can be better handled using do-while loop which guarantees to execute its block at least once.使用 do-while 循环可以更好地处理这个问题,该循环保证至少执行一次其块。

Given below is the sample code as per your requirement:以下是根据您的要求提供的示例代码:

import java.util.Scanner;

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

        int input = 0;
        int sum = 0;

        do {
            System.out.print("Enter number: ");
            input = scanner.nextInt();
            if (input > 100) {
                System.out.println("his number is already more than 100");
                break;
            }
            if (sum + input <= 100)
                sum += input;

        } while (sum < 100);

        System.out.println("Sum of Numbers : " + sum);
    }
}

A sample run:示例运行:

Enter number: 10
Enter number: 20
Enter number: 70
Sum of Numbers : 100

Another sample run:另一个示例运行:

Enter number: 101
his number is already more than 100
Sum of Numbers : 0

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

相关问题 While 循环 1 - 100 之间的质数 - While Loop Prime Numbers between 1 - 100 由于我不想超过 100,我应该如何限制最多 100? JAVA - How am I supposed to limit up to 100 since I do not want to exceed it to 100? JAVA Integers.parseint(在扫描仪输入上),我也想进行限制,因此用户只能输入0到100范围内的数字 - Integers.parseint( on scanner input ), also i want to make limits so user can only input number in range from 0 to 100 总和的立方等于100 - sum numbers cubed equal to 100 如何为在循环中创建的100个文本字段创建changeListener? - How do I create a changeListener for 100 textfields that were created in a loop? JAVA-使用FOR,WHILE和DO WHILE循环求和1到100 - JAVA - using FOR, WHILE and DO WHILE loops to sum 1 through 100 找到数字100中的数字总和! (我的while循环不会停止) - Find the sum of the digits in the number 100! (My while loop would not stop) 当总和超过100时仍如何执行总和和平均值,如何获取此代码以停止输入? - How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average? 如何创建具有100个随机数且每行15个数字的文件? - How do I create a file with 100 random numbers with 15 numbers on each line? 如何从 4,000,000,000 个号码中获取最频繁的 100 个号码? - How can I get the most frequent 100 numbers out of 4,000,000,000 numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM