小编典典

Java重启程序帮助

java

import java.util.Scanner ;

public class Mal {

    public static void main (String []args) {

        System.out.println("Welcome") ;
        Scanner myinput=new Scanner (System.in) ;
        System.out.println("Make your choice. \n 1.Check a card number \n 2.Quit.");
        int choise=myinput.nextInt();
        switch(choise) {
            case 1:System.out.println("Enter your credit card number: ");
            break;
            case 2:System.out.println("Are you sure?") ;
            String answer=myinput.next();

            if(answer.equals("yes")){
                System.out.println("Byee :)") ;
                System.exit(0);
            }
            break;
            default: System.out.println("Idiot!") ;
            break;
        }
    }       
}

我想要一个程序,如果用户键入的内容不同于“是”,则该程序可以重新启动


阅读 206

收藏
2020-11-30

共1个答案

小编典典

这就是你可以做的

有这样的标志

boolean quit = false;

一会儿附上您的选择

while(!quit){
...
...
}

设置quit = true是否要退出。

编辑:

像这样

public static void main(String a[]) {
    System.out.println("Welcome");
    Scanner myinput = new Scanner(System.in);
    boolean quit = false;
    while (!quit) {
        System.out
                .println("Make your choise. \n 1.Check a card number \n 2.Quit.");
        int choise = myinput.nextInt();
        switch (choise) {
        case 1:
            System.out.println("Enter your credit card number: ");
            break;
        case 2:
            System.out.println("Are you sure?");
            String answer = myinput.next();

            if (answer.equals("yes")) {
                System.out.println("Byee :)");
                quit= true;
            }
            break;
        default:
            System.out.println("Idiot!");
            break;
        }
    }
2020-11-30