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();

それを削除すると、true の場合に無限ループが発生するはずrepeat == 'Y'です。

同じことが他のループにも当てはまります。ループオーバーするコードを必ず中かっこで囲んでください。

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


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

ループを使用してゲームをもう一度プレイしたい場合は、ループを使用することをお勧めしdo/whileます。少なくとも 1 回はプレイしたいからです。

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ループに入れる

repeatwhile ループの前にブール値として定義- 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 に答える