简体   繁体   English

如何在Java中修复此错误

[英]How can I fix this error in Java

I wrote this Code, and the code is running but I have faced problem in the result 我写了这段代码,代码正在运行,但是结果却遇到了问题

package calcavrage;

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


public class InfoStudent {

    int avrage;
    int Grade = 0;
    String NameOfStudent;
    ArrayList<String> StudentNames = new ArrayList<>();
    ArrayList<Integer> Grade1 = new ArrayList<>();

    public void infoStudent()
    {
     Scanner s = new Scanner(System.in);
     System.out.println("Enter Number Of Studens");
     int NoOfStudents = s.nextInt();
     for (int i = 0; i < NoOfStudents; i++)
     {
         System.out.println("Enter Name Of Student");
         NameOfStudent= s.next();
         StudentNames.add(NameOfStudent);
         System.out.println(" Enter Student Grade");
         Grade = Grade+ s.nextInt();
         Grade1.add(Grade);
     }
     avrage = Grade / NoOfStudents; 
     System.out.println("The Avrage is = " + avrage);
     System.out.println("The names of the student is " + StudentNames + " And His Grade " + Grade1); 
     }
}

The result which appears like this if I execute the code with 3 students is: 如果我与3个学生一起执行代码,则出现的结果是:

Enter Number Of Studens
3
Enter Name Of Student
aa
 Enter Student Grade
85
Enter Name Of Student
ww
 Enter Student Grade
98
Enter Name Of Student
qq
 Enter Student Grade
77
The Avrage is = 86
The names of the student is [aa, ww, qq] And His Grade [85, 183, 260] 

The result that I want like this: 我想要这样的结果:

Enter Number Of Studens
3
Enter Name Of Student
aa
 Enter Student Grade
85
Enter Name Of Student
ww
 Enter Student Grade
98
Enter Name Of Student
qq
 Enter Student Grade
77
The Avrage is = 86
The names of the student is [aa, ww, qq] And His Grade [85, 98, 77] 

How can I get this result? 我怎么能得到这个结果?

You should be adding individual student grades to your list instead of partial sums : 您应该将个别学生的成绩添加到列表中,而不是部分和:

 for (int i = 0; i < NoOfStudents; i++)
 {
     System.out.println("Enter Name Of Student");
     NameOfStudent= s.next();
     StudentNames.add(NameOfStudent);
     System.out.println(" Enter Student Grade");
     int g = s.nextInt(); // read a single grade into a variable
     Grade = Grade + g; // add it to the sum
     Grade1.add(g); // add the single grade to the list
 }

If you give your variables more descriptive names, it will be easier to avoid such errors. 如果为变量赋予更多描述性名称,则避免此类错误会更容易。 Grade should be gradeTotal , Grade1 should be gradeList , etc... Grade应该是gradeTotalGrade1gradeList ,等...

In order to display each student name with his grade, you'll need to iterate over your lists : 为了显示每个学生的姓名和年级,您需要遍历列表:

for (int i = 0; i < NoOfStudents; i++)
    System.out.println("The name of the student is " + StudentNames.get(i) + " and his grade is " + Grade1.get(i));

You can do it as: 您可以按照以下方式进行操作:

for (int i = 0; i < NoOfStudents; i++)
{
    System.out.println("Enter Name Of Student");
    NameOfStudent= s.next();
    StudentNames.add(NameOfStudent);
    System.out.println(" Enter Student Grade");
    int stuGrade = s.nextInt()
    Grade = Grade+stuGrade;
    Grade1.add(stuGrade);
 }

This gives you the solution as you wanted. 这为您提供了所需的解决方案。

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

public class InfoStudent {

int avrage;
int Grade = 0;
int sum=0;
String NameOfStudent;
ArrayList<String> StudentNames = new ArrayList<String>();
ArrayList<Integer> Grade1 = new ArrayList<Integer>();

public void infoStudent()
{
 Scanner s = new Scanner(System.in);
 System.out.println("Enter Number Of Studens");
 int NoOfStudents = s.nextInt();
 for (int i = 0; i < NoOfStudents; i++)
 {
     System.out.println("Enter Name Of Student");
     NameOfStudent= s.next();
     StudentNames.add(NameOfStudent);
     System.out.println(" Enter Student Grade");

     Grade = s.nextInt();
     sum=sum+Grade;
     Grade1.add(Grade);

 }
 avrage = sum / NoOfStudents; 
 System.out.println("The Avrage is = " + avrage);
 System.out.println("The names of the student is " + StudentNames + " And His Grade " + Grade1); 
 }
public static void main(String args[]){
    InfoStudent is=new InfoStudent();
    is.infoStudent();
}
}

result: 结果:

Enter Number Of Studens
3
Enter Name Of Student
aa
 Enter Student Grade
1
Enter Name Of Student
bb
 Enter Student Grade
2
Enter Name Of Student
cc
 Enter Student Grade
3
The Avrage is = 2
The names of the student is [aa, bb, cc] And His Grade [1, 2, 3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM