Java - 你想继续吗?(Y/N)

Java - Do you want to continue?(Y/N)

我想写一个简单的循环让程序返回并重新启动。

只是一个简单的1题程序。然后系统询问用户是否想再做一次。如果用户输入 Y ... 程序将循环回到开头并再次 运行 整个程序。如果用户输入N,则退出。

import java.util.Scanner; // show them as code

public class HowToDoLoop {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("How much money do you want to have? ");
    double money = input.nextDouble();

    System.out.println("Ok, here is yours $" + money);

    System.out.println("Do you want to continue y or n");
while(true){

    System.out.println("How much money do you want to have? ");
    double money = input.nextDouble();

    System.out.println("Ok, here is yours $" + money);

    System.out.println("Do you want to continue y or n");
    String c = input.nextLine();

   if(c.equalsIgnoreCase("n")){ 
      break;
     }//else continue to loop on any string ;-)

}
String c = "";
do{
    System.out.println("How much money do you want to have? ");
    double money = input.nextDouble();

    System.out.println("Ok, here is yours $" + money);

    System.out.println("Do you want to continue y or n");
    c = input.nextLine();

}while(c.equalsIgnoreCase("Y"));