我對Java很陌生,只是隨處可用基礎知識。我做了某種計算器。如何重新啓動一個java程序
這裏(有可能更容易計算器,只需用什麼來給我的腦海裏工作)是我的代碼:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double second;
System.out.println("Please tell me a number");
double first = input.nextDouble();
input.nextLine(); // Consume newline left-over
System.out.println("You chose the number " + first);
System.out
.println("Now please choose what you want to do with the number. (divide, add, multiply or subtract)");
String text = input.nextLine();
switch (text) {
case "divide":
System.out.println("What do you want to divide your number " + first + " with?");
second = input.nextDouble();
double resultDivide = first/second;
System.out.println(first + "/" + second + " = " + resultDivide);
break;
case "add":
System.out.println("What do you want to add to your number " + first + "?");
second = input.nextDouble();
double resultAdd = first + second;
System.out.println(first + " + " + second + " = " + resultAdd);
break;
case "multiply":
System.out.println("What do you want to multiply your number " + first + " with?");
second = input.nextDouble();
double resultMultiply = first * second;
System.out.println(first + " * " + second + " = " + resultMultiply);
break;
case "subtract":
System.out.println("What do you want to subtract your number " + first + " with?");
second = input.nextDouble();
double resultSubtract = first - second;
System.out.println(first + " - " + second + " = " + resultSubtract);
break;
default:
System.out.println("Sorry, I did not quite understand that. Please try again.");
}
}
所以我的問題是:當它在開關case語句達到默認情況下,我可以做該程序從重新啓動?
在此先感謝。
中斷並繼續可選地接受一個參數。 –
你可以在代碼的主要部分放一個'while'或'do ... while'循環。 – Poohl
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html –