问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
package second;
import java.util.ArrayList;
import java.util.Scanner;
class Student{
String studentNumber;//记录学生编号
String studentName;
int markForMaths;
int markForEnglish;
int markForScience;
Student(String studentNumber,String studentName,int markForMaths,int markForEnglish,int markForScience)
{
this.studentName=studentName;
this.studentNumber=studentNumber;
this.markForEnglish=markForEnglish;
this.markForMaths=markForMaths;
this.markForScience=markForScience;
}
Student(String studentNumber,int markForMaths,int markForEnglish,int markForScience)
{
this.studentNumber=studentNumber;
this.markForEnglish=markForEnglish;
this.markForMaths=markForMaths;
this.markForScience=markForScience;
}
String getNumber()
{
return studentNumber;
}
}
public class StudentList{
static boolean isused(ArrayList<Student>arrayList,String a)//1
{
boolean flag=false;
for(int i=0;i<arrayList.size();i++)
{
Student stu=arrayList.get(i);
if(stu.getNumber().equals(a)) {
flag=true;
break;
}
}
return flag;
}
public static void main(String[] args)
{
ArrayList<Student>list=new ArrayList<>();
int i=0;
Scanner in=new Scanner(System.in);
int n=in.nextInt();
String f=in.nextLine();
String[] a=new String[n];
while(i<n)
{
String line =in.nextLine();
String[] Line=line.split("\\s");
switch(Line[0])
{
case "1":
a[i]= addStudent(list,Line);
break;
case "2":
a[i]=removeStudent(list,Line);
break;
case "3":
a[i]= updateStudent(list,Line);
break;
case "4":
a[i]=showaverage(list,Line);
break;
case "5":
a[i]= judgeempty(list);
break;
case "6":
a[i]= gettotal(list);
break;
}
i++;
}
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
public static String gettotal(ArrayList<Student> list) {
String m= String.valueOf(list.size());
return m;
}
public static String judgeempty(ArrayList<Student> list) {
if(list.size()==0)
{
return "List is empty";
}
else
{
return"List is not empty";
}
}
public static String showaverage(ArrayList<Student> list,String[] line) {
String b=line[1];
String m=null;
String[] a=new String[3];
int i=0;
while(i<list.size())
{
Student stu=list.get(i);
if(stu.getNumber().equals(b))
{
double average=(stu.markForEnglish+stu.markForMaths+stu.markForScience)*(1.0)/(3.0);
String result=String.format("%.1f",average);
m="Student ID:"+stu.studentNumber+"\n"+
"Name:"+stu.studentName+"\n"+
"Average Score:"+result;
break;
}
else if(i+1==list.size())
{
m="Students do not exist";
}
i++;
}
return m;
}
public static String updateStudent(ArrayList<Student> list,String[] line) {
String number=line[1];
String name= "da";
int maths=Integer.parseInt(line[2]);
int english=Integer.parseInt(line[3]);
int science=Integer.parseInt(line[4]);
String m=null;
Student stu=new Student(number,name,maths,english,science);
if(list.size()==0)
{
m="Students do not exist";
}
for(int i=0;i<list.size();i++)
{
//输入学号与集合中存储的学号进行比对如果一样进行修改,如果不一样提示。
Student stu1=list.get(i);
if(stu.getNumber().equals(stu1.getNumber()))
{
m=stu1.studentName;
stu.studentName=m;
list.set(i, stu);//修改集合中的set方法修改,直接通过下标修改。
m= "Update success";
break;
}
else
{
m= "Students do not exist";
}
}
return m;
}
public static String removeStudent(ArrayList<Student> list,String[] line) {
String m = null;
int i=0;
if(list.size()==0)
{
m= "Students do not exist";
}
String b=line[1];
if(isused(list,b))
{
for(i=0;i<list.size();i++ )
{
Student stu=list.get(i);
if(stu.getNumber().equals(b))
{
list.remove(i);
m="Delete success";
break;
}
}
}
else
{
m="Students do not exist";
}
return m;
}
public static String addStudent(ArrayList<Student> list,String[] line) {
String number=line[1];
if(isused(list,number))
{
return "Students already exist";
}
String name=line[2];
int maths=Integer.parseInt(line[3]);
int english=Integer.parseInt(line[4]);
int science=Integer.parseInt(line[5]);
Student stu=new Student(number,name,maths,english,science);
list.add(stu);
return"Add success";
}
}
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/030546690056183.png "#left")
这以上就是oj要求和我自己程序跑的结果了。