我一直在为我的游戏制作一个登录系统原型(prototype),遗憾的是它不起作用!我不知道为什么,但这是我的代码:
这是我的主类的代码:`
package darkbyte.tests;
import java.util.Scanner;
public class Main {
private static FileCreator creator = new FileCreator();
private static FileReader reader = new FileReader();
private static void login() {
Scanner input = new Scanner(System.in);
boolean passwordExists;
System.out.println();
System.out.println("Please enter your username: ");
String username = input.nextLine();
System.out.println("Please enter your password: ");
String password = input.nextLine();
reader.openFile(username + ".user");
if(reader.doesStringExist(password)) {
passwordExists = true;
} else {
passwordExists = false;
}
reader.closeFile();
if(passwordExists) {
System.out.println("Welcome back to Darkbyte " + username + "!");
} else {
System.out.println("The password you entered is incorrect!");
login();
}
}
private static void register() {
Scanner input = new Scanner(System.in);
System.out.println();
System.out.println("Please enter the username you desire: ");
String username = input.nextLine();
System.out.println("Please enter the password you desire: ");
String password = input.nextLine();
creator.openFile(username + ".user");
creator.writeString(username);
creator.writeString(password);
creator.closeFile();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Darkbyte!");
System.out.println("Would you like to login or register an account?");
System.out.println("Enter 0 to login and any other number to register: ");
int choice = input.nextInt();
if(choice == 0) {
login();
} else {
register();
}
}
}
这是我的 FileCreator 类的代码:
package darkbyte.tests;
import java.io.*;
import java.util.*;
public class FileCreator {
private Formatter format;
public void openFile(String file) {
try {
format = new Formatter(file);
} catch(Exception e) {
System.err.println("Darkbyte: There was an error in opening the file!");
}
}
public void closeFile() {
format.close();
}
public void writeInteger(int i) {
format.format("%i%n", i);
}
public void writeFloat(float f) {
format.format("%f%n", f);
}
public void writeString(String s) {
format.format("%s%n", s);
}
public void appendInt(String file, int i) {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
out.println(i);
}catch (IOException e) {
System.err.println("Darkbyte: Couldn't find file!");
}
}
public void appendFloat(String file, float f) {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
out.println(f);
}catch (IOException e) {
System.err.println("Darkbyte: Couldn't find file!");
}
}
public void appendString(String file, String s) {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)))) {
out.println(s);
}catch (IOException e) {
System.err.println("Darkbyte: Couldn't find file!");
}
}
}
这是我的 FileReader 类的代码:
package darkbyte.tests;
import java.io.*;
import java.util.*;
public class FileReader {
private Scanner scanner;
public void openFile(String file) {
try {
scanner = new Scanner(new File(file));
} catch(Exception e) {
System.err.println("Darkbyte: Couldn't find file!");
}
}
public void closeFile() {
scanner.close();
}
public void readInt(int i, int lookingFor) {
while(i != lookingFor) {
if(scanner.hasNext()) {
i = Integer.parseInt(scanner.next());
}
}
}
public void readFloat(float f, float lookingFor) {
while(f != lookingFor) {
if(scanner.hasNext()) {
f = Float.parseFloat(scanner.next());
}
}
}
public void readString(String s, String lookingFor) {
while(s != lookingFor) {
if(scanner.hasNext()) {
s = scanner.next();
}
}
}
public boolean doesIntExist(int i) {
boolean intIsFound = false;
int tempInt;
while(scanner.hasNext()) {
if(!intIsFound) {
tempInt = Integer.parseInt(scanner.next());
if(tempInt == i) {
intIsFound = true;
} else {
intIsFound = false;
}
}
}
return intIsFound;
}
public boolean doesFloatExist(float f) {
boolean floatIsFound = false;
float tempFloat;
while(scanner.hasNext()) {
if(!floatIsFound) {
tempFloat = Float.parseFloat(scanner.next());
if(tempFloat == f) {
floatIsFound = true;
} else {
floatIsFound = false;
}
}
}
return floatIsFound;
}
public boolean doesStringExist(String s) {
boolean stringIsFound = false;
String tempString;
while(scanner.hasNext()) {
if(!stringIsFound) {
tempString = scanner.next();
if(tempString == s) {
stringIsFound = true;
} else {
stringIsFound = false;
}
}
}
return stringIsFound;
}
}
问题是,当我尝试登录时,即使我的用户名和密码正确,它仍然显示我的详细信息不正确!
我不知道为什么,但你能帮我吗?
最佳答案
我认为错误在于
if(tempString == s) {
stringIsFound = true;
} else {
stringIsFound = false;
}
将其更改为:
if(tempString.equals(s)) {
stringIsFound = true;
} else {
stringIsFound = false;
}
比较字符串时始终使用equals
,否则您比较的是它们的引用而不是它们的值。
关于Java:登录系统无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137663/