Java-While循环不循环

时间:2019-03-12 02:52:49

标签: java loops do-while

程序在接收到来自用户的错误输入之前终止do while循环。测试时,所有元素均单独工作。与其他元素一起放置时,仅做时不起作用。 do while语句的执行方式似乎与其他人建议的方式相同,并且在单独运行时起作用。我在论坛上搜索并查看了类似的问题,觉得我在以其他方式执行此操作和arent取得相同的结果,所以我显然只是看不到正确的东西。当放置在此代码中时,它不起作用。我已经将它放置在各个地方进行测试,但它仍然不会循环播放。感谢您的任何提前帮助。

import java.util.Scanner;
import java.text.DecimalFormat;

public class Conversion {
    public static void main(String[] args) {

        // variables declared
        int startRange = 0, endRange = 0;
        double conv = 0;
        final double INCH = 2.54;
        String answer = "y";

        //initiallizing user input device

        Scanner scan = new Scanner(System.in);

        //output
        do {

            System.out.println("Please enter begining range number: ");
            startRange = scan.nextInt();

            System.out.println("Please enter ending range number: ");
            endRange = scan.nextInt();

            if (startRange < 1 || endRange < startRange + 6 || endRange >= startRange + 37) {
                System.out.print("Please enter a valid number range: ");
            } else {
                System.out.println();
                System.out.println("       Conversion Chart");
                System.out.println();
                System.out.println("    Inches      Centimeters");
                System.out.println("   ********    *************");
                System.out.println();

                for (conv = startRange; endRange >= startRange; startRange += 6) {
                    DecimalFormat fmt = new DecimalFormat("0.##");
                    conv = conv * INCH;
                    System.out.println("         " + startRange + "            " + fmt.format(conv) + "\n");
                }
            }

            System.out.println("Would you like to do another conversion (y/n)? ");
            answer = scan.nextLine();
        }while (answer.equalsIgnoreCase("y"));
    }
}

0 个答案:

没有答案