如何在给定的java程序中最后完整地显示输入的学生信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20898347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:06:29  来源:igfitidea点击:

how to display entered students info completely at last in given java program

java

提问by Mani Beginner

without using array.. if number of students = 2 and i'm giving two students information using getdata() and finally i want to display student 1 and student 2 informations. but here only it displays last entered student information i.e student 2 information... can you solve this...!

不使用数组.. 如果学生人数 = 2 并且我使用 getdata() 为两名学生提供信息,最后我想显示学生 1 和学生 2 的信息。但这里只显示最后输入的学生信息,即学生 2 的信息……你能解决这个问题吗……!

import java.util.Scanner;

public class StudInfo
{
int regno;
String name,course;
int eng,tam,mat,tot;
double avg;
//*
void getdata()
{
Scanner ip=new Scanner(System.in);
System.out.print("\n\n Enter Roll No.\t:\t");
regno=ip.nextInt();
System.out.print("\n Enter Name   \t:\t");
name=ip.next();
System.out.println();
System.out.print(" Enter Marks [English, Tamil and Maths Marks out of 100] : ");
eng=ip.nextInt();
tam=ip.nextInt();
mat=ip.nextInt();
}
//*
void display()
{
tot=eng+tam+mat;
avg=tot/3;
System.out.println("\n"+"\n Roll No.\t:\t"+regno+
    "\n Name   \t:\t"+name+
    "\n English\t:\t"+eng+
    "\n Tamil   \t:\t"+tam+
    "\n Maths   \t:\t"+mat+
    "\n Total   \t:\t"+tot+
    "\n Average\t:\t"+avg);


System.out.print(" Grade   \t:\t");
if((eng>=50)&&(tam>=50)&&(mat>=50))
{
if(avg>=91) System.out.print("O");
else if(avg>=81) System.out.print("A");
else if(avg>=71) System.out.print("B");
else if (avg>=61) System.out.print("C");
else if(avg>=51) System.out.print("D");
else System.out.print("Fail");
}
else System.out.print("Fail");
System.out.println();
}
//*
public static void main(String arg[])
{
StudInfo s=new StudInfo();
int maxs,nos;
Scanner ip1=new Scanner(System.in);
System.out.print("\nEnter No. of Students : "); 
nos=ip1.nextInt();
for(maxs=1;maxs<=nos;maxs++) 
{
System.out.println("\nWelcome to Student database");
System.out.print("\n Enter Student [ "+maxs+" ] details");
s.getdata();
}
s.display();
}
}

i want to display entered number of students info.. but here displaying last entered student info.. can you solve this...

我想显示输入的学生信息数量..但这里显示最后输入的学生信息..你能解决这个问题...

回答by doomsdaymachine

try this:

尝试这个:

public static void main(String[] args) {
    int maxs, nos;
    Scanner ip1 = new Scanner(System.in);
    System.out.print("\nEnter No. of Students : ");
    nos = ip1.nextInt();
    StudInfo[] studInfos = new StudInfo[nos];
    for (maxs = 0; maxs < nos; maxs++) {
        System.out.println("\nWelcome to Student database");
        System.out.print("\n Enter Student [ " + (maxs + 1) + " ] details");
        studInfos[maxs] = new StudInfo();
        studInfos[maxs].getdata();
    }
    for (StudInfo s : studInfos) {
        s.display();
    }
}

hope this helps.

希望这可以帮助。

回答by Malav

Change your main method with below code

使用以下代码更改您的主要方法

public static void main(String arg[])
{
    List<StudInfo> students = new ArrayList<StudInfo>();
    int maxs,nos;
    Scanner ip1=new Scanner(System.in);
    System.out.print("\nEnter No. of Students : "); 
    nos=ip1.nextInt();
    for(maxs=1;maxs<=nos;maxs++) 
    {
        System.out.println("\nWelcome to Student database");
        System.out.print("\n Enter Student [ "+maxs+" ] details");
        StudInfo s = new StudInfo();
        s.getdata();
        students.add(s);
    }
    for (StudInfo s : students) {
        s.display();
    }
}

This will print all the student list.

这将打印所有学生列表。

In your code you are just storing the last student details in the sreference. This will store all the StudInfodetails in the studentslist

在您的代码中,您只是将最后一个学生的详细信息存储在sreference 中。这将存储列表中的所有StudInfo详细信息students