[英]Prompting user to re-enter integers until binary number is entered
I am new with JAVA (computer programming by the way).我是 JAVA 新手(顺便说一下,计算机编程)。 The following program checks if the input is binary or not.
以下程序检查输入是否为二进制。 And it should prompt user to re-enter integers until binary number is entered.
它应该提示用户重新输入整数,直到输入二进制数。 But this program is doing exactly opposite.
但是这个程序做的恰恰相反。 It is asking me to re-enter integers if input is binary, and the program terminates when non-binary is entered.
如果输入是二进制,它要求我重新输入整数,并且程序在输入非二进制时终止。 I need a serious help, please.
我需要一个认真的帮助,请。 Here is my output
这是我的输出
public static void main(String[] args) {
int value, userValue;
int binaryDigit = 0, notBinaryDigit = 0;
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
break;
} else {
System.out.println(value + " is a Binary Number.");
}
}
}
It might be too late for the answer.答案可能为时已晚。 But the code can be simplified much if you make use of the method.
但是如果使用该方法,代码可以大大简化。
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Please enter positive integers: ");
int userValue = scan.nextInt();
if (isBinary(userValue)) {
System.out.println(userValue + " is a Binary Number.");
break;
} else {
System.out.println(userValue + " is a not Binary Number.");
}
}
}
public static boolean isBinary(int input) {
while (input > 0) {
if ((input % 10 != 0) && (input % 10 != 1)) {
return false;
}
input = input / 10;
}
return true;
}
}
Change your code更改您的代码
from this由此
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
break;
} else {
System.out.println(value + " is a Binary Number.");
}
to this对此
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
notBinaryDigit--;
} if(binaryDigit >0 {
System.out.println(value + " is a Binary Number.");
binaryDigit--;
break;
}
and it will ask for values and tell if it is binary or not and it terminates if it is binary它会询问值并判断它是否是二进制的,如果它是二进制的,它会终止
Error coming due to Scanner class.由于 Scanner 类而出现错误。 you can run your program and check your logic by removing the Scanner class like this.
您可以通过像这样删除 Scanner 类来运行您的程序并检查您的逻辑。
public class Test{公共课测试{
public static void main(String []args){
int value, userValue=34;
int binaryDigit = 0, notBinaryDigit = 0;
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
} else {
System.out.println(value + " is a Binary Number.");
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.