2013-03-02 14 views
1

我試圖從用戶輸入學生數據。首先,我問用戶他們輸入數據的學生人數。然後,代碼向用戶詢問用戶輸入第一個問題的確切數量的數據。顯示數組中的列表,以數字方式輸入數據

下面是我的代碼的開始。我在初始變量之後獲取用戶輸入時遇到問題。我需要拿這個變量,比如說用戶輸入5,我需要提示用戶5次輸入學生姓名和成績。像這樣:

Student 1 last name: 
Student 1 first name: 
Student 1 grade: 

Student 2 last name: 

我必須使用一個數組,我只需要弄清楚如何正確獲取用戶輸入。

import java.util.Scanner; 

public class StudentScoresApp { 

    public static Score score = new Score(); 
    private static Student student; 

    public static void main(String[] args) { 
     System.out.println("Welcome to the Student Scores Application.\n"); 
     getStudentScores(); 
    } 

    public static void getStudentScores() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter number of students to enter: "); 
     int num = input.nextInt(); 
     int [] a = new int[num]; 
     for (int i = 0 ; i < num ; i++); { 
      System.out.print("Enter Student " + (i + 1) + " last name:"); 
      a[i] = in.nextInt(); 
     } 
    } 
} 
+0

*我有問題*:哪些問題?另外,你打算如何將姓氏存入一個int? – 2013-03-02 16:32:02

回答

1
String [] lastNames = new String [num]; 
String [] firstNames = new String [num]; 
int [] grades = new int [num]; 

for (int i = 0; i < num; i++) 
{ 
    System.out.print ("Enter Student " + (i + 1) + " last name:"); 
    lastNames [i] = in.nextLine(); 
    System.out.print ("Enter Student " + (i + 1) + " first name:"); 
    firstNames [i] = in.nextLine(); 
    System.out.print ("Enter Student " + (i + 1) + " grade:"); 
    gradess [i] = in.nextInt(); 
} 
1

在我看來,這不是一個很好的做法來處理陣列之間的關聯,反正它是由你來決定你的設計。如果你想這樣做,那麼@米哈伊爾弗拉基米羅夫的建議就是要走的路。

另一方面,只需根據需要設計一個類,並將該類的對象存儲在數組或列表中。

public class StudentScore{ 
    String firstName; 
    String lastName; 
    int grade; 

    pulbic StudnetScore(String firstName, String lastName, int grade){ 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.grade = grade; 
    } 

    //getters(), setters() 
} 

在主類:

StudentScore[] studentScores = new StudentScore[num]; 
for (int i = 0; i < studentScores.length; i++){ 
    System.out.print ("Enter Student " + (i + 1) + " last name:"); 
    String lastName = in.nextLine(); 
    System.out.print ("Enter Student " + (i + 1) + " first name:"); 
    String firstName = in.nextLine(); 
    System.out.print ("Enter Student " + (i + 1) + " grade:"); 
    int grade = in.nextInt(); 
    studentScores[i] = new StudentScore(firstName,lastName,grade); 
} 
0

,我建議你使用ArrayList來存儲Student對象。考慮下面的例子以更好地理解:

首先,您可以創建一個模型類來存儲學生的詳細信息,其中包含getters()& setters()。它必須是這個樣子:

package com.stack.overflow.works.model; 

public class Student { 

    private String firstName; 
    private String lastName; 
    private int score; 

    public Student() {} 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public String getLastName() { 
     return lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public int getScore() { 
     return score; 
    } 

    public void setScore(int score) { 
     this.score = score; 
    } 
} 

接下來,你就可以創造StudentScoresApp如下圖所示,從用戶讀取輸入:現在

package com.stack.overflow.works.main; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner; 

import com.stack.overflow.works.model.Student; 

public class StudentScoresApp { 

    public static List<Student> getStudentScores() { 
     List<Student> students = new ArrayList<Student>(); 
     Student student = null; 
     Scanner scanner = new Scanner(System.in); 
     System.out.print("Enter number of students to enter: "); 
     int numberOfStudents = scanner.nextInt(); 

     for (int i = 0; i < numberOfStudents; i++) { 
      student = new Student(); 
      System.out.print("Enter Student " + (i + 1) + " First Name:"); 
      String firstName = scanner.next(); 
      student.setFirstName(firstName); 
      System.out.print("Enter Student " + (i + 1) + " Last Name:"); 
      String lastName = scanner.next(); 
      student.setLastName(lastName); 
      System.out.print("Enter Student " + (i + 1) + " Score:"); 
      int score = scanner.nextInt(); 
      student.setScore(score); 
      students.add(student); 
     } 
     scanner.close(); 

     return students; 
    } 

    public static void displayStudentScores(List<Student> students) { 
     int i = 1; 
     for (Student student: students) { 
      System.out.println("Student " + (i) + " First Name:" + student.getFirstName()); 
      System.out.println("Student " + (i) + " Last Name:" + student.getLastName()); 
      System.out.println("Student " + (i) + " Score:" + student.getScore()); 
      i++; 
     } 
    } 

    public static void main(String[] args) { 
     System.out.println("Welcome to the Student Scores Application"); 
     System.out.println("*****************************************"); 
     List<Student> students = StudentScoresApp.getStudentScores(); 
     System.out.println(); 
     System.out.println("Displaying Student Scores:"); 
     System.out.println("*************************"); 
     StudentScoresApp.displayStudentScores(students); 
    } 

} 

,您可以運行StudentScoresApp。樣品測試結果如下圖所示:

Welcome to the Student Scores Application 
***************************************** 
Enter number of students to enter: 3 
Enter Student 1 First Name:Sandeep 
Enter Student 1 Last Name:Thulaseedharan 
Enter Student 1 Score:100 
Enter Student 2 First Name:Sathya 
Enter Student 2 Last Name:Narayanan 
Enter Student 2 Score:100 
Enter Student 3 First Name:Jayakrishnan 
Enter Student 3 Last Name:Lal 
Enter Student 3 Score:100 

Displaying Student Scores: 
************************* 
Student 1 First Name:Sandeep 
Student 1 Last Name:Thulaseedharan 
Student 1 Score:100 
Student 2 First Name:Sathya 
Student 2 Last Name:Narayanan 
Student 2 Score:100 
Student 3 First Name:Jayakrishnan 
Student 3 Last Name:Lal 
Student 3 Score:100 

希望這有助於..

謝謝you..Happy編碼...