我正在對 Java 回圈做簡單的練習,但我被困在一個回圈上。
目標:系統詢問用戶名,然后詢問密碼。如果他們的 id 對應,則一切正常。如果不是,那就不是。
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
// Suppose we have a database composed of two fields or columns (arrays), the first field is for the username (user[]) and the other one is for the password(pass[]) .
// This is how it looks like:
// user[0] = “Hassan” ;
// user[1] =”Idris”;
// user[2]=”Trevor” ;
// And their passwords correspond with their indexes.
// pass[0] = “homecomingking”;
// pass[1] = “turnupcharlie”;
// pass[2] = “afraidofthedark”;
// Then if one of them had successfully login, the output should be:
// Enter username: Hassan
// Enter password: homecomingking
// Hello Hassan!
// But if not, “Incorrect Login!”
// You can ignore case for the username but not for the password.
// ============= SAMPLE RUN ================
// Program Starts:
// Enter username:
// hassan
// Enter password:
// homecomingking
// Output:
// Hello Hassan!
我的代碼如下所示:
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner person = new Scanner(System.in);
System.out.println("Enter username: ");
String username = person.nextLine();
System.out.println(username);
username==user; // this is not right, i guess
System.out.println("Enter password: ");
String password = person.nextLine();
System.out.println(password);
password==pass; // this is not right as well
if (user.getId.equals(pass.getId)){
System.ot.println("Hello " user);
} else {
System.out.println("Incorrect Login!");
}
當被問到時,我不明白如何獲取用戶 ID 和傳遞 ID。
uj5u.com熱心網友回復:
使用compareTo方法String來匹配兩個字串。
import java.util.*;
class MainTest{
public static void main(String ... $){
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner person = new Scanner(System.in);
System.out.println("Enter username: ");
String username = person.nextLine();
System.out.println(username);
//username==user; // this is not right, i guess
System.out.println("Enter password: ");
String password = person.nextLine();
System.out.println(password);
//password==pass; // this is not right as well
//if (user.getId.equals(pass.getId)){
// System.out.println("Hello " user);
//} else {
// System.out.println("Incorrect Login!");
//}
boolean passwordMatch = false;
for(int i =0 ;i<user.length;i )
if(user[i].compareTo(username) == 0)//checking weather entered username exists or not
if(pass[i].compareTo(password) == 0)//comparing password for corresponding user
passwordMatch = true;//password matched
if(passwordMatch)
System.out.println("Hello \"" username "\"");
else
System.out.println("Incorrect Login!");#password didn't matched
}
}
輸出:
$ javac MainTest.java && java MainTest
Enter username:
Hassan
Hassan
Enter password:
homecomingking
homecomingking
Hello "Hassan"
uj5u.com熱心網友回復:
username==user; // this is not right, I guess
首先,這是一個結果(真/假)未存盤在任何變數中的比較。此外,“user”和“pass”是字串陣列,這意味著需要對它們進行索引才能檢索您可能想要與之比較的實際字串。您要查找的是輸入用戶名在其陣列中的索引,并將其與輸入密碼在其陣列中的索引進行比較,因為它們應該是相關的。它可能看起來像這樣:
int userIndex = 0;
for(int i = 0; i < user.length; i )
if(username.equalsIgnoreCase(user[i])
userIndex = i;
int passIndex = 1;
for(int i = 0; i < pass.length; i )
if(password.equals(user[i])
passIndex = i;
if (userIndex == passIndex){
System.out.println("Hello " user[userIndex]);
} else {
System.out.println("Incorrect Login!");
}
但是,這是一個非常復雜的解決方案,您最好使用 Map 結構進行這種查找。
uj5u.com熱心網友回復:
100% 作業代碼。100/100 分給你。
package com.company.testjava;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] user = {"Hassan", "Idris", "Trevor"};
String[] pass = {"homecomingking", "turnupcharlie", "afraidofthedark"};
Scanner scanner = new Scanner(System.in);
System.out.println("Enter username: ");
String username = scanner.nextLine();
System.out.println("Enter password: ");
String password = scanner.nextLine();
int userIndex = -1;
for (int i = 0; i < user.length; i ) {
if (user[i].equalsIgnoreCase(username)) {
userIndex = i;
break;
}
}
if (userIndex >= 0 && pass[userIndex].equals(password)) {
System.out.println("Hello " user[userIndex] "!");
} else {
System.out.println("Incorrect Login!");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/426852.html
下一篇:Python:回圈中語法正確
