可以幫我修改一下這JAVA

2012-12-07 4:35 am
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.print("1)Addition\n2)Subtraction\n3)Multiplication\n\n9)Quit\n> ");
int chose=s.nextInt();
System.out.print("Number ofQuestions: ");
int num=s.nextInt();
question(chose, num, s);
}
public static void question(int chose,int num,Scanner s){
float correct=0;
for(int i=0;i<num;i++){
int result=0;
int a=(int)(Math.random()*48+2);
int b=(int)(Math.random()*48+2);
int c=(int)(Math.random()*100);
int d=(int)(Math.random()*100);
int e;
switch(chose){
case 1:
result=a+b;
System.out.print("Q"+(i+1)+":"+a+"+"+b+"=");
break;
case 2:
result=c-d;
if(c<d){
e=c;
c=d;
b=e;
}
System.out.print("Q"+(i+1)+":"+c+"-"+d+"=");
break;
case 3:
result=a*b;
System.out.print("Q"+(i+1)+":"+a+"*"+b+"=");
break;
default:
System.out.println("Wrong!");
break;}
int answer=s.nextInt();
if(result==answer){correct++;}
}
System.out.println("You’ve answered "+num+" questions\n"+(int)correct+" of them are correct.And your mark is "+(correct/num)*100.0+"%");
}
}

我想输入9後 输出BYEBYE字句
像這樣
9)Quit
>9
byebye!
應該怎改要用do?

和 我想加多一項 4)除數
第一個操作數應該是1到99之間。
第一個操作數不應該是一個奇數。
第一個操作數必須是由第二個操作數整除。
第二個操作數應該是在2和40之間。

回答 (1)

2012-12-11 1:34 am
✔ 最佳答案
請參考我的做法


import java.util.Scanner;


public class Demo2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int total = 0, correct = 0;
do {
System.out.print(
"1) Addition\n" +
"2) Subtraction\n" +
"3) Multiplication\n" +
"4) Division\n" +
"9) Quit\n" +
"> ");
int choose = s.nextInt();
if (choose == 9) {
break;
}


int a = (int)(Math.random() * 48 + 2),
b = (int)(Math.random() * 48 + 2), result = 0;
switch (choose) {
case 1:
result = a + b;
System.out.print("Q" + (total+1) + ": " + a + " + " + b + " = ");
break;
case 2:
a = (int)(Math.random() * 100);
b = (int)(Math.random() * (100-a)) + a;
result = b - a;
System.out.print("Q" + (total+1) + ": " + b + " - " + a + " = ");
break;
case 3:
result = a * b;
System.out.print("Q" + (total+1) + ": " + a + " * " + b + " = ");
break;
case 4:
a = (int)(Math.random() * 98) + 1;
if (a % 2 == 1) a++;
do {
b = (int)(Math.random() * 39) + 2;
} while (a % b != 0);
result = a / b;
System.out.print("Q" + (total+1) + ": " + a + " / " + b + " = ");
break;
}
int z = s.nextInt();
if (z == result) correct++;
total++;
} while (true);
System.out.println("You've answered " + total + " questions\n" +
correct + " of them are correct.\n" +
"And your mark is " + (correct * 100D / total) + "%");
System.out.println("byebye!");
}
}


收錄日期: 2021-04-21 14:10:42
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20121206000051KK00277

檢視 Wayback Machine 備份