这是我第一次尝试用Java创建对象数组。我的代码混乱不堪,充满了愚蠢的初学者错误。
package javaapplication62;
import java.util.Scanner;
public class JavaApplication62 {
public static void main(String[] args) {
while (true) {
Scanner in = new Scanner(System.in);
System.out.println("1.Enroll Student");
System.out.println("2.Offer Course");
String name = in.next();
int choice = in.nextInt();
if (choice == 1) {
student_class[] studentList = new student_class[500];
studentList[0] = new student_class();
while (true) {
System.out.print(" Enter name of the student: ");
studentList[0].setName(in.next());
System.out.println("");
System.out.print(" The student is given the unique ID = " + studentList[0].getUniqueStudentID() + " and the number of students = " + studentList[0].getNumberOfStudents());
System.out.print("> and the number of students : ");
studentList[0].setnumberOfStudents(in.nextInt);
System.out.println("");
System.out.print(" Enter the e-mail address of the student: ");
studentList[0].setemail(in.next);
System.out.print("Enter the semester number of the student: ");
studentList[0].setsemesterNumber(in.nextInt);
System.out.print(" Add another student (Y/N): ");
char add = in.next().charAt(0);
if (add == 'Y' || add == 'y') {
}
if (add == 'N' || add == 'n') {
break;
}
}
} else if (choice == 2) {
while (true) {
Course[] courseOfferedList;
courseOfferedList = new Course[500];
courseOfferedList[0] = new Course();
System.out.println("Enter the course code : ");
courseOfferedList[0].setCourseCode(in.next());
System.out.println(" Enter the course title: ");
courseOfferedList[0].setCourseTitl(in.next());
System.out.println(" Enter the credit hours of the course: ");
courseOfferedList[0].setCreditHours(in.nextInt());
System.out.print(" Add another course (Y/N): ");
char add2 = in.next().charAt(0);
if (add2 == 'Y' || add2 == 'y') {
}
if (add2 == 'N' || add2 == 'n') {
break;
}
}
}
}
}
}
学生班。
public class student_class {
private String name;
private int uniqueStudentID;
private String email;
private int mobileNumber;
private int semesterNumber;
static int numberOfStudents = 0;
public student_class() {
name = null;
uniqueStudentID = 0;
email = null;
mobileNumber = 0;
semesterNumber = 0;
numberOfStudents++;
}
public void setName(String name) {
this.name = name;
}
public void setUniqueStudentID(int uniqueStudentID) {
this.uniqueStudentID = uniqueStudentID;
}
public void setEmail(String email) {
this.email = email;
}
public void setMobileNumber(int mobileNumber) {
this.mobileNumber = mobileNumber;
}
public void setSemesterNumber(int semesterNumber) {
this.semesterNumber = semesterNumber;
}
public void setNumberOfStudents(int numberOfStudents) {
this.numberOfStudents = numberOfStudents;
}
public String getName() {
return name;
}
public int getUniqueStudentID() {
return uniqueStudentID;
}
public String getEmail() {
return email;
}
public int getMobileNumber() {
return mobileNumber;
}
public int getSemesterNumber() {
return semesterNumber;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
void setName() {
}
void setuniqueStudentID() {
}
void setnumberOfStudents() {
}
void setsemesterNumber() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setmobileNumber() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void setemail() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
课程类别:
public class Course {
private int courseCode;
private String courseTitl;
private int creditHours;
static int numberOfCourses = 0;
public int getCourseCode() {
return courseCode;
}
public void setCourseCode(int courseCode) {
this.courseCode = courseCode;
}
public String getCourseTitl() {
return courseTitl;
}
public void setCourseTitl(String courseTitl) {
this.courseTitl = courseTitl;
}
public int getCreditHours() {
return creditHours;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
public static int getNumberOfCourses() {
return numberOfCourses;
}
public static void setNumberOfCourses(int numberOfCourses) {
Course.numberOfCourses = numberOfCourses;
}}
每当我运行它时,都会显示此错误:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at javaapplication62.student_class.setName(student_class.java:11)
at javaapplication62.JavaApplication62.main(JavaApplication62.java:29)
Java Result:
这就是我想要的输出
>用于输入,<用于输出
{Display the menu, Choose the menu item}
> 1
> Enter name of the student: john
< The student is given the unique ID = 1 and the number of students = 1
> Enter the e-mail address of the student: [email protected]
> Enter the semester number of the student: 3
> Add another student (Y/N): Y
> Enter name of the student: Ibrahim
< The student is given the unique ID = 2 and the number of students = 2
> Enter the e-mail address of the student: [email protected]
> Enter the semester number of the student: 3
> Add another student (Y/N): N
{Display the menu, Choose the menu item}
> 2
> Enter the course code: CPCS203
> Enter the course title: Programming 2
> Enter the credit hours of the course: 4
> Add another course (Y/N): Y
> Enter the course code: CPCS204
> Enter the course title: Data Structures
> Enter the credit hours of the course: 3
> Add another course (Y/N): N
{Display the menu, Choose the menu item}
您的大多数错误都在JavaApplication62
课堂上。我已经纠正了它们,以获得所需的所需输出。您所有的课程仍然有很多问题。我将由您自行纠正。
查看注释以获取更正。
/**
* Created by Ninan John J P on 6/30/2016.
*/
import java.util.Scanner;
public class JavaApplication62 {
public static void main(String[] args) {
while (true) {
Scanner in = new Scanner(System.in);
System.out.println("1.Enroll Student");
System.out.println("2.Offer Course");
//String name = in.next(); Not required. This will want a text input before proceeding further.
int choice = in.nextInt();
if (choice == 1) {
student_class[] studentList = new student_class[500];
studentList[0] = new student_class();
while (true) {
System.out.print(" Enter name of the student: ");
studentList[0].setName(in.next());
System.out.println("");
System.out.print(" The student is given the unique ID = " + studentList[0].getUniqueStudentID() + " and the number of students = " + studentList[0].getNumberOfStudents());
/* System.out.print("> and the number of students : ");
//studentList[0].setnumberOfStudents(in.nextInt);
studentList[0].setNumberOfStudents(in.nextInt());
*/ //This is not required. You have already done this in the previous print statment.
System.out.println("");
System.out.print(" Enter the e-mail address of the student: ");
//studentList[0].setemail(in.next); you are calling the wrong method. Also in.next should be a method.
studentList[0].setEmail(in.next());
System.out.print("Enter the semester number of the student: ");
// studentList[0].setsemesterNumber(in.nextInt); // same as prev error
studentList[0].setSemesterNumber(in.nextInt());
System.out.print(" Add another student (Y/N): ");
char add = in.next().charAt(0);
if (add == 'Y' || add == 'y') {
}
if (add == 'N' || add == 'n') {
break;
}
}
} else if (choice == 2) {
while (true) {
Course[] courseOfferedList;
courseOfferedList = new Course[500];
courseOfferedList[0] = new Course();
System.out.println("Enter the course code : ");
//courseOfferedList[0].setCourseCode(in.next()); // next() returns a string. You need nextInt() here.
courseOfferedList[0].setCourseCode(in.nextInt());
System.out.println(" Enter the course title: ");
courseOfferedList[0].setCourseTitl(in.next());
System.out.println(" Enter the credit hours of the course: ");
courseOfferedList[0].setCreditHours(in.nextInt());
System.out.print(" Add another course (Y/N): ");
char add2 = in.next().charAt(0);
// if (add2 == 'Y' || add2 == 'y') { // not needed
// }
if (add2 == 'N' || add2 == 'n') {
break;
}
}
}
}
}
}
另外,我还没有改善您的缩进。您也应该看看。
这将为您提供您在问题中提到的输出。但是我认为您应该完全重构很多代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句