计算用户输入的偶数和奇数 - java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26331268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count Even and Odd Numbers User has Inputted - java
提问by GGstudent
Hi this is what a question on a course I am doing wants...
嗨,这是我正在做的课程中想要的问题...
- The program prints "Type numbers” until the user types the number -1. When the user types the number -1, the program prints "Thank you and see you later!" and ends
- the program should print the sum of the numbers entered by the user (without the number -1).
- the program should print how many numbers the user typed (without the number -1).
the program should print the average of the numbers the user typed (without the number -1).
5. the program should print the number of even and odd numbers that the user typed (without the number -1).
- 程序打印“Type numbers”,直到用户输入数字-1。当用户输入数字-1时,程序打印“Thank you and see you later!”并结束
- 程序应该打印用户输入的数字的总和(没有数字 -1)。
- 程序应该打印用户输入了多少个数字(没有数字 -1)。
程序应该打印用户输入数字的平均值(没有数字 -1)。
5. 程序应该打印用户输入的偶数和奇数的个数(没有数字-1)。
I have completed 1-4 but am completely stuck on 5. I did make a start on trying to work it out including putting a for loop inside my while loop but apart from the fact it didnt work it looked well out of place so i removed it. Anyway here is what i have done so far which as I say all works in its own magical way.
我已经完成了 1-4,但完全停留在 5。我确实开始尝试解决它,包括在我的 while 循环中放置一个 for 循环,但除了它没有工作的事实之外,它看起来很不合适,所以我删除了它。无论如何,这就是我到目前为止所做的一切,正如我所说,所有这些都以其神奇的方式运作。
And so if anyone can help me with question 5that would be great. ps.I'm sure what i've done so far could have been written better but don't focus on that for now cos for me and where i am with Java this is nothing short of a miracle.x
因此,如果有人可以帮助我解决问题 5,那就太好了。ps.我确信我到目前为止所做的事情本来可以写得更好,但现在不要专注于此,因为对我来说以及我在 Java 中所处的位置,这简直就是一个奇迹。x
import java.util.Scanner;
public class LoopsEndingRemembering {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int sum = 1; // to counteract the -1 from the user
int total = 0;
double avg;
//int even = 0;
//int odd = 0;
System.out.println("Type numbers:");
while (true) {
int numbers = Integer.parseInt(reader.nextLine());
sum += numbers;
total++;
avg = ((double) sum) / (total - 1);
if (numbers == -1) {
System.out.println("Thank you and see you later!");
System.out.println("The sum is " + sum);
System.out.println("How many numbers: " + (total - 1));
System.out.println("Average: " + avg);
//System.out.println("Even numbers: " + even);
//System.out.println("Odd numbers: " + odd);
break;
}
}
}
}
采纳答案by Slendertron
You could store variables where one counts odd numbers and one counts even numbers. If the number is odd, increment the odd numbers variable. If even, increment the even numbers variable. Use the % operator to get the remainder of the input divided by 2.
您可以存储一个计算奇数和一个计算偶数的变量。如果数字是奇数,则增加奇数变量。如果偶数,则增加偶数变量。使用 % 运算符获取输入的余数除以 2。
int oddNumbers = 0;
int evenNumbers = 0;
if(numbers % 2 == 1){
oddNumbers++;
} else if(numbers % 2 == 0){
evenNumbers++;
}
回答by laurlijeong
import java.util.Scanner; public class Main {
导入 java.util.Scanner; 公共课主要{
public static void main(String[] args) {
Scanner in= new Scanner (System.in);
int sum = 0;
System.out.print("Enter limit number: ");
int limit = in.nextInt();
int oddNumbers = 0;
int evenNumbers = 0;
for(int i= 1;i<=limit;i++)
{
System.out.println("Enter limit number: "+i+"");
int numbers= in.nextInt();
if(numbers %2==0)
{
evenNumbers++;
}
else if(numbers %2==1)
{
oddNumbers++;
}
}
System.out.println("There are: "+oddNumbers+" odd numbers");
System.out.println("There are :"+evenNumbers+" even numbers");
}
}
回答by Dinesh Bhosale
import java.lang.Math;
public class HelloWorld {
public static void main(String[] args) {
int firstnum = 4;
int lastnum = 104;
int evncnt, oddcnt;
int count;
System.out.println("First number is " + firstnum);
System.out.println("Last number is " + lastnum);
count = lastnum - firstnum + 1;
System.out.println("Total numbers are " + count);
if (count % 2 == 0) {
System.out.println("Total even numbers are " + count / 2);
System.out.println("Total odd numbers are " + count / 2);
} else {
if (firstnum % 2 == 0) {
System.out.println("Total Even numbers are ");
System.out.println((count + 1) / 2);
System.out.println("Total Odd numbers are ");
System.out.println((count - 1) / 2);
} else {
System.out.println("Total even numbers are ");
System.out.println((count - 1) / 2);
System.out.println("Total odd numbers are ");
System.out.println((count + 1) / 2);
}
}
}
}