简体   繁体   English

如何使用for循环输入10个数字并仅打印正数?

[英]How to use for loop to input 10 numbers and print only the positives?

I'm trying to make a "for" loop in which it asks the user to input 10 numbers and then only print the positives. 我试图做一个“ for”循环,要求用户输入10个数字,然后只打印正数。

Having trouble controlling the amount of inputs. 无法控制输入量。 I keep getting infinite inputs until I add a negative number. 我不断获得无限的输入,直到我添加一个负数。

import java.util.Scanner;

public class ej1 {
    public static void main(String args[]) {

        int x;

        for (x = 1; x >= 0; ) {
            Scanner input = new Scanner(System.in);
            System.out.print("Type a number: ");
            x = input.nextInt();
        }
    }
}

From a syntax point of view, you've got several problems with this code. 从语法的角度来看,此代码存在一些问题。

  • The statement for (x = 1; x >= 0; ) will always loop, since x will always be larger than 0, specifically because you're not introducing any kind of condition in which you decrement x . for (x = 1; x >= 0; )的语句将始终循环,因为x始终大于0,特别是因为您没有引入任何使xx的条件。

  • You're redeclaring the scanner over and over again. 您要一遍又一遍地声明扫描仪。 You should only declare it once, outside of the loop. 您只应在循环外部声明一次。 You can reuse it as many times as you need. 您可以根据需要多次重复使用它。

  • You're going to want to use nextLine() after nextInt() to avoid some weird issues with the scanner. 您将要nextInt()之后使用nextLine()以避免扫描仪出现一些奇怪的问题。

    Alternatively, you could use nextLine() and parse the line with Integer.parseInt . 另外,您可以使用nextLine()并使用Integer.parseInt解析该行。

That said, there are several ways to control this. 也就是说,有几种方法可以控制此情况。 Using a for loop is one approach, but things get finicky if you want to be sure that you only ever print out ten positive numbers, regardless of how many negative numbers are entered. 使用for循环是一种方法,但是如果要确保仅打印出十个正数,而不管输入了多少个负数,事情就会变得很棘手。 With that, I propose using a while loop instead: 这样,我建议改为使用while循环:

int i = 0;
Scanner scanner = new Scanner(System.in);
while(i < 10) {
    System.out.print("Enter a value: ");
    int value = scanner.nextInt();
    scanner.nextLine();
    if (value > 0) {
        System.out.println("\nPositive value: " + value);
        i++;
    }
}

If you need to only enter in ten values, then move the increment statement outside of the if statement. 如果需要输入十个值,则将增量语句移至if语句之外。

i++;
if (value > 0) {
    System.out.println("\nPositive value: " + value);
}

As a hint: if you wanted to store the positive values for later reference, then you would have to use some sort of data structure to hold them in - like an array. 提示:如果您想存储正值以供以后参考,则必须使用某种数据结构将其保存在其中-就像数组一样。

int[] positiveValues = new int[10];

You'd only ever add values to this particular array if the value read in was positive, and you could print them at the end all at once: 如果读入的值是正数,则只将值添加到该特定数组中,并且可以一次将它们打印在最后:

// at the top, import java.util.Arrays
System.out.println(Arrays.toString(positiveValues));

...or with a loop: ...或循环播放:

for(int i = 0; i < positiveValues.length; i++) {
    System.out.println(positiveValues[i]);
}
Scanner scan = new Scanner(System.in);
int input=-1;
for(int i=0;i<10;i++)
{
 input = sc.nextInt();
if(input>0)
System.out.println(input);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM