這個代碼,怎么實現(xiàn)重復(fù)輸入,條件一直報錯
import java.util.Scanner;
public class Do_while03 {
public static void main(String[] args) {
//鍵盤錄入
Scanner nc = new Scanner (System.in);
System.out.println("請輸入數(shù)字");
//x值不等于1或2時重新輸入數(shù)字
do{int x=nc.nextInt();//接收鍵盤錄入的數(shù)據(jù)
if (x==1) {System.out.println("A");
break;
}else if(x==2){System.out.println("B");
break;
}else {
System.out.println("請重新嘗試輸入____");
}while(ture)
}
}
}
2020-06-07
int i = 1;
Scanner nc = new Scanner(System.in);
System.out.print("請輸入數(shù)字:");
do {
int x = nc.nextInt();
i++;
if (x == 1) {
System.out.println("A");
} else if (x == 2) {
System.out.println("B");
} else {
System.out.print("請重新嘗試輸入:");
x = nc.nextInt();
}
} while (true);
// 只能重復(fù)輸入一次,之后就要重新運(yùn)行了
2020-05-06
干嘛用break呢,你都在跳出來了,還怎么進(jìn)去
2020-05-06
package day_1;
import java.util.Scanner;
public class aa {
public static void main(String[] args) {
int i = 1;
Scanner nc = new Scanner (System.in);
System.out.println("請輸入數(shù)字");
do{int x=nc.nextInt();
i++;
if (x==1) {System.out.println("A");
System.out.println(i);
}else if(x==2){
System.out.println("B");
System.out.println(i);
}else {
System.out.println("請重新嘗試輸入____");
System.out.println(i);
}
}while(i+1<=4);
}
}
2020-03-09
//這個是規(guī)范的寫法:
import java.util.Scanner;
public class Do_while03 {
public static void main(String[] args) {
// 鍵盤錄入
Scanner nc = new Scanner(System.in);
System.out.println("請輸入數(shù)字");
// x值不等于1或2時,實現(xiàn)重新接收鍵盤輸入的數(shù)字
do {
int x = nc.nextInt();// 接收鍵盤錄入的數(shù)據(jù)
if (x == 1) {
System.out.println("A");
break;
} else if (x == 2) {
System.out.println("B");
break;
} else {
System.out.println("請重新嘗試輸入____");
} // 此處的大括號容易被丟
} while (true);
}
}
2020-03-09
import java.util.Scanner;
public class Do_while03 {
public static void main(String[] args) {
//鍵盤錄入
Scanner nc = new Scanner (System.in);
System.out.println("請輸入數(shù)字");
//x值不等于1或2時,實現(xiàn)重新接收鍵盤輸入的數(shù)字
do{int x=nc.nextInt();//接收鍵盤錄入的數(shù)據(jù)
if (x==1) {System.out.println("A");
break;
}else if(x==2){System.out.println("B");
break;
}else {
System.out.println("請重新嘗試輸入____");
}//此處的大括號容易被丟
}while(true);//單詞錯誤
}
}