如何得到一个自然数序列,每一个数在一个新行中?

v09wglhw  于 2021-07-06  发布在  Java
关注(0)|答案(5)|浏览(301)

给定一个自然数序列。对于序列输出的每个编号 even 如果数字是偶数,否则, odd . 如果数字等于 0 ,程序必须停止读取和处理数字。

class Main {

    public static void main(String... args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        //int max = 0;
        int number = 0;

        while (scanner.hasNext()) {
            number = scanner.nextInt();
            if (number % 2 != 0) {
                System.out.println("Odd");
                continue;
            } else if (number % 2 == 0) {
                System.out.println("even");
                break;
            }
        }
    }
}

我有以下问题:
我可以´输入一个自然数序列,每一个数在一个新行中(请参阅附件)。我只能输入2个数字。。。

测试失败,第1页,共9页。答错了
这是问题陈述中的一个示例测试!
测试输入:

1
2
3
4
0

正确输出:

odd
even
odd
even

您的代码输出:

Odd
even
nr9pn0ug

nr9pn0ug1#

你不必扫描键盘上的数字

根据要求,您不必扫描键盘上的数字;相反,你必须从一个自然数序列中扫描它们。因此 Scanner 对象需要用自然数序列示例化,而不是 System.in .

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        String nums = "4 10 45 60 63 75 78 90 92 0 15 18";
        Scanner scanner = new Scanner(nums);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            if (n == 0) {
                break;
            }
            System.out.println(n + " => " + (n % 2 == 0 ? "Even" : "Odd"));
        }
    }
}

输出:

4 => Even
10 => Even
45 => Odd
60 => Even
63 => Odd
75 => Odd
78 => Even
90 => Even
92 => Even

更新(基于op的澄清)

您在评论中提到,您的要求确实是从键盘扫描数字。除此之外,您还提到要验证这些数字。
为此,可以使用无限循环(例如。 while(true) { } )当输入为 0 . 如果数字无效,只需显示一条错误消息,而不进行处理。
演示:

import java.util.InputMismatchException;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;
        while (true) {
            try {
                n = scanner.nextInt();
                if (n == 0) {
                    break;
                } else if (n < 0) {
                    System.out.println("It's not a natural number. Please try again.");
                } else {
                    System.out.println(n + " => " + (n % 2 == 0 ? "Even" : "Odd"));
                }
            } catch (InputMismatchException e) {
                System.out.println("It's not an integer. Please try again.");
            }
        }
    }
}

示例运行:

10
10 => Even
-2
It's not a natural number. Please try again.
5
5 => Odd
15
15 => Odd
16
16 => Even
-30
It's not a natural number. Please try again.
0
zc0qhyus

zc0qhyus2#

public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int number = 0;

    System.out.println("Start putting numbers");

    while (scanner.hasNext()) {
        number = scanner.nextInt();
            if (number % 2 != 0) {
                System.out.println("Odd");
                continue;
            } else if (number % 2 == 0){
                System.out.println("even");
            }
            if(number == 0)
                break;
            }
        }
    }
}
gmol1639

gmol16393#

public static void main(String... args) {
    Scanner scan = new Scanner(System.in);

    while (true) {
        int num = scan.nextInt();

        if (num == 0)
            break;

        System.out.println(num % 2 == 0 ? "even" : "odd");
    }
}
snz8szmq

snz8szmq4#

我认为您的代码中有一个输入错误:)请重构行:

System.out.println("Odd");

System.out.println("odd");

此外,您可能根本不调用循环控件(break,continue):

public static void main(String... args) {
    Scanner scanner = new Scanner(System.in);
    int number = 0;

    while ((number = scanner.nextInt()) != 0) {
        if (number % 2 != 0) {
            System.out.println("odd");
        } else {
            System.out.println("even");
        }

    }
}
nmpmafwu

nmpmafwu5#

当数字是偶数时,你不应该打破这个条件。应该有另一个条件来检查 number = 0 ```
public static void main(String[] args) {
// put your code here
Scanner scanner = new Scanner(System.in);
int number = 0;

    while (scanner.hasNext()) {
        number = scanner.nextInt();
        if(number == 0) {
             break;
        }
        else if (number % 2 == 0) {
            System.out.println("even");
        } else {
            System.out.println("odd");
        }
   }

}

相关问题