[英]How to check for odd number of digits in Java?
我需要编写一个接受以下条件的程序:
我目前坚持尝试检查它是否具有奇数位数。 任何帮助,将不胜感激。 更新:谢谢大家的所有帮助。 真的很有帮助!
import java.util.Scanner;
public class TestOddNumbers {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int userInput;
final int EXIT = -1;
final int MIN = 101;
final int MAX = 1000001;
do{
System.out.println("please enter a positive whole number between"
+ "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
+ "when you are done entering numbers.");
userInput = stdin.nextInt();
}
while(userInput != EXIT && userInput >= MIN && userInput <= MAX);
if(userInput % 2 == 1){
System.out.println("This is an odd number");
}
else{
System.out.println("This is not an odd number");
}
}
}
}
这个怎么样 ?
此伪代码不是解决方案,也不是解决方案的尝试。
//this should probably be outside and before your while loop.
int length = Integer.toString(userInput).length();
if (length % 2 == 0) {
return false; //even number of digits
}
...
//this should be inside your while loop
while(userInput != 0) {
remainder = userInput % 10;
userInput = userInput / 10;
if (remainder % 2 != 0) {
return false; //One of the digit is even
}
//if every digit is odd, it has to be an odd number, so checking if origina userInput is an odd number is not necessary.
}
以该伪代码为起点。
尝试将userInput的值传递到字符串中,并检查其长度是否为奇数。
String total = "";
do{
System.out.println("please enter a positive whole number between"
+ "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
+ "when you are done entering numbers.");
userInput = stdin.nextInt();
total += String.valueOf(userInput);
}
while(userInput != EXIT && userInput >= MIN && userInput <= MAX);
System.out.println(total);
if(total.length() % 2 == 1){
System.out.println("This is an odd number");
}
else{
System.out.println("This is not an odd number");
}
这段代码将检查每个数字是否为奇数,以及长度是否也为奇数。
int test = 6789;
int a = test;
int length =0;
boolean hasEachDigitOdd = true;
while (a!= 0) {
int remaider = a % 10;
if (remaider % 2 == 0) {
hasEachDigitOdd = false;
}
a = a / 10;
length++;
}
System.out.println(length);
System.out.println(hasEachDigitOdd);
if (num >= 101 && num <= 1000001 && num % 2 == 0)
百分号为您提供除法运算的其余部分。
请在下面检查-满足您所有需求的解决方案。 甚至您输入的方式也需要固定。
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int userInput;
final int EXIT = -1;
final int MIN = 101;
final int MAX = 1000001;
do {
System.out.println("please enter a positive whole number between"
+ "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
+ "when you are done entering numbers.");
userInput = stdin.nextInt();
if(userInput==EXIT){
System.out.println("done");
break;
}
if (userInput % 2 == 1) {
System.out.println("This is an odd number");
} else {
System.out.println("This is not an odd number");
}
if(String.valueOf(userInput).length()%2==1){
System.out.println("Has odd number of digits");
} else {
System.out.println("Has even number of digits");
}
boolean[] allOdds = {true};
String.valueOf(userInput).chars().forEach(i -> {
if(Integer.parseInt(String.valueOf((char)i))%2==0){
allOdds[0] = false;
}
});
if(allOdds[0]){
System.out.println("all digits are odds");
}else{
System.out.println("all digits are not odds");
}
} while (userInput >= MIN && userInput <= MAX);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.