Java - 你想继续吗?(是/否)

问题描述 投票:-3回答:2

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

只是一个简单的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");
java loops repeat continue
2个回答
1
投票
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"));

1
投票
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 ;-)

}
© www.soinside.com 2019 - 2024. All rights reserved.