java随机输出10计算题
方法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.變量名的使用。
?
轉載于:https://www.cnblogs.com/karmapeng/p/6305903.html
總結
以上是生活随笔為你收集整理的java随机输出10计算题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maile:教你程序员怎么发邮件
- 下一篇: Elasticsearch-5.1.2分