Desperate11221 2022-04-16 16:11 采纳率: 100%
浏览 85
已结题

oj一直是答案错误50%,真想不到还有其他什么情况了,代码贴在下面

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
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")



img

img

img


这以上就是oj要求和我自己程序跑的结果了。

  • 写回答

1条回答 默认 最新

  • 嘉祐-小萝卜算子 2022-04-16 17:37
    关注

    多少题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月25日
  • 已采纳回答 4月17日
  • 创建了问题 4月16日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装