如何向该java代码添加尝试次数

如何向该java代码添加尝试次数,java,Java,我想在这里加上3次尝试,我尝试了不同的组合,例如“int n=0;for(n>3;n++)”,但它并没有像预期的那样工作您应该在while子句中添加条件 public static void main(String[] args) { Scanner reader = new Scanner(System.in); while (true) { System.out.print("Type password:\t"); String command

我想在这里加上3次尝试,我尝试了不同的组合,例如“int n=0;for(n>3;n++)”,但它并没有像预期的那样工作

您应该在
while
子句中添加条件

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    while (true) {
        System.out.print("Type password:\t");
        String command = reader.nextLine();
        if (command.equals("carrot")) {
            break;

        } else {
            System.out.println("Wrong!");
        }

    }
    System.out.println("Right!");
    System.out.println("The secret is: jryy qbar!");
    reader.close();
}

您应该在
while
子句中添加条件

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    while (true) {
        System.out.print("Type password:\t");
        String command = reader.nextLine();
        if (command.equals("carrot")) {
            break;

        } else {
            System.out.println("Wrong!");
        }

    }
    System.out.println("Right!");
    System.out.println("The secret is: jryy qbar!");
    reader.close();
}
Scanner reader=新的扫描仪(System.in);
对于(int i=0;i<3;i++){
System.out.print(“键入密码:\t”);
String命令=reader.nextLine();
if(command.equals(“carrot”)){
System.out.println(“对!”);
System.out.println(“秘密是:jryy-qbar!”);
打破
}否则{
System.out.println(“错误!”);
}
}
reader.close();
扫描仪阅读器=新扫描仪(System.in);
对于(int i=0;i<3;i++){
System.out.print(“键入密码:\t”);
String命令=reader.nextLine();
if(command.equals(“carrot”)){
System.out.println(“对!”);
System.out.println(“秘密是:jryy-qbar!”);
打破
}否则{
System.out.println(“错误!”);
}
}
reader.close();

Try
for(int n=0;n<3;n++)
参见Oracle的Java教程,了解如何正确使用
for
创建循环。作为for循环的替代方法,初始化变量,例如
int-truts=3在循环中递减它,并在(尝试次数>0){truments--;…}
时执行
。这样,您还可以告诉用户还有多少次尝试(
System.out.println(“错误!再试一次,您还有”+attempts+”)
)。
int-attemptsLeft=3;for(;attemptsLeft>0;attemptsLeft--)
Try
for(int n=0;n<3;n++)
请参阅Oracle的Java教程,了解如何正确使用
for
创建循环。作为for循环的替代方法,初始化变量,例如
int-truments=3在循环中递减它,并在(尝试次数>0){truments--;…}
时执行
。这样,您还可以告诉用户还有多少次尝试(
System.out.println(“错误!再试一次,您还有”+attempts+”)
)。
int-attemptsLeft=3;对于(;attempsleft>0;attempsleft--)
java中没有bool数据类型!java中没有bool数据类型@ErwinBolwidt使用System.exit是否存在任何问题??我已经更新了我的答案来删除这个,但这有什么关系??这只是一个主要的方法??你能告诉我我的答案有什么问题吗。。。因为我想学习并改善我的未来answers@Abishek
系统退出
不用于正常流量控制。如果在更大的程序中使用此函数会怎么样?@ErwinBolwidt使用System.exit有什么问题吗??我已经更新了我的答案来删除这个,但这有什么关系??这只是一个主要的方法??你能告诉我我的答案有什么问题吗。。。因为我想学习并改善我的未来answers@Abishek
系统退出
不用于正常流量控制。如果在更大的程序中使用此函数会怎么样?
try (Scanner reader = new Scanner(System.in)) {
  // your code using scanner
}
public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int maxAttempts = 3;
    while (maxAttempts > 0) {
        System.out.print("Type password:\t");
        String command = reader.nextLine();
        if (command.equals("carrot"))  {
            System.out.println("Right!");
            break;
        } else{
            System.out.println("Wrong!");
            System.out.println("The secret is: jryy qbar!");
            maxAttempts--;
            if(maxAttempts == 0){
                System.out.println("You have reached the max number of attempts!");
            }
        }
    }
    reader.close();
}
    Scanner reader = new Scanner(System.in);
    for (int i = 0; i < 3; i++) {
        System.out.print("Type password:\t");
        String command = reader.nextLine();
        if (command.equals("carrot")) {
            System.out.println("Right!");
            System.out.println("The secret is: jryy qbar!");
            break;
        } else {
            System.out.println("Wrong!");
        }

    }
    reader.close();
public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int i = 0;
        while (true) {
            System.out.print("Type password:\t");
            String command = reader.nextLine();
            if (command.equals("carrot")) {
                System.out.println("Right!");
                System.out.println("The secret is: jryy qbar!");
                break;
            } else {
                i++;
                System.out.println("Wrong!");
                if (i == 3) {
                    System.out.println("Max attempts reached!! Exiting....");
                    break;
                }
            }
        }
        reader.close();
}