java随机输出10计算题
方法1.
public static void main(String[] args){
System.out.println("將開始10次加法測(cè)試");
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("請(qǐng)輸入答案輸入-1退出:");
int resullt=scanner.nextInt();
if(resullt==-1){
return;
}
if((a+b)==resullt){
count++;
System.out.println("正確");
}else{
System.out.println("錯(cuò)誤");
}
}
System.out.println("此測(cè)試結(jié)束您的得分為"+count*10);
方法二.
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("將開始10次加法測(cè)試...");
Scanner scanner=new Scanner(System.in);
//用于記載分?jǐn)?shù)
int score=0;
//構(gòu)建10次循環(huán)
for(int i=1;i<=10;i++){
//隨機(jī)生成兩個(gè)加數(shù)
int a=(int) (Math.random()*100);
int b=(int) (Math.random()*100);
int result=a+b;
//輸出需要計(jì)算的加法表達(dá)式
System.out.println("("+i+")."+a+"+"+b+"=?");
System.out.println("請(qǐng)輸入答案(輸入-1退出):");
int answer=scanner.nextInt();
//判斷對(duì)錯(cuò)
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("此次測(cè)驗(yàn)結(jié)束,你的分?jǐn)?shù)是:"+score);
思考:1.return直接返回下邊程序不再執(zhí)行,break終止當(dāng)前循環(huán),下邊的程序依然會(huì)執(zhí)行,所以第一程序存在bug。
? ? ? ? 2.continue的使用。
? ? ? ? 3.變量名的使用。
?
轉(zhuǎn)載于:https://www.cnblogs.com/karmapeng/p/6305903.html
總結(jié)
以上是生活随笔為你收集整理的java随机输出10计算题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: maile:教你程序员怎么发邮件
- 下一篇: Elasticsearch-5.1.2分