-5
import java.io.*;
public class AssignmentMT {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int number1, number2, number3, number4, number5;
double answer;
double geanswer = 0;
String word;
String operator = "";
int score=0, totalscore=10;
float percentage;
int intoperator;
word = br.readLine();
//Game Enhancement.
while (word.equals("Game Enhancement")){
System.out.println("Now you are in the game enhancement, be ready for your 4 level qustions. Write your answer after press enter.");
System.out.println("Press 'Go' to start!");
word = br.readLine();
while (!word.equals("Go")){
System .out.println("Please enter again.");
word = br.readLine();
}
while (word.equals("Go")){
System.out.println("-Level 1-");
number1 = (int) (Math.round (Math.random() * 9)+1);
number2 = (int) (Math.round (Math.random() * 9)+1);
intoperator = (int) (Math.round (Math.random() * 4)+1);
switch (intoperator) {
case 1:
operator = "+";
geanswer = number1+number2;
break;
case 2:
operator = "-";
geanswer = number1-number2;
break;
case 3:
operator = "×";
geanswer = number1*number2;
break;
case 4:
operator = "÷";
geanswer = (double)Math.round(((double)number1/(double)number2)*100)/100;
break;
default:
operator = "+";
geanswer = number1+number2;
break;
}
System.out.println(number1 + operator + number2 + "=");
answer = Double.parseDouble(br.readLine());
if(answer==geanswer){
score++;
}
System.out.println("Your Score: " + score);
System.out.println("-Level 2-");
for (int i=1;i<=2;i++){
number1 = (int) (Math.round (Math.random() * 9)+1);
number2 = (int) (Math.round (Math.random() * 9)+1);
number3 = (int) (Math.round (Math.random() * 9)+1);
intoperator = (int) (Math.round (Math.random() * 4)+1);
switch (intoperator) {
case 1:
operator = "+";
geanswer = number1+number2+number3;
break;
case 2:
operator = "-";
geanswer = number1-number2-number3;
break;
case 3:
operator = "×";
geanswer = number1*number2*number3;
break;
case 4:
operator = "÷";
geanswer = (double)Math.round(((double)number1/(double)number2/(double)number3)*100)/100;
break;
default:
operator = "+";
geanswer = number1+number2+number3;
break;
}
System.out.println(number1 + operator + number2 + operator + number3 + "=");
answer = Double.parseDouble(br.readLine());
if(answer == geanswer){
score++;
}
}
System.out.println("Your Score: " + score);
System.out.println("-Level 3-");
for (int i=1;i<=3;i++){
number1 = (int) (Math.round (Math.random() * 9)+1);
number2 = (int) (Math.round (Math.random() * 9)+1);
number3 = (int) (Math.round (Math.random() * 9)+1);
number4 = (int) (Math.round (Math.random() * 9)+1);
intoperator = (int) (Math.round (Math.random() * 4)+1);
switch (intoperator) {
case 1:
operator = "+";
geanswer = number1+number2+number3+number4;
break;
case 2:
operator = "-";
geanswer = number1-number2-number3-number4;
break;
case 3:
operator = "×";
geanswer = number1*number2*number3*number4;
break;
case 4:
operator = "÷";
geanswer = (double)Math.round(((double)number1/(double)number2/(double)number3/(double)number4)*100)/100;
break;
default:
operator = "+";
geanswer = number1+number2+number3+number4;
break;
}
System.out.println(number1 + operator + number2 + operator + number3 + operator + number4 + "=");
answer = Double.parseDouble(br.readLine());
if(answer == geanswer){
score++;
}
}
System.out.println("Your Score: " + score);
System.out.println("-Level 4-");
for (int i=1;i<=4;i++){
number1 = (int) (Math.round (Math.random() * 9)+1);
number2 = (int) (Math.round (Math.random() * 9)+1);
number3 = (int) (Math.round (Math.random() * 9)+1);
number4 = (int) (Math.round (Math.random() * 9)+1);
number5 = (int) (Math.round (Math.random() * 9)+1);
intoperator = (int) (Math.round (Math.random() * 4)+1);
switch (intoperator) {
case 1:
operator = "+";
geanswer = number1+number2+number3+number4+number5;
break;
case 2:
operator = "-";
geanswer = number1-number2-number3-number4-number5;
break;
case 3:
operator = "×";
geanswer = number1*number2*number3*number4*number5;
break;
case 4:
operator = "÷";
geanswer = (double)Math.round(((double)number1/(double)number2/(double)number3/(double)number4/(double)number5)*100)/100;
break;
default:
operator = "+";
geanswer = number1+number2+number3+number4+number5;
break;
}
System.out.println(number1 + operator + number2 + operator + number3 + operator + number4 + operator + number5 + "=");
answer = Double.parseDouble(br.readLine());
if(answer == geanswer){
score++;
}
}
percentage = (float)((score*100)/totalscore);
System.out.println("Your total Score: " + score);
System.out.println(" ");
System.out.println("Your got " + percentage + "% mark! Congratulation!");
System.out.println("Please enter Quit to end.");
word = br.readLine();
if (word.equals("Quit")){
System.out.println(" ");
System.out.println("That's the end of the contest. Thank you!");
}
}
}
}
}
嗨,大家好,我是一名高中生。現在我在學校有11年級的計算機科學。 問題是,如你所見,老師要求參加數學競賽程序。他說我們需要至少做10個關卡。 1級有一個隨機運算符。 2級有兩個隨機運算符 ...等等。 此外,每次參與者回答正確時,他/她將得到一個分數,這意味着我必須寫點東西來檢查正確的答案。我知道我需要使用for循環來完成這十個級別,事實是我已經嘗試過但我不能.................我要瘋了。如何在java中隨機選擇多個數學題目並檢查正確答案
Plz help me ..... Thanks for read it !!!
請閱讀[問]並製作[mcve]。這是不可讀的... – Tunaki
你需要做的第一件事是*學習如何格式化你的代碼*。你有什麼坦白地說只是可怕的。 –
@Shirley這是一個非常基本的問題(如果你把它分解下來,這個問題很簡單)確保你可以先生成1個測試問題。完成後,用for循環將其括起來。 – user3437460