gpt4 book ai didi

java - 我如何在这里使用 "do while"命令?

转载 作者:行者123 更新时间:2023-12-01 07:23:03 26 4
gpt4 key购买 nike

我正在学习java,这是我的代码

import java.util.Scanner;


public class Application {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


System.out.println("Enter Password");
String value = input.nextLine();


if(value.equals("mycat")){
System.out.println("Password Accepted!");
System.out.println("My Personal Number is: ");
System.out.println("blahblah");
}
else
System.out.println("Incorrect Password!");


}


}

我想一直说错误的密码并允许输入另一个而不是停止。基本上我想要循环,但每当我这样做时,我都不会得到我想要的结果。

最佳答案

Scanner input = new Scanner(System.in);

do
{
System.out.println("Enter Password");
String value = input.nextLine();

if(value.equals("mycat")){
System.out.println("Password Accepted!");
System.out.println("My Personal Number is: ");
System.out.println("blahblah");
}
else
System.out.println("Incorrect Password!");
} while(!value.equals("mycat"))

input.close();

将代码包含在 Try-Catch block 中。

关于java - 我如何在这里使用 "do while"命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30846264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com