如何在 Java 中计算多位数中的奇数、偶数和零的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19129509/
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
How can I count the number of odd, evens, and zeros in a multiple digit number in Java?
提问by user2837136
Basically I need to write a program that takes user input up to and including 2^31 -1
in the form of an integer and returns the amount of odd, even, and zero numbers in the int. For example,
基本上,我需要编写一个程序,该程序以2^31 -1
整数形式接受用户输入,并返回整数中奇数、偶数和零数的数量。例如,
Input: 100
Output: 1 Odd, 0 Even, 2 Zeros // 1(Odd)0(Zero)0(Zero)
or
或者
Input: 2034
Output: 1 Odd, 2 Even, 1 Zero // 2(Even)0(Zero)3(Odd)4(Even)
I'm pretty sure I'm over thinking it but I can't slow my brain down. Can anyone help? This is the third iteration of the code, the first two were attempted with for loops.
我很确定我想多了,但我不能放慢我的大脑。任何人都可以帮忙吗?这是代码的第三次迭代,前两次尝试使用 for 循环。
import java.util.Scanner;
public class oddEvenZero
{
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
int value;
int evenCount = 0, oddCount = 0, zeroCount = 0;
System.out.print("Enter an integer: ");
value = scan.nextInt();
while (value > 0) {
value = value % 10;
if (value==0)
{
zeroCount++;
}
else if (value%2==0)
{
evenCount++;
}
else
{
oddCount++;
}
value = value / 10;
}
System.out.println();
System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
}
}
Sorry, the code formatted weirdly in the textbox.
抱歉,文本框中的代码格式很奇怪。
采纳答案by nhgrif
value = value % 10;
Probably the end-all-be-all of your problems.
可能是您所有问题的最终结果。
If value
is 2034
, then value % 10
returns 4
... and then assigns that value to value
, you go through your if else
block, then do 4/10
get 0
, and exit the while loop
without addressing the other 3 digits.
如果value
是2034
,则value % 10
返回4
... 然后将该值分配给value
,您通过您的if else
块,然后执行4/10
get 0
,然后退出while loop
而不处理其他 3 位数字。
I suggest something more like this:
我建议更像这样的:
while (value > 0) {
if ((value%10)==0) {
zeroCount++;
}
else if (value%2==0) { //As per comment below...
evenCount++;
}
else {
oddCount++;
}
value /= 10;
}
Or, int thisDigit = value % 10
, then replace value
in your current if else
block with thisDigit
.
或者,int thisDigit = value % 10
然后用 替换value
当前if else
块thisDigit
。
回答by TheKojuEffect
value = value % 10;
This statement will override your original value
with a reminder i.e value % 10
.
此声明将覆盖您的原始value
提示,即value % 10
。
If value = 2034
and value % 10 = 4
, then value = 4
which isn't what you want.
如果value = 2034
和 value % 10 = 4
,那么value = 4
这不是你想要的。
Instead use a temporary variable
而是使用临时变量
int lastDigit = value % 10;
Then your code becomes;
那么你的代码就变成了;
while (value > 0) {
int lastDigit = value % 10;
if (lastDigit==0)
{
zeroCount++;
}
else if (lastDigit%2==0)
{
evenCount++;
}
else
{
oddCount++;
}
value = value / 10;
}
回答by user2833496
import java.util.Scanner;
导入 java.util.Scanner;
public class oddEvenZero {
公共类oddEvenZero {
public int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
if (sarray != null)
{
int k= sarray.length-1;
int intarray[] = new int[k];
for (int i = 1; i < sarray.length; i++) {
intarray[i-1] = Integer.parseInt(sarray[i]);
}
return intarray;
}
return null;
}
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String value;
System.out.print("Enter an integer: ");
value = scan.next();
String words[] = value.split("");
oddEvenZero obj = new oddEvenZero();
try{
int intarray[]= obj.convertStringArraytoIntArray(words);
int even_number =0;
int odd_number =0;
int zero_number =0;
for (int h: intarray)
{
if(h==0)
{
zero_number++;
}
else if(h%2==0)
{
even_number++;
}
else{
odd_number++;
}
}
System.out.println("even numbers are"+ even_number);
System.out.println("odd numbers are"+odd_number);
System.out.println("Zero numbers are"+zero_number);
}
catch(Exception ex)
{
System.out.println("Please enter number");
}
}
}
}
回答by TheFiveHundredYears
If some of you are still unable to figure this code out, I found this while searching around for a bit, and works just fine:
如果你们中的一些人仍然无法弄清楚这段代码,我在四处搜索时发现了这个,并且工作得很好:
import java.util.*;
public class Java_1
{
public static void main (String[] args)
{
String string;
int zero = 0, odd = 0, even = 0, length, left = 0;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter any positive number: ");
string = scan.next();
length = string.length();
while (left < length)
{
string.charAt(left);
if (string.charAt(left) == 0)
zero++;
else if (string.charAt(left) % 2 == 0)
even++;
else
odd++;
left++;
}
System.out.println ("There are: "+ zero + " zeros.");
System.out.println ("There are: "+ even + " even numbers.");
System.out.println ("There are: "+ odd + " odd numbers.");
}
}