ArrayList学生登录编译错误
ArrayList Student Login compilation error
我的代码有问题。我正在尝试创建一个学生日志,在其中正确输入后将允许用户访问 memberEnter() 部分,如果错误则系统应该退出。
我一直在尝试编译代码,但它一直给我一个错误,谁能帮我解决这个问题?
我得到的错误是:
cannot find symbol - variable studentidlogin
发生错误的行是
if(mem.studentidlogin = ("\n Not a member "))
在MainSystem
的main
方法中(代码本身也有标注)
主系统代码:
import java.io.*;//imports the io package
import java.util.Scanner;//imports scanner
import java.util.*;//imports the util package
public class MainSystem {
static String fileName = null;
static Library lib = new Library();
static Scanner in = new Scanner(System.in);
static Boolean running = true;
static Member mem = new Member();//static variables that can be initialised at compile time, but can be modified at run time
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
/************* ERROR OCCURS HERE **************/
if(mem.studentidlogin = ("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
private static void newMember() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int libraryNumber;
String StudentID, Username, FullName, Address, email, PhoneNumber;
System.out.println("\nEnter Library Number: ");
libraryNumber= Integer.parseInt(br.readLine());;//prompts user to enter library number.
System.out.println("\nEnter Student ID: ");//prompts user to enter student id.
StudentID = br.readLine();
System.out.println("\nEnter Full Name: ");//prompts user to enter full name
FullName = br.readLine();
System.out.println("\nEnter Username: ");//prompts user to enter username
Username = br.readLine();
System.out.println("\nEnter E-Mail Address: ");//prompts user to enter an email address
email = br.readLine();
System.out.println("\nEnter Home Address: ");//prompts user to enter home address
Address = br.readLine();
System.out.println("\nEnter Phone Number: ");//prompts user to enter phone number
PhoneNumber = br.readLine();
StudentSignUp b = new StudentSignUp(libraryNumber, FullName, StudentID, Username, Address, email, PhoneNumber);
mem.newMember(b);
System.out.println("\nThankyou. You can now Login.: ");// prompts user that they can now sign in.
}
private static void memberEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for Search For A Book");//options available if member is enters.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved script
break;
case 1:
saveAndQuit();//saves and quits from the system
break;
case 2:
System.out.println(lib.toString());//lists all the books in the library
break;
case 3:
searchBook();//searches for a book
break;
}
}
System.exit(0);
}
private static void librairanEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for add book to library"
+ "\nEnter 4 for Search For A Book"
+ "\nEnter 5 for list of current members"
+ "\nEnter 6 to borrow a book"
+ "\nEnter 7 to return a book");//options available if the librarian logs in.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved libraries
break;
case 1:
saveAndQuit();//saves and quits
break;
case 2:
System.out.println(lib.toString());//prints current books in library
break;
case 3:
addBook();//adds a book to the system
break;
case 4:
searchBook();//searches for a book in the system
case 5:
System.out.println(mem.toString());//list of current members
break;
case 6:
borrowBook();//allows to borrow a book.
case 7:
returnBook();//allows to return a book.
}
}
System.exit(0);//exits from the system/
}
private static void addBook() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int isbn, numcopies;
String author, title, genre;// initializes the variables
System.out.println("\nEnter ISBN: ");
isbn = Integer.parseInt(br.readLine());//prompts user to enter isbn
System.out.println("\nEnter Author: ");//prompts user to enter the auth
author = br.readLine();
System.out.println("\nEnter Title: ");//prompts user to enter title
title = br.readLine();
System.out.println("\nEnter Genre: ");//prompts user to enter genre
genre = br.readLine();
System.out.println("\nEnter Number Of Copies: ");//prompts user to enter number of copies
numcopies = Integer.parseInt(br.readLine());
Book b = new Book(isbn, author, title, genre, numcopies);//Creates the new book and adds it to the library
lib.addBook(b);
}
private static void saveAndQuit() {//saves and quits
// TODO Auto-generated method stub
System.out.println("Enter file name: ");//prompts the user to enter the file name.
fileName = in.next() + ".ser";//saves the file name
running = false;
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(lib);
fos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void loadScript(String name) {//loads a previously saved script
// TODO Auto-generated method stub
FileInputStream fis = null;
ObjectInputStream in = null;
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
lib = (Library) in.readObject();
fis.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("\nThe file does not exist!");
}
}
private static void searchBook() throws IOException//allows searching for a book
{
String titleSearch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."////prompts the user to answer 0
+ "\nEnter 3 to go back");//promts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the title of the book
titleSearch = br.readLine();
System.out.println(lib.searchTitle( titleSearch));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void borrowBook() throws IOException
{
String titleBorrow;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."//prompts the user to answer 0
+ "\nEnter 3 to go back");//prompt the user to answer 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user ot enter the book title
titleBorrow = br.readLine();
System.out.println(lib.borrowBook( titleBorrow));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void returnBook() throws IOException
{
String returnedBook;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to retrun a book."//promts the user to enter 0
+ "\nEnter 3 to go back");//prompts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the book title.
returnedBook = br.readLine();
System.out.println(lib.returnBook( returnedBook));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
}
会员Class:
import java.util.*;
import java.io.*;
/**
* Write a description of class Member here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Member
{
private List<StudentSignUp> memberList;//creates the array list
public Member()
{
memberList = new ArrayList<StudentSignUp>(); //makes the 'memberList' the array list.
}
public void newMember(StudentSignUp student)
{
memberList.add(student);//adds a student to the array list.
}
public String toString()
{
String totalmem = "\n ";
for (int i=0; i<memberList.size(); i++)
{
StudentSignUp b = memberList.get(i);
totalmem = totalmem + b.toString(); //prints out all students
}
return totalmem;
}
public String studentidlogin(String studentidlogin) {
if (studentidlogin == null) return "\n Not a member ";
for(int i = 0; i < memberList.size(); i++){
if(studentidlogin.equalsIgnoreCase(memberList.get(i).getStudentID())){
return memberList.get(i).toString();//allows user to search for a book in the system
}
}
return "\n Not a member "; //reachable only if no book found
}
}
这个:
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
你在 if 语句中做了 mem.studentidlogin 而不是 studentidlogin:
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
if(studentidlogin.equals("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
你的函数
public String studentidlogin(String studentidlogin)
接受一个字符串参数,但你是这样调用它的:
mem.studentidlogin = ("\n Not a member ")
编辑:
只是为了扩展我的答案。你还有几个问题要解决。也许阅读 java 基础知识会有所帮助。
例如
if(studentidlogin = ("\n Not a member "))
您想要比较两个字符串并使用赋值运算符 (=),
但在您的情况下,您需要 equals 方法。
String str = "anything";
if (str.equals("anything")) {
//do something
}
我认为你的意思是使用 == 运算符,但它只比较两个内存点。
您的代码:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
mem.studentidlogin
在 println()
调用中的第一个用法是正确的。由于 studentidlogin
是 Member
class 的 方法 ,因此应将其作为方法调用 - 使用与其参数匹配的括号和参数。
第二种用法不正确。您将其视为 字段 。我想也许您希望这是从上次调用返回的结果,但如果是这样,Java 就不会那样工作。
此外,我相信您正在尝试将它与字符串 "\n Not a member "
进行比较。字符串的比较应该只用 equals()
或 equalsIgnoreCase()
!
正如代码所代表的那样,看起来您正在尝试为一个字段(不存在)分配一个值,在 if
语句中只需要一个布尔值(比较将是 ==
正如我所说,无论如何你都不应该使用它,但你正在使用赋值 =
).
你有两个选择。要么再正确调用:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin().equals("\n Not a member "))
或者最好,为了避免重复所有的工作,定义一个变量:
String loginResult = mem.studentidlogin(studentidlogin);
System.out.println(loginResult);
if( loginResult.equals("\n Not a member "))
我的代码有问题。我正在尝试创建一个学生日志,在其中正确输入后将允许用户访问 memberEnter() 部分,如果错误则系统应该退出。
我一直在尝试编译代码,但它一直给我一个错误,谁能帮我解决这个问题?
我得到的错误是:
cannot find symbol - variable studentidlogin
发生错误的行是
if(mem.studentidlogin = ("\n Not a member "))
在MainSystem
的main
方法中(代码本身也有标注)
主系统代码:
import java.io.*;//imports the io package
import java.util.Scanner;//imports scanner
import java.util.*;//imports the util package
public class MainSystem {
static String fileName = null;
static Library lib = new Library();
static Scanner in = new Scanner(System.in);
static Boolean running = true;
static Member mem = new Member();//static variables that can be initialised at compile time, but can be modified at run time
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
/************* ERROR OCCURS HERE **************/
if(mem.studentidlogin = ("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
private static void newMember() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int libraryNumber;
String StudentID, Username, FullName, Address, email, PhoneNumber;
System.out.println("\nEnter Library Number: ");
libraryNumber= Integer.parseInt(br.readLine());;//prompts user to enter library number.
System.out.println("\nEnter Student ID: ");//prompts user to enter student id.
StudentID = br.readLine();
System.out.println("\nEnter Full Name: ");//prompts user to enter full name
FullName = br.readLine();
System.out.println("\nEnter Username: ");//prompts user to enter username
Username = br.readLine();
System.out.println("\nEnter E-Mail Address: ");//prompts user to enter an email address
email = br.readLine();
System.out.println("\nEnter Home Address: ");//prompts user to enter home address
Address = br.readLine();
System.out.println("\nEnter Phone Number: ");//prompts user to enter phone number
PhoneNumber = br.readLine();
StudentSignUp b = new StudentSignUp(libraryNumber, FullName, StudentID, Username, Address, email, PhoneNumber);
mem.newMember(b);
System.out.println("\nThankyou. You can now Login.: ");// prompts user that they can now sign in.
}
private static void memberEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for Search For A Book");//options available if member is enters.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved script
break;
case 1:
saveAndQuit();//saves and quits from the system
break;
case 2:
System.out.println(lib.toString());//lists all the books in the library
break;
case 3:
searchBook();//searches for a book
break;
}
}
System.exit(0);
}
private static void librairanEnter() throws IOException {
while (running) {
System.out.println("\nEnter 0 for load a library."
+ "\nEnter 1 for save and quit"
+ "\nEnter 2 for list all books in library"
+ "\nEnter 3 for add book to library"
+ "\nEnter 4 for Search For A Book"
+ "\nEnter 5 for list of current members"
+ "\nEnter 6 to borrow a book"
+ "\nEnter 7 to return a book");//options available if the librarian logs in.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the file name to load");
loadScript(in.next());//loads previously saved libraries
break;
case 1:
saveAndQuit();//saves and quits
break;
case 2:
System.out.println(lib.toString());//prints current books in library
break;
case 3:
addBook();//adds a book to the system
break;
case 4:
searchBook();//searches for a book in the system
case 5:
System.out.println(mem.toString());//list of current members
break;
case 6:
borrowBook();//allows to borrow a book.
case 7:
returnBook();//allows to return a book.
}
}
System.exit(0);//exits from the system/
}
private static void addBook() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int isbn, numcopies;
String author, title, genre;// initializes the variables
System.out.println("\nEnter ISBN: ");
isbn = Integer.parseInt(br.readLine());//prompts user to enter isbn
System.out.println("\nEnter Author: ");//prompts user to enter the auth
author = br.readLine();
System.out.println("\nEnter Title: ");//prompts user to enter title
title = br.readLine();
System.out.println("\nEnter Genre: ");//prompts user to enter genre
genre = br.readLine();
System.out.println("\nEnter Number Of Copies: ");//prompts user to enter number of copies
numcopies = Integer.parseInt(br.readLine());
Book b = new Book(isbn, author, title, genre, numcopies);//Creates the new book and adds it to the library
lib.addBook(b);
}
private static void saveAndQuit() {//saves and quits
// TODO Auto-generated method stub
System.out.println("Enter file name: ");//prompts the user to enter the file name.
fileName = in.next() + ".ser";//saves the file name
running = false;
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(lib);
fos.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void loadScript(String name) {//loads a previously saved script
// TODO Auto-generated method stub
FileInputStream fis = null;
ObjectInputStream in = null;
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
lib = (Library) in.readObject();
fis.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("\nThe file does not exist!");
}
}
private static void searchBook() throws IOException//allows searching for a book
{
String titleSearch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."////prompts the user to answer 0
+ "\nEnter 3 to go back");//promts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the title of the book
titleSearch = br.readLine();
System.out.println(lib.searchTitle( titleSearch));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void borrowBook() throws IOException
{
String titleBorrow;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to search on title."//prompts the user to answer 0
+ "\nEnter 3 to go back");//prompt the user to answer 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user ot enter the book title
titleBorrow = br.readLine();
System.out.println(lib.borrowBook( titleBorrow));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
private static void returnBook() throws IOException
{
String returnedBook;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
while(running) {
System.out.println("\nEnter 0 to retrun a book."//promts the user to enter 0
+ "\nEnter 3 to go back");//prompts the user to enter 3
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("Enter the book Title");//promts the user to enter the book title.
returnedBook = br.readLine();
System.out.println(lib.returnBook( returnedBook));
break;
case 3:
librairanEnter();//takes the user back to the librarian home page.
}
}
}
}
会员Class:
import java.util.*;
import java.io.*;
/**
* Write a description of class Member here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Member
{
private List<StudentSignUp> memberList;//creates the array list
public Member()
{
memberList = new ArrayList<StudentSignUp>(); //makes the 'memberList' the array list.
}
public void newMember(StudentSignUp student)
{
memberList.add(student);//adds a student to the array list.
}
public String toString()
{
String totalmem = "\n ";
for (int i=0; i<memberList.size(); i++)
{
StudentSignUp b = memberList.get(i);
totalmem = totalmem + b.toString(); //prints out all students
}
return totalmem;
}
public String studentidlogin(String studentidlogin) {
if (studentidlogin == null) return "\n Not a member ";
for(int i = 0; i < memberList.size(); i++){
if(studentidlogin.equalsIgnoreCase(memberList.get(i).getStudentID())){
return memberList.get(i).toString();//allows user to search for a book in the system
}
}
return "\n Not a member "; //reachable only if no book found
}
}
这个:
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
你在 if 语句中做了 mem.studentidlogin 而不是 studentidlogin:
public static void main(String[] args) throws IOException {// there has to be an input/output
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//allows the user to type in the script.
String user, password;
while (running) {
System.out.println("\nEnter 0 To login as a Librarian"
+ "\nEnter 1 to Login"
+ "\nEnter 2 to Sign Up");//first display screen in the interface.
int answer = in.nextInt();
switch (answer) {
case 0:
System.out.println("\nEnter Username: ");
user = br.readLine();
System.out.println("\nEnter Password: ");
password = br.readLine();//if '0' is selected this is displayed.
if (user.matches("enter") && (password.matches("Password")))// if 0 is entered this has to be entered.
{
librairanEnter();
break;
}
else
{
System.exit(0);
}
case 1:
String studentidlogin;
System.out.println("\nEnter Student ID: ");
studentidlogin = br.readLine();//if '1' is pressed this is displayed
System.out.println(mem.studentidlogin(studentidlogin));
if(studentidlogin.equals("\n Not a member "))
{
System.exit(0);
}else
{
memberEnter();
break;
}
case 2:
newMember();//if '2' is selected this is displayed.
break;
}
}
}
你的函数
public String studentidlogin(String studentidlogin)
接受一个字符串参数,但你是这样调用它的:
mem.studentidlogin = ("\n Not a member ")
编辑: 只是为了扩展我的答案。你还有几个问题要解决。也许阅读 java 基础知识会有所帮助。
例如
if(studentidlogin = ("\n Not a member "))
您想要比较两个字符串并使用赋值运算符 (=), 但在您的情况下,您需要 equals 方法。
String str = "anything";
if (str.equals("anything")) {
//do something
}
我认为你的意思是使用 == 运算符,但它只比较两个内存点。
您的代码:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin = ("\n Not a member "))
mem.studentidlogin
在 println()
调用中的第一个用法是正确的。由于 studentidlogin
是 Member
class 的 方法 ,因此应将其作为方法调用 - 使用与其参数匹配的括号和参数。
第二种用法不正确。您将其视为 字段 。我想也许您希望这是从上次调用返回的结果,但如果是这样,Java 就不会那样工作。
此外,我相信您正在尝试将它与字符串 "\n Not a member "
进行比较。字符串的比较应该只用 equals()
或 equalsIgnoreCase()
!
正如代码所代表的那样,看起来您正在尝试为一个字段(不存在)分配一个值,在 if
语句中只需要一个布尔值(比较将是 ==
正如我所说,无论如何你都不应该使用它,但你正在使用赋值 =
).
你有两个选择。要么再正确调用:
System.out.println(mem.studentidlogin(studentidlogin));
if(mem.studentidlogin().equals("\n Not a member "))
或者最好,为了避免重复所有的工作,定义一个变量:
String loginResult = mem.studentidlogin(studentidlogin);
System.out.println(loginResult);
if( loginResult.equals("\n Not a member "))