標籤:out 判斷 產生 dom scan erro ann 構建 count
方法1.
public static void main(String[] args){
System.out.println("將開始10次加法測試");
Scanner scanner=new Scanner(System.in);
int count=0;
for(int i=1;i<=10;i++){
int a=(int) (Math.random()*1000)+1;
int b=(int) (Math.random()*1000)+1;
System.out.println("("+i+")."+a+"+"+b+"=?");
System.out.println("請輸入答案輸入-1退出:");
int resullt=scanner.nextInt();
if(resullt==-1){
return;
}
if((a+b)==resullt){
count++;
System.out.println("正確");
}else{
System.out.println("錯誤");
}
}
System.out.println("此測試結束您的得分為"+count*10);
方法二.
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("將開始10次加法測試...");
Scanner scanner=new Scanner(System.in);
//用於記載分數
int score=0;
//構建10次迴圈
for(int i=1;i<=10;i++){
//隨機產生兩個加數
int a=(int) (Math.random()*100);
int b=(int) (Math.random()*100);
int result=a+b;
//輸出需要計算的加法運算式
System.out.println("("+i+")."+a+"+"+b+"=?");
System.out.println("請輸入答案(輸入-1退出):");
int answer=scanner.nextInt();
//判斷對錯
if(answer == -1){
break;
}else if(answer != result){
System.out.println("Error!");
continue;
}else{
score+=10;
System.out.println("Correct!");
//continue;
}
}
scanner.close();
System.out.println("此次測驗結束,你的分數是:"+score);
思考:1.return直接返回下邊程式不再執行,break終止當前迴圈,下邊的程式依然會執行,所以第一程式存在bug。
2.continue的使用。
3.變數名的使用。
java隨機輸出10計算題