java的新手,while循环?

时间:2015-07-14 00:41:36

标签: java loops while-loop

我这里有一个代码,应该将Celsius转换为Fahrenheit,反之亦然,然后询问用户是否要再次运行程序。但是,有一个边界,该用户只能输入范围从-100到100的数字。如果用户输入的数字大于或小于范围,则必须要求用户输入新数字。我在程序结束时遇到问题,并要求用户在输入正确的号码后重新运行该程序。有没有人有什么建议?谢谢!

import javax.swing.JOptionPane;
public class Temp3 {

        public static void main(String[] args) {
            // String sentence = "This is a Java program for converting
            // temperatures.";

            float option = JOptionPane.YES_OPTION;

            while (option == JOptionPane.YES_OPTION) {
                option = JOptionPane.showConfirmDialog(null, "Would you like to convert from Celsius to Fahrenheit?");

                if (option == JOptionPane.YES_OPTION) {
                    c2f();
                }

                else if (option == JOptionPane.NO_OPTION) {
                    f2c();
                }

                else {
                    JOptionPane.showMessageDialog(null, "Goodbye");
                    System.exit(0);
                }

                option = JOptionPane.showConfirmDialog(null, "Would you like to run the program again?");
                }// while loop

        if (option == JOptionPane.NO_OPTION) {
            JOptionPane.showMessageDialog(null, "Goodbye");
        } // main method
    }
        private static void c2f() {
            String celVal = JOptionPane.showInputDialog("Please enter degrees in Celsius. ");
            float cel = Float.parseFloat(celVal);

            while (cel <= 100 && cel >= -100) {
                double holder = ((cel * 1.8) + 32);
                JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + holder);
                break;
            }

            while (cel > 100) {

                String celVal2 = JOptionPane.showInputDialog("That number is too high. Please enter a number lower than 100 and try again.");
                float cel2 = Float.parseFloat(celVal2);

            while (cel2 <= 100 && cel2 >= -100){
                JOptionPane.showMessageDialog(null, "The temperature in Fahrenheit is " + ((cel2 * 1.8) + 32));
                cel2 = 101;

                }

            } 
            }



        // cel to fah

        private static void f2c() {
            String fahVal = JOptionPane.showInputDialog(
                    "You are now converting from Fahrenheit to Celsius. Please enter degrees in Fahrenheit.");
            float fah = Float.parseFloat(fahVal);
            while (fah < 100 && fah >-100){
                JOptionPane.showMessageDialog(null, "The temperature in Celcius is " + ((fah - 32) / 1.8));
                break;
            }
            if (fah > 100) {
                JOptionPane.showMessageDialog(null,
                        "That number is too high. Please enter a temperature less than 100 and try again.");
            }

            else if (fah < -100) {
                JOptionPane.showMessageDialog(null,
                        "That number is too low. Please enter a temperature more than -100 and try again.");


            }


        } // fah to cel

2 个答案:

答案 0 :(得分:1)

对于你的转换功能,我建议说while(enteredNum is out of range)而不是在数字在范围内时说,因为它并没有真正告诉你任何事情。有点事,一直在问。然后,您可以轻松地进行转换并返回

答案 1 :(得分:1)

这是一个很好的解决方案:

if(id=>701 NOT EXIST IN $ret)
{
    $ret[]=$thv;
}

基本上我使用 showOptionDialog 而不是 showConfirmDialog ,这样我就可以编辑按钮的标题。我们需要运行应用程序至少一次,因此执行while循环是完美的。