java - 编写无限循环

标签 java infinite-loop

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

最佳答案

while 循环后面有分号:

while (repeat == 'Y'); // <-- Right here
      System.out.println();

如果删除它,如果 repeat == 'Y' 为 true,则应该会出现无限循环。

另一个循环也是如此。只需确保在要循环的代码周围使用大括号即可:

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!");*

关于java - 编写无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18155665/

相关文章:

java - android 将字符串转换为日期格式 转换回字符串

java - 将数据解析成表

java - 使用蒙特卡罗方法找到最低价格的理想量子

javascript 停止无限循环

c++ - 为什么这会导致 Mac 上的无限循环?

java - 为什么我的 Spring 数据的 Page 对象的自定义 json 序列化器没有被调用

c# - 无限循环使 TimeManager 无效

c - 为什么这个程序会导致无限循环输出?

Reactjs router onEnter hook 认证导致无限循环

java - Eclipse 文件夹结构