如何在java

时间:2015-05-31 23:17:01

标签: java

我知道这是一个简单的Java概念,但我现在正在学习如何编写代码。我想知道是否有人可以帮我写一份声明,以便在打印转换后打印另一份声明,说明"键入'重做'去程序的开头。"然后,这将允许他们做出另一个选择。这是我的代码:

package convertorPackage;

import java.util.Scanner;

public class SimpleConvertor {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double length = 0.39370;

        System.out.println("Welcome to simple convertor.");
        System.out.println("Pick an option and its corresponding letter to select.");
        System.out.println("Farenheight to Celsius: f");
        System.out.println("Celsius to Farenheight: c");
        System.out.println("Inches to Centimeters: i");
        System.out.println("Centimeters to Inches: ce");
        System.out.println("");
        System.out.println("Make your choice: ");
        String choice = input.nextLine();

        if ( choice.equals("f") ) {

            float farenheight;     

            System.out.println("Enter temperatue in Fahrenheit: ");
            farenheight = input.nextInt();

            farenheight = ((farenheight - 32)*5)/9;

            System.out.println("Temperatue in Celsius = " + farenheight);

        } else if ( choice.equals("c") ) {

            float celsius;     

            System.out.println("Enter temperatue in Celsius: ");
            celsius = input.nextInt();

            celsius = ((celsius)*18/10)+32;

            System.out.println("Temperatue in Farenheight = " + celsius);

        } else if ( choice.equals("i") ) {

            double inches;     

            System.out.println("Enter length in Inches: ");
            inches = input.nextInt();

            inches = (inches/length);

            System.out.println("Length in Centimeters = " + inches);
        } else if ( choice.equals("ce") ) {

            double centimeters;     

            System.out.println("Enter length in Centimeters: ");
            centimeters = input.nextInt();

            centimeters = (centimeters*length);

            System.out.println("Length in Inches is = " + length);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

package convertorPackage;

import java.util.Scanner;

public class SimpleConvertor {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        while (true) {
            //Conversion stuff here
            String response = input.nextLine();
            if (!response.equals("redo")) {
                break;
            }
        }

    }
}

答案 1 :(得分:1)

将需要循环的代码换行一个while循环。

public class SimpleConvertor {

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);

        double length = 0.39370;

        System.out.println("Welcome to simple convertor.");

        boolean cont = true;
        while (cont) {
            System.out.println("Pick an option and its corresponding letter to select.");
            System.out.println("Farenheight to Celsius: f");
            System.out.println("Celsius to Farenheight: c");
            System.out.println("Inches to Centimeters: i");
            System.out.println("Centimeters to Inches: ce");
            System.out.println("");
            System.out.println("Make your choice: ");
            String choice = input.nextLine();

            if ( choice.equals("f") ) {

                float farenheight;

                System.out.println("Enter temperatue in Fahrenheit: ");
                farenheight = input.nextInt();

                farenheight = ((farenheight - 32)*5)/9;

                System.out.println("Temperatue in Celsius = " + farenheight);

            } else if ( choice.equals("c") ) {

                float celsius;

                System.out.println("Enter temperatue in Celsius: ");
                celsius = input.nextInt();

                celsius = ((celsius)*18/10)+32;

                System.out.println("Temperatue in Farenheight = " + celsius);

            } else if ( choice.equals("i") ) {

                double inches;

                System.out.println("Enter length in Inches: ");
                inches = input.nextInt();

                inches = (inches/length);

                System.out.println("Length in Centimeters = " + inches);
            } else if ( choice.equals("ce") ) {

                double centimeters;

                System.out.println("Enter length in Centimeters: ");
                centimeters = input.nextInt();

                centimeters = (centimeters*length);

                System.out.println("Length in Inches is = " + length);
            }
            choice = input.nextLine();
            if ("redo".equals(choice)) {
                cont = true;
            } else {
                cont = false;
            }
        }
    }
}