gpt4 book ai didi

java - 如何让这个程序在之后终止

转载 作者:行者123 更新时间:2023-12-02 03:03:27 25 4
gpt4 key购买 nike

下面是我编写的一个简单的程序,它会要求输入密码。如果我输入的密码不正确,系统会提示我“密码不正确,您想再试一次吗?”,如果我说“否”或任何其他不以“y”开头的内容,它将终止程序。问题是,如果我输入正确的密码“Noah”,它会显示“密码正确”,然后再次循环回到“输入密码”。输入正确的密码后如何使该程序终止?谢谢。

import java.util.Scanner;
public class methods
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
String response = "yes";
System.out.println("Enter password:");
while(response.charAt(0)=='y')
{
String input = sc.nextLine();
if(input.equalsIgnoreCase("Noah")==true)
{
System.out.println("Password correct");
}
else if(input.equalsIgnoreCase("Noah")==false)
{
System.out.println("Password incorrect, would you like to try again?");
response = sc.nextLine();
}
}
}
}

最佳答案

  while(response.charAt(0)=='y')
{ System.out.println("Enter password")
String input = sc.nextLine();
if(input.equalsIgnoreCase("Noah")==true)
{
System.out.println("Password correct");
break;
}
else if(input.equalsIgnoreCase("Noah")==false)
{
System.out.println("Password incorrect, would you like to try again?");
response = sc.nextLine();
}
}

关于java - 如何让这个程序在之后终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835324/

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