接受一系列的数字? for循环
accept a series of number? for loop
我是编程新手,所以我不确定如何编写一个程序来接受一系列数字,并且只有在用户输入 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?
一些线索:
你的线路
num = x.nextInt();
可能会导致编译错误,因为您还没有声明变量num
。
此外,您的循环应该继续 while 数字大于 0。为此,请使用 while 循环:
int num = 1; //some non-zero starting number
while (num != 0) {
num = x.nextInt();
count++;
}
您需要重写您的逻辑,如两种方式处理插入的数字或在插入 0 后处理所有
下面是插入过程的示例
虽然(真)
{
读行
检查是否为零
休息 ;
别的
检查是否奇数
增加奇数
别的
增加 evens 计数
}
显示你想要的
第二种情况 --
虽然(真)
{
读行
检查是否为零
休息 ;
别的
添加否到列表
}
对于列表中的每个条目
{
检查是否奇数
增加奇数
别的
增加 evens 计数
}
显示你想要的
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++;
}
代码是自描述的
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);
}
}
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
我是编程新手,所以我不确定如何编写一个程序来接受一系列数字,并且只有在用户输入 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?
一些线索:
你的线路
num = x.nextInt();
可能会导致编译错误,因为您还没有声明变量num
。
此外,您的循环应该继续 while 数字大于 0。为此,请使用 while 循环:
int num = 1; //some non-zero starting number
while (num != 0) {
num = x.nextInt();
count++;
}
您需要重写您的逻辑,如两种方式处理插入的数字或在插入 0 后处理所有
下面是插入过程的示例
虽然(真)
{
读行
检查是否为零
休息 ;
别的
检查是否奇数
增加奇数
别的
增加 evens 计数
}
显示你想要的
第二种情况 --
虽然(真)
{
读行
检查是否为零
休息 ;
别的
添加否到列表
}
对于列表中的每个条目 { 检查是否奇数 增加奇数 别的 增加 evens 计数 } 显示你想要的
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++;
}
代码是自描述的
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);
}
}
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