如何阻止控制台重复循环运行?

huangapple 未分类评论46阅读模式
英文:

How to stop console from repeating loop over and over?

问题

我正在尝试使密码不正确时重新开始循环直到输入正确的密码为止这一点我已经实现了),但是 System.out.println 语句会在控制台中无限重复打印使用 break 语句会终止代码执行但我希望代码继续执行只打印这些语句一次有什么解决办法吗

public static void main(String[] args) {

    System.out.println("请输入密码:");

    Scanner passwordScan = new Scanner(System.in);
    String pass = passwordScan.nextLine();
    String password = "JohnnyRoberts123";

    while (!password.equals(pass)) {
        System.out.println("密码错误!");
        System.out.println("请重试,输入密码:");

        pass = passwordScan.nextLine();
    }
}
英文:

I'm trying to make the incorrect password start the loop over until the right password is inputted (which it does) but System.out.println statements repeat infinitely in the console. A break statement just terminates the code, but I want it to keep going except only printing the statements once. Any solutions?

	public static void main(String[] args){
	
	System.out.println("What is the password?");
	
	Scanner passwordScan = new Scanner(System.in);
	String pass = passwordScan.nextLine();
	String password = "JohnnyRoberts123";
	
	 
	while(password != pass) {
		System.out.println("Wrong password!");
		System.out.println("Try again, what is the password?");
		
		continue;

答案1

得分: 0

我看不到任何从用户获取输入的代码。while 循环在遇到 continue 时会立即重新开始。

你可能更想要像这样的代码:

java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(password != pass) {
        System.out.println("密码错误!");
        System.out.println("请重试,密码是什么?");
        password = in.readLine();
        continue;
}
英文:

I don't see any code that would get any input from the user. The while loop gets immediately reiterated when it hits continue.

You rather want something like:

java.io.BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(password != pass) {
        System.out.println("Wrong password!");
        System.out.println("Try again, what is the password?");
        password = in.readLine();
        continue;
}

答案2

得分: 0

首先,永远不要在代码中硬编码密码。如果仅用于测试,是可以的。

其次,在 while 循环中,您必须更新密码,以便进行验证,如下所示:

public static void main(String[] args){

    System.out.println("密码是什么?");

    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    String pass = "";

    while (!password.equals(pass)) {
        System.out.println("密码错误!");
        System.out.println("请重试,密码是什么?");
        pass = passwordScan.nextLine();
        continue;
    }
}

请注意,您不能使用 == 来将字符串作为整数进行比较,字符串被视为对象,因此必须使用 str1.equals(str2) 进行比较。

英文:

First, never hardcode the password. If just for testing , it's ok.

Second, you must update the password in the while loop, in order to check for validation as follows :

public static void main(String[] args){

    System.out.println("What is the password?");

    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    String pass="";

    while(!password.equals(pass)) {
        System.out.println("Wrong password!");
        System.out.println("Try again, what is the password?");
        pass = passwordScan.nextLine();
        continue;
}

Notice here, you can't compare strings as integers using == , Strings are treated as objects so you must compare them by using str1.equals(str2)

答案3

得分: 0

你只需要稍微调整一下你的代码,让要求输入的那行代码(passwordScan.nextLine())放在 while 循环的内部

public static void main(String[] args) {
    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    System.out.println("What is the password?");

    while (true) {
        String pass = passwordScan.nextLine();

        if (!password.equals(pass)) {
            System.out.println("Wrong password!");
            System.out.println("Try again, what is the password?");
        } else {
            break;
        }
    }
}

另外请注意,您不应该使用 != 来比较字符串,应该始终使用 equals 方法,因为它实际上比较的是字符串内容,而不仅仅是对象本身。

英文:

You just need to move your code around a little so the line asking for input (passwordScan.nextLine()) is inside the while loop:

public static void main(String[] args) {
    Scanner passwordScan = new Scanner(System.in);
    String password = "JohnnyRoberts123";
    System.out.println("What is the password?");

    while (true) {
        String pass = passwordScan.nextLine();

        if (!password.equals(pass)) {
            System.out.println("Wrong password!");
            System.out.println("Try again, what is the password?");
        } else {
            break;
        }
    }
}

Also note that you should never use != to compare strings, always use the equals method because it actually compares the string contents, not just the objects themselves.

答案4

得分: 0

你的代码存在两个问题:

  1. 你使用了!=来比较字符串,这是不正确的。请注意,==!=比较的是引用,而不是内容。
  2. 你没有将输入部分放在while循环中,以便在输入错误的情况下,用户可以被要求再次输入密码。

请按以下方式修改代码:

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        Scanner passwordScan = new Scanner(System.in);
        String pass = "";
        String password = "JohnnyRoberts123";

        while (!password.equals(pass)) {
            System.out.println("What is the password?");
            pass = passwordScan.nextLine();
            if (!password.equals(pass)) {
                System.out.println("Wrong password!");
                System.out.println("Try again, what is the password?");
                continue;
            }
        }
        // ...rest of code
    }
}

一个示例运行:

What is the password?
hello
Wrong password!
Try again, what is the password?
What is the password?
hi
Wrong password!
Try again, what is the password?
What is the password?
JohnnyRoberts123
英文:

There are two problems with your code:

  1. You have used != to compare strings, which is not correct. Note that == or != compares the references, not the content.
  2. You haven't placed the input part in the while loop so that in case of wrong input, the user can be asked to enter the password again.

Do it as follows:

import java.util.Scanner;

class Main {
	public static void main(String args[]) {
		Scanner passwordScan = new Scanner(System.in);
		String pass = "";
		String password = "JohnnyRoberts123";

		while (!password.equals(pass)) {
			System.out.println("What is the password?");
			pass = passwordScan.nextLine();
			if (!password.equals(pass)) {
				System.out.println("Wrong password!");
				System.out.println("Try again, what is the password?");
				continue;
			}
		}
		// ...rest of code
	}
}

A sample run:

What is the password?
hello
Wrong password!
Try again, what is the password?
What is the password?
hi
Wrong password!
Try again, what is the password?
What is the password?
JohnnyRoberts123

huangapple
  • 本文由 发表于 2020年5月29日 05:25:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/62074809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定