gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:05:21 24 4
gpt4 key购买 nike

我知道这是一个简单的 Java 概念,但我现在才刚刚开始学习如何用它编写代码。我想知道是否有人可以帮我写一个语句,以便在打印转换后,打印另一个语句说“键入'redo'以转到程序的开头。”这样他们就可以做出另一种选择。这是我的代码:

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

}
}

关于java - 如何循环回到java中程序的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com