简体   繁体   English

if 和 else 语句 JAVA

[英]if and else statements JAVA

I'm practicing if and else statements and i did a guess your password string, if you input the right password a congratulation message appears but if you enter the wrong one another message appears that says "Please try again" the issue that im having is that i dont know how to set another congratulation message if the user guess the right password on try again.我正在练习 if 和 else 语句,我猜测了您的密码字符串,如果您输入了正确的密码,则会出现一条祝贺消息,但如果您输入错误,则会出现另一条消息,上面写着“请再试一次”,我遇到的问题是如果用户再次尝试猜到正确的密码,我不知道如何设置另一条祝贺消息。

public static void main(String[] args) {

    String password = "Shippuden345";
    System.out.println("Enter or guess the password: ");

    Scanner scanner = new Scanner(System.in);
    String guess = scanner.nextLine();

    System.out.println(password.equals(guess));

    if (password.equals(guess)) {
        System.out.println("Your guess was correct");
        return;
    }
    else;
    {
        System.out.println("Please try again: ");
        Scanner scanner1 = new Scanner(System.in);
        String again = scanner1.nextLine();
    }

}

} }

i want to set another message in here, if the user guess the password correctly after trying again.我想在这里设置另一条消息,如果用户在重试后猜对了密码。

 else;
    {
        System.out.println("Please try again: ");
        Scanner scanner1 = new Scanner(System.in);
        String again = scanner1.nextLine();
    }

}

} }

It would be best if you used a do while loop:最好使用 do while 循环:

public static void main(String[] args) {

    String password = "Shippuden345";
    String guess;
    do{
    System.out.println("Enter or guess the password: ");
    guess = scanner.nextLine();

    System.out.println(password.equals(guess));

    if (password.equals(guess)) {
        System.out.println("Your guess was correct");
        return;
    }
    else {
        System.out.println("Please try again: ");
    }

    }while(!password.equals(guess));
}

I have edited your code.我已经编辑了你的代码。 However, I have added it to a static void:但是,我已将其添加到静态无效:

I have also added an int to be shown if the first attempt was incorrect.如果第一次尝试不正确,我还添加了一个int以显示。 Hope you like it.希望你喜欢。


class s{

public static void check(){

int attempts = 0;

    String password = "Shippuden345";
    System.out.println("Enter or guess the password: ");

    Scanner scanner = new Scanner(System.in);
    String guess = scanner.nextLine();

    System.out.println(password.equals(guess));

while(attempts < 5){
    if (password.equals(guess) && attempts == 0) {
        System.out.println("Your guess was correct");
        return;
    }
else
    if (password.equals(guess) && attempts > 0) {
        System.out.println("Attempt number "+attempts+" was correct.");
        return;
    }

    else{
        System.out.println("Please try again: ");
    attempts++;
guess = scanner.nextLine();
    }
}

}

public static void main(String[] args) {
check();
}

}```

You can put the input and processing of the input value inside an infinite loop (eg while(true){} ) which you can break on the correct input.您可以将输入值的输入和处理放在一个无限循环中(例如while(true){} ),您可以在正确的输入上中断。

import java.util.Scanner;

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

        while (true) {
            System.out.print("Enter or guess the password: ");
            String guess = scanner.nextLine();

            if (password.equals(guess)) {
                System.out.println("Your guess was correct");
                break;
            } else {
                System.out.println("Please try again: ");
            }
        }
    }
}

A sample run:示例运行:

Enter or guess the password: aSD
Please try again: 
Enter or guess the password: 12H
Please try again: 
Enter or guess the password: Shippuden345
Your guess was correct

You can use a loop that starts after the first guess of passwords if the guess is incorrect and continues the loop if passwords entered in the following trials are incorrect.如果猜测不正确,您可以使用在第一次猜测密码后开始的循环,如果在以下试验中输入的密码不正确,则继续循环。

Then breaks the loop when the password matches and prints the congratulate message.然后在密码匹配时中断循环并打印祝贺消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM