【问题标题】:How make this program terminate after如何使该程序终止后
【发布时间】:2014-01-17 02:09:20
【问题描述】:

下面是我编写的一个简单程序,它会要求输入密码。如果我输入了错误的密码,系统会提示我“密码不正确,你想再试一次吗?”,如果我说不或者其他不以'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();
         }
      }
   }
}

【问题讨论】:

  • input.equalsIgnoreCase("Noah")==true 去掉==true,看到这个我生气了。
  • 也不需要“if(input.equalsIgnoreCase("Noah")==false)”,如果它不是真的,它是假的。

标签: java loops if-statement for-loop while-loop


【解决方案1】:
  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();
         }
      }

【讨论】:

    【解决方案2】:

    使用break;然后你可以终止。

    if("Noah".equalsIgnoreCase(input)){
        System.out.println("Password correct");
        break;
    }
    

    【讨论】:

      【解决方案3】:

      有几种方法。越来越严重:

      1) 使用break 语句。这将把程序控制流带到while 循环结束之后。

      2) 使用return 语句。这将退出函数,在您的特定情况下,这将结束程序。

      3) 插入System.exit(n),其中n 是一个可能非零的数字,以指示返回状态。这将终止 Java 虚拟机,并将值 n 返回给操作系统。

      在你的情况下,我倾向于选择 (1) 或 (2)。

      【讨论】:

        【解决方案4】:

        if(input.equalsIgnoreCase("Noah")==true) { System.out.println("Password correct"); break;
        }

        或者你可以加response ="no";

        if(input.equalsIgnoreCase("Noah")==true) { System.out.println("Password correct"); response ="no";
        }

        "no" 或任何不以 'y' 字符开头的内容。

        【讨论】:

          【解决方案5】:

          做了一些更改后,我想这就是您要寻找的:

          导入 java.util.Scanner;

          公共类方法{

          public static void main (String [] args){
          
              Scanner sc = new Scanner(System.in);
              String response = "yes";
          
              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();
                  }
              }
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2020-01-20
            • 1970-01-01
            • 1970-01-01
            • 2014-05-01
            • 2023-04-03
            • 1970-01-01
            • 1970-01-01
            • 2023-03-26
            • 1970-01-01
            相关资源
            最近更新 更多