Java,Do-While循环问题的新手

Y. *_*hen 0 java loops boolean while-loop

我对Java比较新,我在运行这个do-while循环时遇到了一些麻烦.

问题1

编写一个程序,允许用户将度数从摄氏度到华氏度或华氏度转换为摄氏度.使用以下公式:Degrees_C = 5(Degrees_F - 32)/ 9 Degrees_F =(9(Degrees_C)/ 5)+ 32提示用户输入温度,C或c表示摄氏度或F或f表示华氏度.如果输入摄氏度,则将温度转换为华氏温度;如果输入华氏温度,则将温度转换为摄氏温度.以可读格式显示结果.如果输入C,c,F或f以外的任何内容,则打印错误消息并停止.

问题2

让我们继续问题1,但使用循环以便用户可以转换其他温度.如果用户在温度之后输入大写或小写的C或F以外的字母,则打印错误消息并要求用户重新输入有效选择.每次转换后,要求用户键入Q或q退出或按任何其他键重复循环并执行另一次转换

public class Lab_4 {

    public static void main(String arg[]) {
        Scanner input = new Scanner(System.in);
        double F; //Fahrenheit
        double C; //Celsius
        String method; 
        boolean keepGoing = true; 
        do {            
            System.out.println("Choose a method:  ");
            System.out.println("(F)  Fahrenheit to Celsius.  ");
            System.out.println("(C)  Celsius to Fahrenheit.  ");
            System.out.println("(Q)  Exit the loop.  ");
            method = input.nextLine();
            if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
                System.out.println("Method F");
                System.out.println("Enter the temperature in Fahrenheit:  ");
                F = input.nextDouble();
                C = 5 * (F - 32) / 9;
                System.out.println("Temperature in Celsius:  " + C); }
            if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
                System.out.println("Method C");
                System.out.println("Enter the temperature in Celsius:  ");
                C = input.nextDouble();
                F = (9 * C / 5) + 32;
                System.out.println("Temperature in Fahrenheit:  " + F); }
            if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
                keepGoing = false; 
                System.out.println("Exiting the loop!  "); }
            else {
                //if index 0 doesn't equal to C, F, Q, it's out of range. 
                System.out.println("Method is out of range!  ");
            }
        }while(keepGoing = true);
        input.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

循环将继续,直到我输入Q或q退出.因此我必须输入Q才能退出循环,但是在获得转换值后我得到了一条错误消息,它只是没有通过循环运行.该程序只是不通过while循环.

在此输入图像描述

试试2,仍然是错误

package Lab_4;

import java.util.Scanner;

    public class Lab_4 {

        public static void main(String arg[]) {
            Scanner input = new Scanner(System.in);
            double F; //Fahrenheit
            double C; //Celsius
            String method; 
            boolean keepGoing = true; 
            do {            
                System.out.println("Choose a method:  ");
                System.out.println("(F)  Fahrenheit to Celsius.  ");
                System.out.println("(C)  Celsius to Fahrenheit.  ");
                System.out.println("(Q)  Exit the loop.  ");
                method = input.nextLine();
                if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
                    System.out.println("Method F");
                    System.out.println("Enter the temperature in Fahrenheit:  ");
                    F = input.nextDouble();
                    C = 5 * (F - 32) / 9;
                    System.out.println("Temperature in Celsius:  " + C); }
                else if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
                    System.out.println("Method C");
                    System.out.println("Enter the temperature in Celsius:  ");
                    C = input.nextDouble();
                    F = (9 * C / 5) + 32;
                    System.out.println("Temperature in Fahrenheit:  " + F); }
                else if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
                    keepGoing = false; 
                    System.out.println("Exiting the loop!  "); }
                else {
                    //if index 0 doesn't equal to C, F, Q, it's out of range. 
                    System.out.println("Method is out of range!  ");
                }
            }while(keepGoing);
            input.close();
        }
    }
Run Code Online (Sandbox Code Playgroud)

lil*_*zek 5

你有一个错字:

}while(keepGoing = true);
Run Code Online (Sandbox Code Playgroud)

应该:

}while(keepGoing == true);
Run Code Online (Sandbox Code Playgroud)

要不就:

}while(keepGoing);
Run Code Online (Sandbox Code Playgroud)

说明

keepGoing = true,即使内部的状态下,不分配keepGoingtrue和(总是返回其值true在这种情况下).

另一方面,keepGoing == true要求在价值之间进行比较keepGoingtrue相等.