终止后如何重新启动Java方法

香奈儿90

我是 Java 新手,我想我会制作经典的用户名和密码验证程序之一,我已经成功地制作了没有明显错误的程序,但是如果用户输入了不正确的信息,我希望程序基本上重新启动输入. 每次用户输入错误信息时,我将如何成功重新启动程序?

代码如下:

import java.util.Scanner;
public class UserPass {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String user; //Creating user-name variable.
        String pass; //Creating password variable.


        System.out.println("Enter username here: "); //Message to tell user to input the user-name.
        user = input.nextLine(); //Taking the users user-name input.

        System.out.println("Enter the password here: "); //Message to tell user to input the password.
        pass = input.nextLine(); //Taking the users password input.

        //Validating the users User-name and password input.
        if(user.equals("Shane") && (pass.equals("Temple"))) {
            System.out.println("Correct!"); //If the User-name and password are both correct then a message will tell the user that they are correct.
        }

        else {
            System.out.println("The Usernname or Password that you have entered was in-correct"); //If above conditions are not met then message will tell the user that they have entered the wrong user-name or password
        }
    }

}

我知道这是非常基本的,因为我说我对 Java 很陌生(2 小时前是新的)。

我想在 else 条件语句中调用“main”方法,但是我听说比程序第一次启动时使用“main”方法的次数更多是基本做法。

提前致谢 :)

桑托什

我希望这就是你想要的。

import java.util.Scanner;

public class UserPass {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String user; //Creating user-name variable.
        String pass; //Creating password variable.
        boolean isValidUser = false;

        while(!isValidUser) {
            System.out.println("Enter username here: "); //Message to tell user to input the user-name.
            user = input.nextLine(); //Taking the users user-name input.

            System.out.println("Enter the password here: "); //Message to tell user to input the password.
            pass = input.nextLine(); //Taking the users password input.

            //Validating the users User-name and password input.
            if(user.equals("Shane") && (pass.equals("Temple"))) {
                System.out.println("Correct!"); 
                isValidUser = true;
            }
            else {
                System.out.println("The Usernname or Password that you have entered was in-correct"); 
                isValidUser = false;
            }
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何终止重新启动的进程?(永远)

来自分类Dev

终止后,VoIP App不重新启动

来自分类Dev

程序终止后几秒钟后重新启动

来自分类Dev

确保旧进程终止后,Gunicorn重新启动

来自分类Dev

程序终止后几秒钟后重新启动

来自分类Dev

部署后如何重新启动puma?

来自分类Dev

如何使用foreach终止并重新启动

来自分类Dev

终止并重新启动后,tmux自动重新连接到会话

来自分类Dev

Postgresql查询未终止,重新启动服务后postgresql无法启动

来自分类Dev

如何从方法重新启动程序

来自分类Dev

重新启动后如何自动启动docker?

来自分类Dev

终止并重新启动进程

来自分类Dev

如何终止“ sudo kill -9”无法重新启动就无法终止的进程?

来自分类Dev

我如何知道docker守护程序重新启动后哪个docker将重新启动

来自分类Dev

如何完成KB3105213的安装-重新启动后仍然需要重新启动

来自分类Dev

更新后重新启动

来自分类Dev

Puma-Linux上的Rails //进程终止后重新启动

来自分类Dev

Spring Batch在异常终止后重新启动持久性作业

来自分类Dev

终止后重新启动Amazon AWS EC2实例

来自分类Dev

一定条件后重新启动委托方法

来自分类Dev

重新启动后永久增加Java堆大小吗?

来自分类Dev

让代码在无效输入后重新启动 (Java)

来自分类Dev

更新后如何重新启动自包

来自分类Dev

重新启动后如何使ntpd立即同步?

来自分类Dev

崩溃后如何重新启动Qt应用程序?

来自分类Dev

完成后如何重新启动python脚本

来自分类Dev

在错误强制停止后如何使程序重新启动?

来自分类Dev

WebStorm:重新启动后如何保存终端会话?

来自分类Dev

种子节点进程崩溃后如何重新启动?

Related 相关文章

热门标签

归档