输入学生java程序的数量

时间:2014-04-17 15:35:45

标签: java arrays object input

我需要帮助制作一个程序,将用户的int输入作为学生人数。目前,如果我想要更多学生,我必须在代码中手动添加学生。我添加了我的其他课程。请尽可能帮忙。

import java.util.Scanner;

// the name of our class its public
public class ClassArray {

 //void main
       public static void main (String[] args){

        //declare class
        Student[] s = new Student[2];
        s[0] = new Student();
        s[1] = new Student();

        //call functions

        s[0].getdata();
        s[1].getdata();

        s[0].finalmark();
        s[1].finalmark();

        s[0].finalgrade();
        s[1].finalgrade();

        System.out.printf("Name\tDefinitive\tLetter\tTest 1\tTest 2\tAssignments\tFinalExam \n");
        s[0].print();
        s[1].print();
       }
    }           
 }

        //declare class
    public static class Student {

        //declare variables.
        private Double finalmark;
        private int test1,test2,assignments1,finalexam;
        private String studentname,finalgrade;


        //functions should be public if needed to access from other class
        public void getdata()
                {
                //print message to enter numbers
                Scanner input = new Scanner(System.in);
                System.out.println("Enter name of student:");
                studentname = input.next();

                while (!studentname.matches("[a-zA-Z]+")) { // Checks to see if only letters are used in the name

                    System.out.println("Please re-enter your name, use alphabets only");
                    studentname = input.nextLine(); // if anything other than letters are used, the user must re-enter his/her name using letters
                   }

                System.out.println("Enter mark test 1 for student:");
                test1 = input.nextInt();

                while (test1 > 100 || test1 < 0){

                    System.out.println("Please enter a double value between 0 and 100");
                    while(!input.hasNextInt()){
                        input.next();
                    }

                    test1 = input.nextInt();
                }

                System.out.println("Enter mark test 2 for student:");
                test2 = input.nextInt();

                while (test2 > 100 || test2 < 0){

                    System.out.println("Please enter a double value between 0 and 100");
                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      test2 = input.nextInt();
                }

                System.out.println("Enter mark assignments for student:");
                assignments1 = input.nextInt();

                while (assignments1 > 100 || assignments1 < 0){

                    System.out.println("Please enter a double value between 0 and 100");

                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      assignments1 = input.nextInt();
                }  

                System.out.println("Enter mark final exam for student:");
                finalexam = input.nextInt();
                while ( finalexam > 100 ||  finalexam < 0){

                    System.out.println("Please enter a double value between 0 and 100");

                    while(!input.hasNextInt()){
                       input.next() ;
                    }
                      finalexam = input.nextInt();
                }


            }




         public void finalmark(){

             finalmark = (test1 * 0.15) + (test2 * 0.25) + (assignments1 * 0.25) + (finalexam * 
         0.35);

     }

         public void finalgrade()
               {
            if(finalmark >= 100)
                 finalgrade="A+";
               else if(finalmark >= 90)
                  finalgrade="A+";
               else if(finalmark >= 80)
                 finalgrade="A";
               else if(finalmark >= 75)
                  finalgrade="B+";
               else if(finalmark >= 70)
                  finalgrade="B";
               else if(finalmark >= 65)
                   finalgrade="C+";
               else if(finalmark >= 60)
                   finalgrade="C";
               else if(finalmark >= 50)
                   finalgrade="D";
               else 
                    finalgrade="F";
         } 



        public void print(){

            System.out.printf("%s\t%.2f\t%s\t%d\t%d\t%d\t\t%d\n", studentname, finalmark,              
            finalgrade, test1, test2, assignments1, finalexam);

    }


      }

3 个答案:

答案 0 :(得分:2)

这样的事情:

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Number of Students:\t");
int numStudents = Integer.parseInt(scanner.nextLine());

您的完整代码将是:

import java.util.Scanner;

public class ClassArray {

   public static void main (String[] args){

       Scanner scanner = new Scanner(System.in);

       System.out.print("Enter Number of Students:\t");
       int numStudents = Integer.parseInt(scanner.nextLine());


       Student[] s = new Student[numStudents];

       for(int i = 0; i < numStudents; i++ ){

            s[i] = new Student();
            s[i].getdata();
            s[i].finalmark();
            s[i].finalgrade();

       }

       System.out.printf("Name\tDefinitive\tLetter\tTest 1\tTest 2\tAssignments\tFinalExam \n");

       //Here it will iterate and print out the stored data as soon as the user has finished adding it.
       for(int j = 0; j < numStudents; j++ ){
         s[j].print();
    }

 }

答案 1 :(得分:1)

简单地说,

 import java.util.Scanner;

 public class ClassArray {

    public static void main (String[] args) {

       Scanner input= new Scanner(System.in); // create Scanner object

       System.out.print("Enter The Number of Students: ");

       int numOfStudents = input.nextInt(); // input an integer value

       // do whatever you like

   }// Ends main
}

这里我创建了一个类Scanner的对象作为输入,我通过类Scanner(输入)的对象调用方法nextInt()

答案 2 :(得分:0)

请参阅此帖子了解用户输入:How can I get the user input in Java?

在示例2中,您也不应该使用已定义为具有一定数量元素的数组。相反,请考虑对象类型为Student的ArrayList,为您的目的可以接受任意数量的学生。

ArrayList<Student> s = new ArrayList<Student>();
//Example add student
Student student1 = new Student();
s.add(student1);

请参阅此帖子了解ArrayList:Java: ArrayList of String Arrays