繁体   English   中英

如何在java中循环回程序的开头

[英]How to loop back to the beginning of a program in 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);
        }
    }
}
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;
            }
        }

    }
}

用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;
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM