循环 - 做,同时

时间:2014-02-11 01:27:16

标签: java loops do-while

我尝试了一个循环,但是“{”符号阻止了我想的所有打印行重复。我希望循环在用户完成输入序列的值后重新显示整个第一个字符串。

import java.util.Scanner;

public class FtoC{

    static Scanner cin = new Scanner (System.in);

    public static void main(String[] args)
    {
        char userInput = ' ';
        System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
                      + "\nEnter " + "\"C\"" + " or " +  "\"c\"" + " for Celsius to Fahrenheit"
                      + "\nEnter something else to quit." +"\n");


         userInput = cin.next().charAt(0);

        if ((userInput == 'F') || (userInput =='f'))
        {print("Enter a temp in Fahrenheit"
                +"\n I will tell you temp in Celsius" +"\n");
            far2cel();
        }
         else if ((userInput == 'C') || (userInput =='c')) {
            print("Enter a temp in Celsius:"
            +"\n I will tell you temp in fahrenheit" +"\n");
                cel2far();
        } else {
            print("Terminating the program" + "\n");
        }
    }

    private static void cel2far() {
        Scanner input = new Scanner(System.in);
        Double celsius = input.nextDouble();
        print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
                + " Fahrenheit");

    }

    private static void far2cel() {
        Scanner input = new Scanner(System.in);

        Double Fahrenheit = input.nextDouble();
        print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
                + " celsius");

    }

    private static void print(String string) {
        System.out.print("\n" + string);
    }
}

1 个答案:

答案 0 :(得分:0)

我不认为我得到你想要的东西,但是这样你就可以运行程序,直到用户在第一部分输入不同于“F”,“f”,“C”,“C”的东西。该计划。

import java.util.Scanner;

public class FtoC{

    static Scanner cin = new Scanner (System.in);

    public static void main(String[] args)
    {
        while(true) {
            char userInput = ' ';
            System.out.print("\nEnter " + "\"F\"" + " or " + "\"f\"" + " for Fahrenheit to Celsius"
                          + "\nEnter " + "\"C\"" + " or " +  "\"c\"" + " for Celsius to Fahrenheit"
                          + "\nEnter something else to quit." +"\n");

             userInput = cin.next().charAt(0);

            if ((userInput == 'F') || (userInput =='f'))
            {print("Enter a temp in Fahrenheit"
                    +"\n I will tell you temp in Celsius" +"\n");
                far2cel();
            }
             else if ((userInput == 'C') || (userInput =='c')) {
                print("Enter a temp in Celsius:"
                +"\n I will tell you temp in fahrenheit" +"\n");
                    cel2far();
            } else {
                print("Terminating the program" + "\n");
                break;
            }
        }
    }

    private static void cel2far() {
        Scanner input = new Scanner(System.in);
        Double celsius = input.nextDouble();
        print(celsius + " celsius is " + ((celsius * 9 / 5.0) + 32)
                + " Fahrenheit");

    }

    private static void far2cel() {
        Scanner input = new Scanner(System.in);

        Double Fahrenheit = input.nextDouble();
        print(Fahrenheit + " Fahrenheit is " + ((Fahrenheit - 32) * (5 / 9.0))
                + " celsius");

    }

    private static void print(String string) {
        System.out.print("\n" + string);
    }
}

我把它放入while循环并添加了“break;”在“终止程序”部分。