当前位置: 首页 > 知识库问答 >
问题:

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

养俊驰
2023-03-14

我知道这是一个简单的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个答案

吕嘉赐
2023-03-14

用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;
            }
        }
    }
}
束高雅
2023-03-14
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;
            }
        }

    }
}
 类似资料:
  • 问题内容: 我正在尝试使用Java进行打字冒险类游戏,但是我至少需要一个与标题中的命令相似的命令,这是代码 每当用户键入除要求之外的其他内容时,都会发生默认情况,但我需要它返回到循环的开始,因此用户可以键入其他情况之一。 问题答案: 您可以使用该语句继续进行下一个迭代。 就是说,您的示例代码中没有看到循环。你可以用一个循环,或。该DO /时 循环至少执行一次- 这通常是你想要询问用户问题时该怎么办

  • 我正在编写一个基于文本的冒险游戏,我遇到了一个问题。我正在将它从终端移植到JFrame GUI,我需要它在继续之前等待用户输入。我设置了一个系统,其中有一个名为按钮的布尔变量,它开始时为false。当按下提交文本按钮时,它会将其更改为true。有一个while循环,它会等待按钮变为true,然后才允许程序继续,基本上是暂停程序直到用户提交输入。问题是,只有当我在while循环中写入system.o

  • 我必须写一个使用循环的程序,计算a和b之间所有奇数的和(包括),其中a和b是输入。 我做了这个(如下),它工作得很好,但我注意到它有一个问题:当我输入一个较大的数字,然后输入一个较小的数字时,它返回0,但当我首先输入一个较小的数字时,它工作得很好。有什么快速的解决办法吗?:)

  • 到目前为止,我的计划是: 现在我需要它做的是生成一个从华氏度到摄氏度的20个温度转换表。如果用户输入 0,下面是输出可能是什么样子的前 3 行的示例: 华氏温度:0摄氏度:-17.78 华氏度: 5 摄氏度: -15.00 华氏:10摄氏度-12.22度 等… 问题是,如果输入大于20,它将不会循环正确的次数。 下面是一个例子,说明如果用户输入5: 输出: 输入您的起始温度(华氏:5)。 华氏摄氏

  • 问题内容: 例如 :- 彼此替换字符。 输出 我该怎么做而没有任何循环? 问题答案: 一种方法是使用流。和地图: 那打印

  • 我如何使它再次循环回到FOR循环的开始?我试着寻找答案。continue的用法似乎对我不起作用。