重启程序的 Java 帮助 [英] Java help for restart program

查看:47
本文介绍了重启程序的 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;
        }
    }       
}

我想要一个程序,如果用户输入的内容不同于是",它会重新启动

i want a program which starts again if user types something different than "yes"

推荐答案

您可以这样做

有一个这样的标志

boolean quit = false;

然后附上您的选项

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

设置 quit = true 如果用户想退出.

set quit = true if the user wants to quit.

像这样

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

这篇关于重启程序的 Java 帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆