猿问

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

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


只是一个简单的 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");


holdtom
浏览 191回答 2
2回答

四季花海

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"));

素胚勾勒不出你

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 ;-)}
随时随地看视频慕课网APP

相关分类

Java
我要回答