我无法创建调用的对象student1并打印出我创建的菜单方法。帮助将不胜感激。这是一个调用类的驱动程序类。
import java.util.Scanner;
public class StudentDriver
{
public static void main(String[] args)
{
Student student1 = new Student();
student1 = menu();
System.out.println(student1);
}
public Student menu(String firstName, String middleName, String lastName, String eNumber, String major,
String concentration, int creditHours, int qualityHours)
{
Scanner kb = new Scanner(System.in);
int choice = 0;
Student student1 = new Student(student1);
while((choice < 9))
{
System.out.print("\nPress number to enter input: ");
System.out.print("\n-------------------------------");
System.out.println("\n1. First Name ");
System.out.println("2. Middle Name ");
System.out.println("3. Last Name ");
System.out.println("4. E-Number ");
System.out.println("5. Major ");
System.out.println("6. Concentration ");
System.out.println("7. Credit Hours ");
System.out.println("8. Quality Hours ");
System.out.println("9. Exit ");
System.out.print("What would you like to enter? ");
int option = kb.nextInt();
if(option == 1)
{
System.out.print("Please enter the first name: ");
firstName = kb.nextLine();
student1.setFirstName(firstName);
}
else if(option == 2)
{
System.out.print("Please enter the middle name: ");
middleName = kb.nextLine();
student1.setMiddleName(middleName);
}
else if(option == 3)
{
System.out.print("Please enter the last name: ");
lastName = kb.nextLine();
student1.setLastName(lastName);
}
else if(option == 4)
{
System.out.print("Please enter the E-Number: ");
eNumber = kb.nextLine();
student1.setENumber(eNumber);
}
else if(option == 5)
{
System.out.print("Please enter the major: ");
major = kb.nextLine();
student1.setMajor(major);
}
else if(option == 6)
{
System.out.print("Please enter the concentration: ");
concentration = kb.nextLine();
student1.setConcentration(concentration);
}
else if(option == 7)
{
System.out.print("\nPlease enter the credit hours: ");
creditHours = kb.nextInt();
student1.setCreditHours(creditHours);
}
else if(option == 8)
{
System.out.print("\nPlease enter the quality hours: ");
qualityHours = kb.nextInt();
student1.setQualityHours(qualityHours);
}
else
{
break;
}
}
return student1;
}
}
答案 0 :(得分:2)
1)这个构造函数没有任何意义:
Student student1 = new Student(student1);
这就是我在申请自包含示例时遇到的情况,因为您是Student
背后唯一可以看到内部逻辑的人。如果您仍然要设置所有字段,请将其更改为no-args构造函数。
Student student1 = new Student();
(当然,您还需要在Student
类定义中更改此内容)
2)使菜单成为一个返回Student
对象
public static Student menu()
3)然后,让menu()
方法的第一行设置您需要的变量:
String firstName, middleName, lastName, eNumber, major, concentration;
int creditHours, qualityHours;
下面的代码汇编并显示菜单,我认为这是你所坚持的:
import java.util.Scanner;
public class StudentDriver
{
public static void main(String[] args)
{
Student myStudent = menu();
System.out.println(myStudent);
}
public static Student menu()
{
String firstName, middleName, lastName, eNumber, major, concentration;
int creditHours, qualityHours;
Scanner kb = new Scanner(System.in);
int choice = 0;
Student student1 = new Student();
while((choice < 9))
{
System.out.print("\nPress number to enter input: ");
System.out.print("\n-------------------------------");
System.out.println("\n1. First Name ");
System.out.println("2. Middle Name ");
System.out.println("3. Last Name ");
System.out.println("4. E-Number ");
System.out.println("5. Major ");
System.out.println("6. Concentration ");
System.out.println("7. Credit Hours ");
System.out.println("8. Quality Hours ");
System.out.println("9. Exit ");
System.out.print("What would you like to enter? ");
int option = kb.nextInt();
if(option == 1)
{
System.out.print("Please enter the first name: ");
firstName = kb.nextLine();
student1.setFirstName(firstName);
}
else if(option == 2)
{
System.out.print("Please enter the middle name: ");
middleName = kb.nextLine();
student1.setMiddleName(middleName);
}
else if(option == 3)
{
System.out.print("Please enter the last name: ");
lastName = kb.nextLine();
student1.setLastName(lastName);
}
else if(option == 4)
{
System.out.print("Please enter the E-Number: ");
eNumber = kb.nextLine();
student1.setENumber(eNumber);
}
else if(option == 5)
{
System.out.print("Please enter the major: ");
major = kb.nextLine();
student1.setMajor(major);
}
else if(option == 6)
{
System.out.print("Please enter the concentration: ");
concentration = kb.nextLine();
student1.setConcentration(concentration);
}
else if(option == 7)
{
System.out.print("\nPlease enter the credit hours: ");
creditHours = kb.nextInt();
student1.setCreditHours(creditHours);
}
else if(option == 8)
{
System.out.print("\nPlease enter the quality hours: ");
qualityHours = kb.nextInt();
student1.setQualityHours(qualityHours);
}
else
{
break;
}
}
return student1;
}
}