2013-03-02 3 views
1

학생 데이터에 대한 입력을 사용자가 취하려고합니다. 먼저, 얼마나 많은 학생들이 데이터를 입력하고 있는지 묻습니다. 그런 다음 코드는 사용자가 첫 번째 질문에 대해 입력 한 정확한 학생 수에 대한 데이터를 사용자에게 묻습니다.배열에서 목록 표시, 숫자 데이터 입력

다음은 내 코드의 시작 부분입니다. 초기 변수 다음에 사용자 입력을받는 데 문제가 있습니다. 변수를 가져와야한다고 사용자가 5을 입력하면 학생 이름과 성적을 입력하기 위해 사용자에게 5 번 프롬프트해야합니다. 좋아요 :

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

Student 2 last name: 

배열을 사용해야하므로 사용자 입력을 올바르게 얻는 방법을 알아야합니다. 제 생각에는

+0

* 나는 데 문제 * : WHICH 문제? 또한 int에 성을 저장하는 방법은 무엇입니까? –

답변

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

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(); 
     } 
    } 
} 
, 어쨌든 그것은 당신의 디자인을 결정하는 당신까지, 배열 사이의 연결을 처리 할 수있는 좋은 방법이 아닙니다. 그렇게하고 싶다면 @Mikhail Vladimirov의 제안이 필요합니다.

한편으로는 필요에 맞게 클래스를 디자인하고 클래스의 개체를 배열이나 목록에 저장하십시오. 메인 클래스에서

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를를 사용하는 것이 좋습니다.

먼저 getters() & setters()를 사용하여 학생 세부 정보를 저장하는 모델 클래스를 만들 수 있습니다. 사용자로부터 입력을 읽고 다음과 같이 당신은 당신의 StudentScoresApp을 만들 수 있습니다,

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; 
    } 
} 

다음 : 그것은 다음과 같이 보일합니다

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 코딩 감사 ...