Java help for restart program

前端 未结 1 1600
春和景丽
春和景丽 2021-01-26 07:04
import java.util.Scanner ;

public class Mal { 

    public static void main (String []args) {

        System.out.println(\"Welcome\") ;
        Scanner myinput=new Sca         


        
1条回答
  •  再見小時候
    2021-01-26 07:23

    Here's what you can do

    have a flag like this

    boolean quit = false;
    

    and a while enclosing your options

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

    and

    set quit = true if the user wants to quit.

    EDIT:

    Something like this

    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;
            }
        }
    

    0 讨论(0)
提交回复
热议问题