1
public class BannerUser
{
private int [] userId = new int[20];
private int [] courseNumber = new int[20];
private final String [] courseName={"Math" , "History", "English", "Art", "Science"};
public int getUserId() {
int [] tempUser = new int[userId.length];
for (int i = 0; i < userId.length; i++)
{
tempUser[i]=userId[i];
}
return tempUser;
}
public void setUserId(int userId) {
for (int i = 0; i < 20; i++)
this.userId[i]=userId;
}
public void displayCourses() {
for (int i=0; i < courseName.length; i++){
courseNumber[i]=i + 1;
System.out.println(courseNumber[i] +"\t"+ courseName[i]);
}
}
public void registration (int courseNumber){
if (courseNumber < courseName.length)
{
for (int i=0; i < 20; i++)
{
this.courseNumber[i]=courseNumber;
}
}
public void displayArrays()
{
System.out.println("Student ID\tCourseNumber");
for (int row=0;row<userId.length;row++){
System.out.print(userId[row] +"\t"+ courseNumber[row]);}
}
import java.util.Scanner;
import javax.swing.JOptionPane;
public class BannerUserClient {
public static void main (String[] args) {
BannerUser bannerUser = new BannerUser();
Scanner scan=new Scanner(System.in);
boolean done=false;
int userType;
int userId;
int courseNumber;
//int [] userId = new int[20];
for (int i =0;i<20;i++)
{
System.out.println("Enter user ID:");
userId=scan.nextInt();
bannerUser.setUserId(userId);
do{
System.out.println("enter\n(1) for faculty services\n(2) for student services:");
userType=scan.nextInt();
if (userType==1) {
System.out.println("Under Construction");
System.exit(0);
}
else if (userType==2) {
done=true;
bannerUser.displayCourses();
System.out.println("Enter a course number");
courseNumber=scan.nextInt();
bannerUser.registration(courseNumber);
//bannerUser.
}
else
//System.out.println("invalid input!");
bannerUser.displayArrays();
} while(!done);
}
}
}
很抱歉,如果這看起來不好的用戶輸入更新Java數組。基本上,我在我的學校開始編程課程的介紹,我一直在困住過去的幾個單元。對於這個項目,我必須創建兩個獨立的類:私有的和公共的。我需要從用戶那裏接受20次輸入來分配學生ID並更新數組userId。在他們輸入他們的ID後,他們需要從5個不同的選擇中選擇一個班級。他們選擇的類應該更新array courseNumber。畢竟,我需要顯示userId和courseNumber值。數組最後不打印,我甚至不確定數組是否正在更新。任何幫助將不勝感激。謝謝!
你有沒有試過運行你的程序呢? – Makky
它運行但終止而不打印陣列。 – user2579000