简体   繁体   English

Java 程序从用户那里读取不定数量的数字,直到输入正数

[英]Java program reading indefinite amount of numbers from a user till a positive is entered

I need to write a java program reading in an indefinite amount of numbers and saves them to a collection of numbers, until an (even number) is entered in by the user.我需要编写一个 java 程序读取不确定数量的数字并将它们保存到数字集合中,直到用户输入一个(偶数)。 I have tried with a while loop, that when a positive number is found in it it stops.我尝试了一个while循环,当在其中找到一个正数时它会停止。 But it is not really working.但它并没有真正起作用。 Here is codes I have tried:这是我尝试过的代码:

public static void main(String[] args) {公共 static 无效主(字符串 [] 参数){

    int programInteger = 1;
    int inputtedInteger;
    
    while (programInteger < 2) {
    System.out.println("Enter a number: "); //Asks user to input a number
    Scanner in = new Scanner(System.in);
    inputtedInteger = Scanner(in.nextLine);
    
    if (inputtedInteger != 0) {
        System.out.println("The number "+ inputtedInteger +" that you inputted is not an even number, try again: ");
        inputtedInteger=in.nextInt();
    }
    else if (inputtedInteger % 2 == 0){ 
        programInteger =+1;
        System.out.println("The number "+inputtedInteger+" that you entered is an even number!");
    }
    
}
    
}
/*  if(inputtedInteger % 2 == 0) {
        System.out.println("The number "+ inmatatTal +" you entered is an even number!");
    }
        else {
            System.out.println("Enter a number?! ");
            inputtedInteger = in.nextInt();
        
        }

Fixing a few things in the logic of the loop should work:修复循环逻辑中的一些事情应该可以工作:

int inputtedInteger = 0;
Scanner in = new Scanner(System.in);
    
while (inputtedInteger < 1) {
    System.out.println("Enter a number: "); //Asks user to input a number
    inputtedInteger = in.nextInt();
    
    if (inputtedInteger % 2 != 0) {
        System.out.println("The number "+ inputtedInteger +" that you inputted is not an even number, try again: ");
    }
    else if (inputtedInteger % 2 == 0){ 
        System.out.println("The number "+inputtedInteger+" that you entered is an even number!");
    }
    
}

I would setup an outline for the code like this:我会为这样的代码设置一个大纲:

setup Scanner, create collection
while true:
   userInput = scanner.nextInt()
   if userInput > 0: break
   collection.add(userInput)
print('user entered', collection.length(), 'numbers.')

Hope that helps.希望有帮助。 I'll leave you to fill this using actual Java syntax.我将让您使用实际的 Java 语法来填写此内容。 I wrote a comment on the OP on why your structure is failing to solve the issue at hand.我在 OP 上写了一篇评论,说明为什么您的结构无法解决手头的问题。

暂无
暂无

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

相关问题 简单的Java App可计算不确定数量的输入数字的平均值 - Simple Java App to calculate the average of an indefinite amount of entered numbers 读取用户的5个数字并计算输入正数的频率 - Read in 5 numbers from a user and compute the frequency of positive numbers entered 根据大小对一定数量的用户输入数字进行排序(Java) - Sort an amount of user entered numbers according to size (Java) java,统计用户输入的数字 - java, counting the numbers entered by the user 爪哇。 尝试仅计算正数的平均值,如果未输入正数,也会显示一条消息 - Java. Trying to calculate average of positive numbers only, also displaying a message if no positive numbers are entered Java:如何在用户输入数字之前打印相应的字母? - Java: How to print corresponding alphabets till a number entered by user? 读取word模板用户在java中输入的值 - Reading word template user entered values in java 程序仅读取输入的第一个数字,而用户程序输入的其余数字将被忽略,并且始终读取第一个数字? - Program reading only the first number was entered, the rest of the number entered by the user program ignores and reading always the first number? 从用户读取正数,直到他们输入负数。 打印出读取到的最大正数。 爪哇 - Read in positive numbers from the user until they enter a negative number. Print out the largest positive number that was read. Java 通过在Java中乘以正数获得负数 - Getting negative numbers from multiplying positive in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM