0

我正在编写应该创建无限循环的代码,但当前意外终止。当我输入“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!");
    }   
}
4

3 回答 3

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!");*
于 2013-08-09T21:11:15.387 回答
1

将整个代码封装在一个while循环中

repeat在 while 循环之前定义为布尔值 - 将其设置为 True

然后替换

while (repeat == 'Y') {
      System.out.println();
}

while (repeat == 'N') {
      System.out.println();
    System.out.println("Goodbye!");

} 

使用if检查用户输入是否为“n”的语句,在这种情况下更改repeat为 False。否则保持repeat为True。

此外 - 代码中有很多语法错误 - 清理它们,你应该没问题

于 2013-08-09T21:11:56.403 回答
1

把你想要重复的所有代码放在它自己的函数中:

void myMathGame() {
    //code for one round: setup, get user input, calculate values, print them
}

然后只需在主函数内部构造一个无条件无限循环,如果用户不想要另一轮,则将其中断:

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;
        }
    }
}
于 2013-08-09T21:41:04.070 回答