使用for循环输入存储在数组中的字符串

时间:2015-07-31 14:14:28

标签: java arrays string loops

我试图使用for循环输入要存储在数组中的名称。它总是跳到输入等级而不是先输入名字。我无法上传图片,但结果会像这样

 //Enter number of student: 2
//Enter name of student 1:  Enter grade: 78
//Enter name of student 2:  Enter grade:   


    import java.util.*;

    public class one{

    public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);

    int n;
    double sum = 0, ave;

    System.out.print(" Enter number of student: ");
    n = keyboard.nextInt();
   //declare arrays
    String[] student = new String[n+1];
    double[] grade = new double[n+1];

   //inputs
    for(int i=1; i<student.length; i++)
    {
    System.out.print(" Enter name of student " + i + ": ");
    student[i] = keyboard.nextLine(); //input a string
    System.out.print(" Enter grade: ");
    grade[i] = keyboard.nextDouble();
    }

  //compute for average
    System.out.println();
   for(int i=1; i<grade.length; i++)
   {
       System.out.println("    " + student[i] + "   " + grade[i]);
       sum = sum + grade[i];
   }
       ave = sum/n;
       System.out.println("\n Average: " + ave);
}

0 个答案:

没有答案