我正在编写应该创建无限循环的代码,但目前正在意外终止。 当我输入“Y”表示“是”或“N”表示“否”时,此代码应输入非终止循环。
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class Math_Island_XTENDED_Final
{
static Scanner sc = new Scanner(System.in);
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Declarations
int Bignumber;
int Mednumber;
int Smallnumber;
double addition;
double subtraction;
double multiplcation;
double division;
double sphere_radius1;
double sphere_radius2;
double sphere_radius3;
double rectangle_measurements;
char repeat;
String input;
System.out.println("Welcome to Math Island :D ! ");
System.out.println("We will use some numbers for our Formula ! ");
System.out.println("1 rule, NO DECIMALS !! ");
System.out.println("Please Pick a # from 1 to 100 NOW!! ");
Bignumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 20 NOW!! ");
Mednumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 5 NOW!! ");
Smallnumber = sc.nextInt();
//Results
addition = Bignumber + Mednumber + Smallnumber;
subtraction = Bignumber - Mednumber - Smallnumber;
multiplcation = Bignumber * Mednumber * Smallnumber;
division = Bignumber / Mednumber / Smallnumber;
sphere_radius1 = Bignumber * 3.14 * 3.14;
sphere_radius2 = Mednumber * 3.14 * 3.14;
sphere_radius3 = Smallnumber * 3.14 * 3.14;
rectangle_measurements = Bignumber * Mednumber * Smallnumber;
System.out.println();
System.out.println();
//results 2
System.out.println(" Your addition answer would be " + addition);
System.out.println(" Your subtraction answer would be " + subtraction);
System.out.println(" Your multiplcation answer would be " + multiplcation);
System.out.println(" Your division answer would be " + division);
System.out.println(" Your first sphere answer would be " + sphere_radius1);
System.out.println(" Your second sphere answer would be " + sphere_radius2);
System.out.println(" Your third sphere answer would be " + sphere_radius3);
System.out.println(" Your recangle size would be " + rectangle_measurements+ " in cubic Feet");
System.out.println();
System.out.println();
System.out.println("Would you like to Play again ? ");
System.out.println("Y for yes, & N for no " );
input = keyboard.nextLine();
repeat = input.charAt(0);
while (repeat == 'Y');
System.out.println();
while (repeat == 'N');
System.out.println();
System.out.println("Goodbye!");
}
}
答案 0 :(得分:3)
你的while循环后你有分号:
while (repeat == 'Y'); // <-- Right here
System.out.println();
如果删除它,如果repeat == 'Y'
为真,则应该获得无限循环。
另一个循环也是如此。只需确保在要循环的代码周围使用大括号:
while (repeat == 'Y')
System.out.println();
while (repeat == 'N') {
System.out.println();
System.out.println("Goodbye!");
}
如果您想使用循环再次玩游戏,我建议使用do/while
循环,因为您想至少玩一次:
do {
System.out.println("Welcome to Math Island :D ! ");
System.out.println("We will use some numbers for our Formula ! ");
System.out.println("1 rule, NO DECIMALS !! ");
System.out.println("Please Pick a # from 1 to 100 NOW!! ");
Bignumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 20 NOW!! ");
Mednumber = sc.nextInt();
System.out.print("Please Pick a # from 1 to 5 NOW!! ");
Smallnumber = sc.nextInt();
//Results
addition = Bignumber + Mednumber + Smallnumber;
subtraction = Bignumber - Mednumber - Smallnumber;
multiplcation = Bignumber * Mednumber * Smallnumber;
division = Bignumber / Mednumber / Smallnumber;
sphere_radius1 = Bignumber * 3.14 * 3.14;
sphere_radius2 = Mednumber * 3.14 * 3.14;
sphere_radius3 = Smallnumber * 3.14 * 3.14;
rectangle_measurements = Bignumber * Mednumber * Smallnumber;
System.out.println();
System.out.println();
//results 2
System.out.println(" Your addition answer would be " + addition);
System.out.println(" Your subtraction answer would be " + subtraction);
System.out.println(" Your multiplcation answer would be " + multiplcation);
System.out.println(" Your division answer would be " + division);
System.out.println(" Your first sphere answer would be " + sphere_radius1);
System.out.println(" Your second sphere answer would be " + sphere_radius2);
System.out.println(" Your third sphere answer would be " + sphere_radius3);
System.out.println(" Your recangle size would be " + rectangle_measurements+ " in cubic Feet");
System.out.println();
System.out.println();
System.out.println("Would you like to Play again ? ");
System.out.println("Y for yes, & N for no " );
input = keyboard.nextLine();
repeat = input.charAt(0);
} while (repeat == 'y');
System.out.println("Goodbye!");*
答案 1 :(得分:1)
将整个代码包含在while
循环
在while循环之前将repeat
定义为布尔值 - 将其设置为True
然后替换
while (repeat == 'Y') {
System.out.println();
}
while (repeat == 'N') {
System.out.println();
System.out.println("Goodbye!");
}
使用if
语句检查用户输入是否为“n”,在这种情况下将repeat
更改为False。否则,请将repeat
保留为True。
此外 - 代码中有很多语法错误 - 清理它们你应该没问题
答案 2 :(得分:1)
获取您想要重复的所有代码并将其放入其自己的函数中:
void myMathGame() {
//code for one round: setup, get user input, calculate values, print them
}
然后简单地在main函数内构造一个无条件的无限循环,如果用户不想要另一轮,你就会中断:
public static void main(String[] args) {
while (true) {
myMathGame();
System.out.println("Would you like to Play again? (Y/N) ");
if (keyboard.nextLine().charAt(0) != "Y") {
break;
}
}
}