接受一系列号码? for循环

时间:2015-09-23 13:03:20

标签: java loops for-loop numbers

我是编程新手,因此我不确定如何编写一个接受一系列数字的程序,只有当用户输入0时才会退出,然后显示在用户输入0之前输入了多少个数字。

我的代码:

import java.util.Scanner;
class Sample {
    public static void main(String[]args) {
        Scanner x = new Scanner(System.in);
        int y;
        int count = 0;

        System.out.print("Enter any number to continue. Enter 0 to stop.");
        num = x.nextInt();
        for(int n = 0; n<=num; n++) { //is this for loop right?
            y = x.nextInt();
            count += y;
        }
        if(num == 0){
            System.out.printf("You entered %d numbers,count");
        }
    }
} // i know my code is missing alot. 

输出应该是这样的。

Enter any number to continue. Enter 0 to stop 
12
11
5
6
1
0
You entered 6 numbers.
2 even
3 odd // i know my code is far from what i want my output to be.
EDIT :
if the user enters 2 then 0 the output should be
You Entered an even number // same with odd. how?

5 个答案:

答案 0 :(得分:2)

一些线索:

你的行

num = x.nextInt();

可能会导致编译错误,因为您尚未声明变量num

此外,您的循环应该继续,而数字大于0.为此,请使用 while 循环:

int num = 1; //some non-zero starting number
while (num != 0) {
    num = x.nextInt();
    count++;
}

答案 1 :(得分:1)

代码是自我描述的

public class Sample {
    public static void main(String[] args) {
        Scanner x = new Scanner(System.in);
        int odd = 0, even = 0, num = 0;
        System.out.println("Enter any number to continue. Enter 0 to stop.");
        while ((num = x.nextInt()) != 0) {

            if (num % 2 == 0) {
                even++;
            } else {
                odd++;
            }

        }
        System.out.printf("\nYou entered %d numbers\n", even + odd);
        System.out.printf("even %d\n", even);
        System.out.printf("odd %d\n", odd);

    }
}

答案 2 :(得分:1)

Working demo here

你关闭,只为每个计数获取变量,并在while循环中检查它:

public static void main(String[] args) {
    Scanner x = new Scanner(System.in);
    int num = 0; // to store user's enter
    int odd = 0, count = 0, even = 0; // to store all counts

    System.out.print("Enter any number to continue. Enter 0 to stop.");
    // ask for first number
    num = x.nextInt();

    // loop until 0 is entered
    while (num != 0) {
        // each time sum 1 because new number entered
        count ++;
        // if num / 2 modulus is 0 then even number 
        if (num % 2 == 0) {
            even ++;
        // if num / 2 modulus is not 0 then odd number 
        } else {
            odd ++;
        }

        // ask for a new number
        num = x.nextInt();
    }

    // print the results
    System.out.printf("You entered %d numbers\n",count);
    System.out.printf("You entered %d even numbers\n",even);
    System.out.printf("You entered %d odd numbers\n",odd);
}

<强>输出

Enter any number to continue. Enter 0 to stop.
12
11
5
6
1
0

You entered 6 numbers
You entered 2 even numbers
You entered 3 odd numbers

答案 3 :(得分:0)

您需要重写逻辑,因为在插入0时插入数字或处理所有数据 下面的示例为Process as inserted 虽然(真) {       读线       检查是否为零         休息       其他          检查是否奇怪             增加奇数nos计数           其他              增量均衡计数 } 显示你想要的东西

第二种情况 -

虽然(真实) {       读线       检查是否为零         休息       其他             将no添加到列表中 }

列表中的每个条目 {         检查是否奇怪             增加奇数nos计数           其他              增量均衡计数 } 显示你想要的东西

答案 4 :(得分:0)

System.out.println("Enter any number to continue. Enter 0 to stop.");
while (true) {
    int num = x.nextInt() ;

    if (num == 0) {
       break;
    }

    if (num % 2 == 0) {
       even++;
    } else {
       odd++;
    }

    count++;
}