十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
lListStudent students = new ArrayListStudent();
创新互联公司专业为企业提供南谯网站建设、南谯做网站、南谯网站设计、南谯网站制作等企业网站建设、网页设计与制作、南谯企业网站模板建站服务,10多年南谯做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
BufferedReader br = new BufferedReader(new FileReader("D:\student.txt"));
String tmpStr = br.readLine();
while(tmpStr != null){
int firstIndex = tmpStr.indexOf(" ");
int secondIndex = tmpStr.indexOf(" ",firstIndex + 1);
int thirdIndex = tmpStr.indexOf(" ", secondIndex + 1);
int forthIndex = tmpStr.indexOf(" ", thirdIndex + 1);
Integer stuId = Integer.parseInt(tmpStr.substring(0,firstIndex));
String stuName = tmpStr.substring(firstIndex + 1,secondIndex);
Integer stuYW = Integer.parseInt(tmpStr.substring(secondIndex + 1,thirdIndex));
Integer stuSX = Integer.parseInt(tmpStr.substring(thirdIndex + 1,forthIndex));
Integer stuYY = Integer.parseInt(tmpStr.substring(forthIndex + 1));
Student student = new Student();
student.setStuId(stuId);
student.setStuName(stuName);
student.setStuYW(stuYW);
student.setStuSX(stuSX);
student.setStuYY(stuYY);
students.add(student);
tmpStr.readLine();
}
//创建一个学生实体类 封装stuId stuName stuYW stuSx stuYY 这5个属性。。。
//已经帮你把数据拆分出来 并以Student 对象的形式放入集合中了 接下来 给分吧 哇咔咔
我也是初学写的不好
public class Student {
static int[] gradesArray={87,68,94,100,83,78,85,91,76,87};
public Student(int[]gradesArray){
}
public void printAllGrade(){
for (int i = 0; i gradesArray.length; i++) {
System.out.println("学号"+(i+1)+"成绩是"+gradesArray[i]);
}
}
public void printAvarage(){
double sum = 0;
for (int i = 0; i gradesArray.length; i++) {
sum+=gradesArray[i];
}
System.out.println("\n平均成绩是"+sum/gradesArray.length);
}
public void printMax(){
int max=gradesArray[0];
for (int i = 0; i gradesArray.length; i++) {
if(gradesArray[i]max){
max=gradesArray[i];
}
}
System.out.println("\n最高分时"+max);
}
public void printMin(){
int min=gradesArray[0];
for (int i = 0; i gradesArray.length; i++) {
if (gradesArray[i]min){
min=gradesArray[i];
}
}
System.out.println("\n最低分时"+min);
}
public void printDistribution(){
int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,j=0,k=0;
for (int i = 0; i gradesArray.length; i++) {
if(gradesArray[i]10){
a++;
}else if (gradesArray[i]=10gradesArray[i]20) {
b++;
}
else if (gradesArray[i]=20gradesArray[i]30) {
c++;
}
else if (gradesArray[i]=30gradesArray[i]40) {
d++;
}
else if (gradesArray[i]=40gradesArray[i]50) {
e++;
}
else if (gradesArray[i]=50gradesArray[i]60) {
f++;
}
else if (gradesArray[i]=60gradesArray[i]70) {
g++;
}
else if (gradesArray[i]=70gradesArray[i]80) {
h++;
}
else if (gradesArray[i]=80gradesArray[i]90) {
j++;
}
else if (gradesArray[i]=90gradesArray[i]100) {
k++;
}
}
System.out.println("\n成绩分布是");
System.out.print("0-10:");
for (int i = 0; i =a; i++) {
System.out.print("*");
}
System.out.print("10-20:");
for (int i = 0; i =b; i++) {
System.out.print("*");
}
System.out.print("20-30:");
for (int i = 0; i =c; i++) {
System.out.print("*");
}
System.out.print("30-40:");
for (int i = 0; i =d; i++) {
System.out.print("*");
}
System.out.print("40-50:");
for (int i = 0; i =e; i++) {
System.out.print("*");
}
System.out.print("50-60:");
for (int i = 0; i =f; i++) {
System.out.print("*");
}
System.out.print("60-70:");
for (int i = 0; i =g; i++) {
System.out.print("*");
}
System.out.print("70-80:");
for (int i = 0; i =h; i++) {
System.out.print("*");
}
System.out.print("80-90:");
for (int i = 0; i =j;i++) {
System.out.print("*");
}
System.out.print("90-100:");
for (int i = 0; i =k; i++) {
System.out.print("*");
}
}
public static void main(String[] args) {
Student student=new Student(gradesArray);
student.printAllGrade();
student.printAvarage();
student.printMax();
student.printMin();
student.printDistribution();
}
}
import java.util.*;
import java.io.*;
class StuMgr{
public static class Student{
public int id;
public String name;
public int age;
public Student(int id ,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString(){
return id + "," + name + "," + age;
}
}
public ListStudent stuList = new LinkedList();
public void add(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch(NumberFormatException ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
if (find(intId) != null){
System.out.println("该学号已经存在!");
return ;
}
System.out.println("请输入学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学生年龄:");
String age = sc.nextLine();
int intAge = 0;
try{
intAge = Integer.parseInt(age);
}catch(NumberFormatException ex){
System.out.println("年龄输入有误,请输入数字!");
return;
}
Student stu = new Student(intId,name,intAge);
stuList.add(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已增加");
System.out.println(stu);
System.out.println("-----------------------");
}
public void del(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch(NumberFormatException ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
Student stu = find(intId);
if ( stu == null){
System.out.println("该学号不存在!");
return ;
}
stuList.remove(stu);
store();
System.out.println("-----------------------");
System.out.println("学生信息已删除");
System.out.println(stu);
System.out.println("-----------------------");
}
public void find(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入学生学号:");
String id = sc.nextLine();
int intId = 0;
try{
intId = Integer.parseInt(id);
}catch(NumberFormatException ex){
System.out.println("学号输入有误,请输入数字!");
return;
}
Student stu = find(intId);
if ( stu == null){
System.out.println("该学号不存在!");
return ;
}
System.out.println("-----------------------");
System.out.println("查找学生信息如下");
System.out.println(stu);
System.out.println("-----------------------");
}
public Student find(int id){
for(Student stu : stuList){
if(stu.id == id){
return stu;
}
}
return null;
}
public void modify(){
store();
}
public void foreach(){
System.out.println("-----------------------");
for(Student stu : stuList){
System.out.println(stu);
}
System.out.println("-----------------------");
}
public void store(){
Iterator iterator = stuList.iterator();
File file = new File("stuList.txt");
FileWriter fw = null;
BufferedWriter writer = null;
try {
fw = new FileWriter(file);
writer = new BufferedWriter(fw);
while(iterator.hasNext()){
writer.write(iterator.next().toString());
writer.newLine();//换行
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
writer.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
StuMgr mgr = new StuMgr();
while(true){
System.out.println("请选择您要进行的操作:");
System.out.println("1:增加学生信息");
System.out.println("2:删除学生信息");
System.out.println("3:查找学生信息");
System.out.println("4:修改学生信息");
System.out.println("5:遍历学生信息");
System.out.println("6:退出");
System.out.println("-----------------------");
Scanner sc = new Scanner(System.in);
String op = sc.nextLine();
if("6".equals(op)){
return;
}
if("1".equals(op)){
mgr.add();
}
if("2".equals(op)){
mgr.del();
}
if("3".equals(op)){
mgr.find();
}
if("4".equals(op)){
mgr.modify();
}
if("5".equals(op)){
mgr.foreach();
}
}
}
}
时间仓促,还有一个modify方法没实现,留给你自己练手。
用java写的话,可以用List来实现学生管理系统:\x0d\x0a首先,管理系统是针对学生对象的,所以我们先把学生对象就写出来:\x0d\x0apackage bean;\x0d\x0apublic class Student {\x0d\x0a String name;\x0d\x0a String studentId;\x0d\x0a String sex;\x0d\x0a int grade;\x0d\x0a public Student(String name,String studentId,String sex,int grade){\x0d\x0a this.name= name;\x0d\x0a this.studentId= studentId;\x0d\x0a this.sex = sex;\x0d\x0a this.grade = grade; \x0d\x0a }\x0d\x0a public int getGrade(){\x0d\x0a return grade;\x0d\x0a }\x0d\x0a public String getName(){\x0d\x0a return name;\x0d\x0a }\x0d\x0a public String getSex(){\x0d\x0a return sex;\x0d\x0a }\x0d\x0a public void setGrade(int g){\x0d\x0a this.grade = g;\x0d\x0a }\x0d\x0a public String getStudentId(){\x0d\x0a return studentId;\x0d\x0a }\x0d\x0a}\x0d\x0a这里面定义了一些得到当前学生对象数据的一些get方法,和成绩修改的set方法,代码很简单,就不做详细的解答。\x0d\x0a就下来就是我们的正文了。\x0d\x0a虽然我们暂时不用swing来做界面,但是总得要看的过去吧,所以,先做了一个比较简单的界面:\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*欢迎来到学生管理系统 *");\x0d\x0a System.out.println("*1:增加学生 *");\x0d\x0a System.out.println("*2:删除学生 *");\x0d\x0a System.out.println("*3:修改成绩 *");\x0d\x0a System.out.println("*4:查询成绩 *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想选择的操作是:");\x0d\x0a这里可以看到,我们的是用一个1234来选择项目,说以不得不讲一下Java如何获取到键盘所输入的数据---------Scanner ,要使用这个,首先需要import进来一个包:\x0d\x0a例如这里:\x0d\x0aimport java.util.*;\x0d\x0a之后的两行代码搞定输入:\x0d\x0aScanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt();\x0d\x0a接下来就是各个功能的实现:\x0d\x0a\x0d\x0apackage test;\x0d\x0aimport java.util.*;\x0d\x0aimport bean.Student;\x0d\x0apublic class Manager {\x0d\x0a static List StudentList = new LinkedList();\x0d\x0a public static void main(String[] agrs){\x0d\x0a select(StudentList); \x0d\x0a }\x0d\x0a private static void select(List StudentList ){\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("*欢迎来到学生管理系统 *");\x0d\x0a System.out.println("*1:增加学生 *");\x0d\x0a System.out.println("*2:删除学生 *");\x0d\x0a System.out.println("*3:修改成绩 *");\x0d\x0a System.out.println("*4:查询成绩 *");\x0d\x0a System.out.println("***************");\x0d\x0a System.out.println("您想选择的操作是:");\x0d\x0a Scanner sc = new Scanner(System.in);\x0d\x0a int choice = sc.nextInt(); \x0d\x0a switch(choice){\x0d\x0a //增加学生\x0d\x0a case 1:\x0d\x0a System.out.print("请输入学生的姓名:");\x0d\x0a Scanner Sname = new Scanner(System.in);\x0d\x0a String name = Sname.nextLine();\x0d\x0a System.out.print("请输入学生的性别:");\x0d\x0a Scanner Ssex = new Scanner(System.in);\x0d\x0a String sex = Ssex.nextLine();\x0d\x0a System.out.print("请输入学生的学号:");\x0d\x0a Scanner SId = new Scanner(System.in);\x0d\x0a String studentId = SId.nextLine();\x0d\x0a System.out.print("请输入学生的成绩:");\x0d\x0a Scanner Sgrade = new Scanner(System.in);\x0d\x0a int grade = Sgrade.nextInt();\x0d\x0a StudentList.add(new Student(name,studentId,sex,grade));\x0d\x0a System.out.println("添加成功!!!!!");\x0d\x0a select(StudentList);\x0d\x0a break;\x0d\x0a //删除学生成绩\x0d\x0a case 2:\x0d\x0a System.out.print("请告诉我需要删除学生的学号:");\x0d\x0a Scanner Sid = new Scanner(System.in);\x0d\x0a String SstudentId = Sid.nextLine();\x0d\x0a boolean isfindDelete = false;\x0d\x0a for (int i = 0; i
回答于 2022-11-16
//主类EnglishTest——
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EnglishTest extends JFrame
{
TestArea testPanel=null;
Container con=null;
public EnglishTest()
{
super("模拟考试");
testPanel=new TestArea();
con=getContentPane();
con.add(testPanel,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
});
setVisible(true);
setBounds(60,40,660,460);
con.validate();
validate();
}
public static void main(String args[])
{
new EnglishTest();
}
}
//读取试题 ReadTestquestion
import java.io.*;
import java.util.*;
public class ReadTestquestion
{ String filename="",
correctAnswer="",
testContent="" ,
selection="" ;
int score=0;
long time=0;
boolean 完成考试=false;
File f=null;
FileReader in=null;
BufferedReader 读取=null;
public void setFilename(String name)
{ filename=name;
score=0;
selection="";
try {
if(in!=null读取!=null)
{
in.close();
读取.close();
}
f=new File(filename);
in=new FileReader(f);
读取=new BufferedReader(in);
correctAnswer=(读取.readLine()).trim();
String temp=(读取.readLine()).trim() ;
StringTokenizer token=new StringTokenizer(temp,":");
int hour=Integer.parseInt(token.nextToken()) ;
int minute=Integer.parseInt(token.nextToken());
int second=Integer.parseInt(token.nextToken());
time=1000*(second+minute*60+hour*60*60);
}
catch(Exception e)
{
testContent="没有选择试题";
}
}
public String getFilename()
{
return filename;
}
public long getTime()
{
return time;
}
public void set完成考试(boolean b)
{
完成考试=b;
}
public boolean get完成考试()
{
return 完成考试;
}
public String getTestContent()
{ try {
String s=null;
StringBuffer temp=new StringBuffer();
if(读取!=null)
{
while((s=读取.readLine())!=null)
{
if(s.startsWith("**"))
break;
temp.append("\n"+s);
if(s.startsWith("endend"))
{
in.close();
读取.close();
完成考试=true;
}
}
testContent=new String(temp);
}
else
{
testContent=new String("没有选择试题");
}
}
catch(Exception e)
{
testContent="试题内容为空,考试结束!!";
}
return testContent;
}
public void setSelection(String s)
{
selection=selection+s;
}
public int getScore()
{ score=0;
int length1=selection.length();
int length2=correctAnswer.length();
int min=Math.min(length1,length2);
for(int i=0;imin;i++)
{ try{
if(selection.charAt(i)==correctAnswer.charAt(i))
score++;
}
catch(StringIndexOutOfBoundsException e)
{
i=0;
}
}
return score;
}20:10 03-8-31
public String getMessages()
{
int length1=selection.length();
int length2=correctAnswer.length();
int length=Math.min(length1,length2);
String message="正确答案:"+correctAnswer.substring(0,length)+"\n"+
"你的回答:"+selection+"\n";
return message;
}
}
//考试区域TestArea
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class FileName implements FilenameFilter
{
String str=null;
FileName (String s)
{
str="."+s;
}
public boolean accept(File dir,String name)
{
return name.endsWith(str);
}
}
public class TestArea extends JPanel implements ActionListener,ItemListener,Runnable
{
Choice list=null;
JTextArea 试题显示区=null,消息区=null;
JCheckBox box[];
JButton 提交该题答案,读取下一题,查看分数;
ReadTestquestion 读取试题=null;
JLabel welcomeLabel=null;
Thread countTime=null;
long time=0;
JTextField timeShow=null;
boolean 是否关闭计时器=false,
是否暂停计时=false;
JButton 暂停或继续计时=null;
public TestArea()
{
list= new Choice();
String 当前目录=System.getProperty("user.dir");
File dir=new File(当前目录);
FileName fileTxt=new FileName("txt");
String fileName[]=dir.list(fileTxt);
for(int i=0;ifileName.length;i++)
{
list.add(fileName[i]);
}
试题显示区=new JTextArea(15,12);
试题显示区.setLineWrap(true);
试题显示区.setWrapStyleWord(true);
试题显示区.setFont(new Font("TimesRoman",Font.PLAIN,14));
试题显示区.setForeground(Color.blue);
消息区=new JTextArea(8,8);
消息区.setForeground(Color.blue);
消息区.setLineWrap(true);
消息区.setWrapStyleWord(true);
countTime=new Thread(this);
String s[]={"A","B","C","D"};
box=new JCheckBox[4];
for(int i=0;i4;i++)
{
box[i]=new JCheckBox(s[i]);
}
暂停或继续计时=new JButton("暂停计时");
暂停或继续计时.addActionListener(this);
提交该题答案=new JButton("提交该题答案");
读取下一题=new JButton("读取第一题");
读取下一题.setForeground(Color.blue);
提交该题答案.setForeground(Color.blue);
查看分数=new JButton("查看分数");
查看分数.setForeground(Color.blue);
提交该题答案.setEnabled(false);
提交该题答案.addActionListener(this);
读取下一题.addActionListener(this);
查看分数.addActionListener(this);
list.addItemListener(this);
读取试题=new ReadTestquestion();
JPanel pAddbox=new JPanel();
for(int i=0;i4;i++)
{
pAddbox.add(box[i]);
}
Box boxH1=Box.createVerticalBox(),
boxH2=Box.createVerticalBox(),
baseBox=Box.createHorizontalBox();
boxH1.add(new JLabel("选择试题文件"));
boxH1.add(list);
boxH1.add(new JScrollPane(消息区));
boxH1.add(查看分数);
timeShow=new JTextField(20);
timeShow.setHorizontalAlignment(SwingConstants.RIGHT);
timeShow.setEditable(false);
JPanel p1=new JPanel();
p1.add(new JLabel("剩余时间:"));
p1.add(timeShow);
p1.add(暂停或继续计时);
boxH1.add(p1);
boxH2.add(new JLabel("试题内容:"));
boxH2.add(new JScrollPane(试题显示区));
JPanel p2=new JPanel();
p2.add(pAddbox);
p2.add(提交该题答案);
p2.add(读取下一题);
boxH2.add(p2);
baseBox.add(boxH1);
baseBox.add(boxH2);
setLayout(new BorderLayout());
add(baseBox,BorderLayout.CENTER);
welcomeLabel=new JLabel("欢迎考试,提高英语水平",JLabel.CENTER);
welcomeLabel.setFont(new Font("隶书",Font.PLAIN,24));
welcomeLabel.setForeground(Color.blue);
add(welcomeLabel,BorderLayout.NORTH);
}
public void itemStateChanged(ItemEvent e)
{
timeShow.setText(null);
是否关闭计时器=false;
是否暂停计时=false;
暂停或继续计时.setText("暂停计时");
String name=(String)list.getSelectedItem();
读取试题.setFilename(name);
读取试题.set完成考试(false);
time=读取试题.getTime();
if(countTime.isAlive())
{
是否关闭计时器=true;
countTime.interrupt();
}
countTime=new Thread(this);
消息区.setText(null);
试题显示区.setText(null);
读取下一题.setText("读取第一题");
提交该题答案.setEnabled(false);
读取下一题.setEnabled(true);
welcomeLabel.setText("欢迎考试,你选择的试题:"+读取试题.getFilename());
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==读取下一题)
{
读取下一题.setText("读取下一题");
提交该题答案.setEnabled(true);
String contentTest=读取试题.getTestContent();
试题显示区.setText(contentTest);
消息区.setText(null);
读取下一题.setEnabled(false);
try {
countTime.start();
}
catch(Exception event)
{
}
}
if(e.getSource()==提交该题答案)
{
读取下一题.setEnabled(true);
提交该题答案.setEnabled(false);
String answer="?";
for(int i=0;i4;i++)
{
if(box[i].isSelected())
{
answer=box[i].getText();
box[i].setSelected(false);
break;
}
}
读取试题.setSelection(answer);
}
if(e.getSource()==查看分数)
{
int score=读取试题.getScore();
String messages=读取试题.getMessages();
消息区.setText("分数:"+score+"\n"+messages);
}
if(e.getSource()==暂停或继续计时)
{
if(是否暂停计时==false)
{
暂停或继续计时.setText("继续计时");
是否暂停计时=true;
}
else if(是否暂停计时==true)
{
暂停或继续计时.setText("暂停计时");
是否暂停计时=false;
countTime.interrupt();
}
}
}
public synchronized void run()
{
while(true)
{
if(time=0)
{
是否关闭计时器=true;
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
timeShow.setText("用时尽,考试结束");
}
else if(读取试题.get完成考试())
{
是否关闭计时器=true;
timeShow.setText("考试效果:分数*剩余时间(秒)="+1.0*读取试题.getScore()*(time/1000));
countTime.interrupt();
提交该题答案.setEnabled(false);
读取下一题.setEnabled(false);
}
else if(time=1)
{
time=time-1000;
long leftTime=time/1000;
long leftHour=leftTime/3600;
long leftMinute=(leftTime-leftHour*3600)/60;
long leftSecond=leftTime%60;
timeShow.setText(""+leftHour+"小时"+leftMinute+"分"+leftSecond+"秒");
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ee)
{
if(是否关闭计时器==true)
return ;
}
while(是否暂停计时==true)
{
try
{
wait();
}
catch(InterruptedException ee)
{
if(是否暂停计时==false)
{
notifyAll();
}
}
}
}
}
}