我有这个项目,我一直在努力。我正在尝试添加选项以在用户想要再次尝试的情况下再次运行项目。
如何循环回到代码的起点?
在哪里应该为它输入循环代码的正确位置?
public static void main(String[] args) {
//Input String data types.
String input, input1, input2, input3, input4, input5, input6, input7, input8, input9, input10;
//Arithmetic Operators and variables
int add = 0, sub = 0, mult = 0;
double div = 0, modulo = 0;
//Startline of the program
JOptionPane.showMessageDialog(parentComponent, "Welcome and lets play numbers", "MATHGAME!", 3);//Use "parentComponent" for define dialogs and icons may show up.
//Ask users to select Operators
input = JOptionPane.showInputDialog(parentComponent, "Choose an Operator to calculate" + "\n1. Addition" + "\n2. Subtraction" + "\n3. Multiplication"
+ "\n4. Division" + "\n5. Modulo", "OPERATORS", 3);
//parse selections to input
int select = Integer.parseInt(input);
//Use switch for the certain selected operation:
switch (select) {
//cast cases for the selected operations
case 1: //Informations will appear on each cases
JOptionPane.showMessageDialog(parentComponent, "You've selected Addition", "ADDITION", 2);
//prompt users how many calculations they want using loops
input1 = JOptionPane.showInputDialog(parentComponent, "How many Calculation do you want?", "ADDITION", 3);
int select1 = Integer.parseInt(input1);
//Declare another Variables to hold the calculations.
int cal = 1,
total = 0;
//use loops for calculations
while (cal <= select1) {
//prompt users to enter the numbers to calculate
input2 = JOptionPane.showInputDialog(parentComponent, "Enter Number to Calculate", "ADDITION", 3);
add = Integer.parseInt(input2);
//Increment calculations
cal++;
// function of the operator
total = (int) (add + total);
}
// the final answer for the calculation
JOptionPane.showMessageDialog(parentComponent, "Your Sum is: " + total, "ADDITION", 2);
break; //break to stop the calculation and let the user to select another operations
case 2:
JOptionPane.showMessageDialog(parentComponent, "You've selected Subtraction", "SUBTRACTION", 2);
input3 = JOptionPane.showInputDialog(parentComponent, "How many Calculation do you want?", "SUBTRACTION", 3);
int select2 = Integer.parseInt(input3);
int cal1 = 1,
total1 = 0;
while (cal1 <= select2) {
input4 = JOptionPane.showInputDialog(parentComponent, "Enter Number to Calculate", "SUBTRACTION", 3);
sub = Integer.parseInt(input4);
cal1++;
total1 = (int) (sub - total1);
}
int nom = total1 - sub;
JOptionPane.showMessageDialog(parentComponent, "Your Difference is: " + (nom), "SUBTRACTION", 2);
break;
case 3:
JOptionPane.showMessageDialog(parentComponent, "You've selected Multiplication", "MULTIPLICATION", 2);
input5 = JOptionPane.showInputDialog(parentComponent, "How many Calculation do you want?", "MULTIPLICATION", 3);
int select3 = Integer.parseInt(input5);
int cal2 = 1,
total2 = 0;
while (cal2 <= select3) {
input6 = JOptionPane.showInputDialog(parentComponent, "Enter Number to Calculate", "MULTIPLICATION", 3);
mult = Integer.parseInt(input6);
cal2++;
total2 = (int) (mult * mult);
}
JOptionPane.showMessageDialog(parentComponent, "Your Product is: " + (total2), "MULTIPLICATION", 2);
break;
case 4:
JOptionPane.showMessageDialog(parentComponent, "You've selected Division", "DIVISION", 2);
input7 = JOptionPane.showInputDialog(parentComponent, "How many Calculation do you want?", "DIVISION", 3);
int select4 = Integer.parseInt(input7);
int cal3 = 1,
total3 = 0;
while (cal3 <= select4) {
input8 = JOptionPane.showInputDialog(parentComponent, "Enter Number to Calculate", "DIVISION", 3);
div = Double.parseDouble(input8);
cal3++;
total3 = (int) (double) (div / div);
}
JOptionPane.showMessageDialog(parentComponent, "Your Quotient is: " + (total3), "DIVISION", 2);
break;
case 5:
JOptionPane.showMessageDialog(parentComponent, "You've selected Percentage", "PERCENTAGE", 2);
input9 = JOptionPane.showInputDialog(parentComponent, "How many Calculation do you want?", "PERCENTAGE", 3);
int select5 = Integer.parseInt(input9);
int cal4 = 1,
total4 = 0;
while (cal4 <= select5) {
input10 = JOptionPane.showInputDialog(parentComponent, "Enter Number to Calculate", "PERCENTAGE", 3);
modulo = Double.parseDouble(input10);
cal4++;
total4 = (int) (modulo + total4);
}
double num = total4 / select5;
JOptionPane.showMessageDialog(parentComponent, "Your Average is: " + (num), "PERCENTAGE", 2);
break;
default: // Default code to prompt error to users that inputs wrong input.
JOptionPane.showMessageDialog(parentComponent, "Input Out of RANGE!", "ERROR", 2);
break;
}
//Prompt users to do more calculations using loops
String again = JOptionPane.showInputDialog(parentComponent, "Do you want to calculate more?" + "\n1. Yes"
+ "\n2. No", "AGAIN?", 3);
int term = Integer.parseInt(again); //parse for selections
if (term == 1){
}
else {
JOptionPane.showMessageDialog(parentComponent,"ThankYou");
}
}
public static Component parentComponent;}
答案 0 :(得分:0)
你可以这样做的方法是声明一个boolean类型的变量,并在这个变量为true时循环你的游戏。 像这样:
JOptionPane.showMessageDialog(parentComponent, "Welcome and lets play numbers", "MATHGAME!", 3);
boolean keepPlaying = true;
while (keepPlaying){
//your game
if(term !=1 ){
JOptionPane.showMessageDialog(parentComponent, "ThankYou");
keepPlaying = false;
}
}