Java 具有奇偶棋盘格的正负棋盘格
我如何检查输入的数字是负数还是正数,如果是正数,它将再次检查它是奇数还是偶数,如果是负数,将显示错误消息并再次询问用户是否要检查其他数字Java 具有奇偶棋盘格的正负棋盘格,java,Java,我如何检查输入的数字是负数还是正数,如果是正数,它将再次检查它是奇数还是偶数,如果是负数,将显示错误消息并再次询问用户是否要检查其他数字 import java.util.Scanner; public class OddEvenChecker{ public static void main(String[] args){ int integer=0; Scanner sc = new Scanner(System.in); whi
import java.util.Scanner;
public class OddEvenChecker{
public static void main(String[] args){
int integer=0;
Scanner sc = new Scanner(System.in);
while (true) {
if (integer>=0) {
System.out.print("Enter a positive Integer: ");//gets the user input
integer = sc.nextInt();
}
if (integer<0) { // checks if the entered number is a positive integer
System.out.println("Error");
System.out.print("Do you Want to Continue? y for Yes, n for No: ");
String choices = sc.nextLine();
} else if(integer>0) {
if (integer%2==0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
}
}
}
import java.util.Scanner;
公共类检查程序{
公共静态void main(字符串[]args){
整数=0;
扫描仪sc=新的扫描仪(System.in);
while(true){
如果(整数>=0){
System.out.print(“输入正整数:”;//获取用户输入
整数=sc.nextInt();
}
if(整数0){
如果(整数%2==0){
System.out.println(“偶数”);
}否则{
系统输出打印项次(“奇数”);
}
}
}
}
}
公共类检查程序{
public static void main(String[] args){
int integer=0;
Scanner sc = new Scanner(System.in);
while (true) {
if (integer>=0) {
System.out.print("Enter a positive Integer: ");
integer = sc.nextInt();
if(integer%2==0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}
}
if (integer<0) {
System.out.println("Error");
System.out.print("Do you Want to Continue? y for Yes, n for No: ");
String choices = sc.nextLine();
}
}
}
publicstaticvoidmain(字符串[]args){
整数=0;
扫描仪sc=新的扫描仪(System.in);
while(true){
如果(整数>=0){
System.out.print(“输入正整数:”);
整数=sc.nextInt();
如果(整数%2==0){
System.out.println(“偶数”);
}
否则{
系统输出打印项次(“奇数”);
}
}
if(整数
您对这种方法有何看法?它是否满足要求?提供的代码有什么问题?如果(整数>=0){
可以更改为,而(整数)问题“是否要继续打印2次,并且我无法调整输入”如果用户输入的n不是“偶数”或“奇数”,我无法结束语句如果用户输入负数,则不应输出,否则不会输出。如果您愿意,请运行它。是的,但如果用户输入负数,则会提示您是否要继续,但我已经修复了它,非常感谢
public static void main(String[] args){
int integer=0;
Scanner sc = new Scanner(System.in);
while (true) {
if (integer>=0) {
System.out.print("Enter a positive Integer: ");
integer = sc.nextInt();
if(integer%2==0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}
}
if (integer<0) {
System.out.println("Error");
System.out.print("Do you Want to Continue? y for Yes, n for No: ");
String choices = sc.nextLine();
}
}
}
do{
choices = sc.nextLine();
if(choices.equals("n") || choices.equals("no"){
return;
}
else if(choices.equals("y") || choices.equals("yes"){
break;
} else {
System.out.println("invalid key");
}
} while(!choices.equals("n") || (!choices.equals("no") || (!choices.equals("y") || (!choices.equals("yes"));
public class OddEvenChecker {
public static void main(String[] args) {
int integer = 0;
String flag = "y";
Scanner sc = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);
do {
System.out.print("Enter a positive Integer: ");//gets the user input
integer = intScanner.nextInt();
if(integer>0) {
if(integer%2==0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
} else {
System.out.println("This is negative number. Try again.");
}
System.out.print("Do you want to Continue? y for Yes, n for No: ");
flag = sc.nextLine();
} while(!(flag.equals("n") || flag.equals("no")));
}
}