[英]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.