简体   繁体   English

用Java创建Login,不会转到else语句吗?

[英]Creating Login with Java, won't go to else statement?

I'm making a login program, but it is getting some errors. 我正在制作一个登录程序,但是出现了一些错误。 If I put in a name that is not in the list, I get an error. 如果输入不在列表中的名称,则会出现错误。 It doesn't seem to reach the else statement also, and I'm not sure why. 它似乎也没有到达else语句,我不确定为什么。

import java.lang.*;
import java.util.*;
import java.io.*;
import java.math.*;
public class HelloWorld {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    String userName;
    int n = 10;
    int i = 0;

    String[] array = new String[n];
    array[0] = "John";
    array[1] = "Johny";
    array[2] = "ben";


    System.out.println("Enter your user name(Note:**Case Sensative**)");
    userName = input.nextLine();

    while (i <= array.length) {
      if (array[i].equals(userName)) {
        System.out.println("Your UserName is valid");
        break;
      }
      else if (!array[i].equals(userName)){
        i++;
      }
      else {
          System.out.println("Your UserName is not valid");
          break;
      }
    }
  }
}

First, I'd consider using a for-loop as it'll manage the i value for you, but also consider the logic you have 首先,我考虑使用for-loop因为它将为您管理i值,但还要考虑您拥有的逻辑

if element.equals(userName) {...} 
else if !element.equals(userName) {...} 
else {...}

How is it possible to ever get into the else block, either element will be or won't be equal to the userName , there is no other state. 如何进入else块,任何一个element都将等于或不等于userName ,没有其他状态。 I'd consider having a flag which indicates if a user was found or not and checking that at the other end of the loop 我考虑使用一个标志来指示是否找到了用户,并在循环的另一端进行检查

As a general concept... 作为一般概念...

//...
userName = input.nextLine();

String validUser = null;
for (int index = 0; index < array.length; index++) {
    if (array[i].equals(userName)) {
        validUser = userName;
        break;
    }
}
if (validUser != null) {
    System.out.println("UserName is valid");
} else {
    System.out.println("Your UserName is not valid");
}

Because you probably don't care about the index so much, you can also do something like... 因为您可能不太在乎索引,所以您也可以执行类似...

for (String element : array) {
    if (array[i].equals(userName)) {
        validUser = userName;
        break;
    }
}

which is shorthand version. 这是简写​​版本。

Have a look at The for statement for more details 看一下for语句以获取更多详细信息

First of all your if and else if are the only condition the java will run in your context and there are no other condition running afterwards. 首先, 如果ifelse if是唯一的条件,则Java将在您的上下文中运行,并且此后没有其他条件在运行。 It is like you are doing TRUE and FALSE and expecting another condition. 就像您正在做TRUE和FALSE并期待另一个条件一样。 Make use of foreach in java much simpler way. 使java中的foreach更加简单。

boolean found = false;
for(String name: array){
    if (name.equals(userName)) {
        found = true;
        break;
    }
}

if(!found){
    System.out.println("Your UserName is not valid");
}else{
    System.out.println("Your UserName is valid");
}

When you get mastered use the methods of Array , Collections and specially lambdas stream api for little bit lift up your java knowledge. 当您掌握了Array,Collections和特别是lambdas流api的方法后,便会稍微提高一点Java知识。

Your Statement will never go to the else part, because the the input( userName ) will either EQUAL array[i] , or not. 您的Statement永远不会转到else部分,因为input( userName )将等于EQUAL array[i]或不等于。 Think about it... 考虑一下...

if (array[i].equals(userName)) { //It will do this if userName is array[i]
    System.out.println("Your UserName is valid");
    break;
}
else if (!array[i].equals(userName)) { //It will do this if userName is NOT array[i]
    i++;
}

else { //this is impossible, unreachable code
    System.out.println("Your UserName is not valid");
    break;
}

The reason your else is not reachable is because it is an impossible scenario. 您的else无法访问的原因是,这是不可能的情况。 I edited your program, and it works now. 我编辑了您的程序,现在可以使用了。 Here's the whole thing: 整个过程如下:

import java.util.Scanner;

public class Experiments {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String userName;
        boolean found = false;

        String[] array = new String[3];
        array[0] = "John";
        array[1] = "Johny";
        array[2] = "ben";


        System.out.println("Enter your user name:");
        userName = input.nextLine();

        for(int i=0; i<array.length; i++) {
            if (userName.equalsIgnoreCase(array[i])) { 
                System.out.println("Your UserName is valid");
                found = true;
                break;
            }
            if (!found && i == array.length-1) {
                System.out.println("Your UserName is not valid");
            }
        }
        input.close();
    } 
}

If the input (userName) is in the array at any spot, it will say "Your UserName is valid" If it isn't in the array, it will say "Your UserName is not valid" . 如果输入(userName)在数组中的任何位置,则将显示"Your UserName is valid"如果不在数组中,则将显示"Your UserName is not valid"

Here is an example of a valid userName: 这是有效的用户名的示例:

Enter your user name:
John
Your UserName is valid

And if the UserName is invalid: 如果用户名无效:

Enter your user name:
Jimmy
Your UserName is not valid

Please comment if you have any questions about this, and I will explain. 如果对此有任何疑问,请发表评论,我会解释。

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

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