ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PAT 1004 JAVA编写

2022-01-20 21:30:17  阅读:155  来源: 互联网

标签:PAT String int stu score JAVA 1004 public name


PAT 1004 JAVA编写

PAT 1004 JAVA编写


文章目录


一、题目理解

一开始感觉很简单,就是认为是数组比大小,具体是Student对象里score属性的对比。但是在实践的时候出现各种问题。问题应该是**对象数组的创建,并对对象属性的赋值**。

二、难点解决

经查阅资料,以下两种方式

1.方法一

代码如下(示例):

public class Student{
	String name;
	int age;
}

public class StudentTest{
	public static void main(String[] args){
		Student[] stu = new Studnet[5];
		Student stu0 = new Student();
		stu0.name = "陈平安";
		stu0.age = 12;
		stu[0] = stu0;
	}
}

2.方法二

代码如下(示例):

public class Student{
	String name;
	int age;
}

public class StudentTest{
	public static void main(String[] args){
		Student[] stu = new Studnet[5];
		stu[0] = new Student();
		stu[0].name = "陈平安";
		stu[0].age = 18;
	}
}

关于代码

import java.util.Scanner;
public class Main {
    private String name;
    private String sID;
    private int score;

//    public String getName() {
//        return name;
//    }
//    public void setName(String name) {
//        this.name = name;
//    }
//    public String getsID() {
//        return sID;
//    }
//    public void setsID(String sID) {
//        this.sID = sID;
//    }
//    public int getScore() {
//        return score;
//    }
//    public void setScore(int score) {
//        this.score = score;
//    }

    public static void sortByScore(Main []stu,int n){
        Main min,max;
        min = stu[0];
        max = stu[0];
        for(int i = 1; i < n;i++){
            if(min.score>stu[i].score){
                min = stu[i];
            }
            if(max.score<stu[i].score){
                max = stu[i];
            }
        }
        System.out.println(max.name+" "+max.sID);
        System.out.println(min.name+" "+min.sID);
    }

    public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        Main[] stu = new Main[n];
        String temp = "";
        for(int i = 0;i < n;i++){
            temp = sc.nextLine();
            String [] student = temp.split(" ");
            stu[i] = new Main();
            stu[i].name = student[0];
            stu[i].sID = student[1];
            stu[i].score = Integer.parseInt(student[2]);
        }
        sc.close();
       	sortByScore(stu, n);
    }
}

代码来源

[https://www.pianshen.com/article/86601207410/]

思考

首先,观察题目,必须要有一个Student类,成员变量包括:姓名,学号,成绩。其次还应该有main主函数,sort排序函数,鉴于OJ,它们必须写在同一个文件中。

一些方法函数

split();字符串的分割,返回值是String[]	

Integer.parseInt():转换为int型数组

标签:PAT,String,int,stu,score,JAVA,1004,public,name
来源: https://blog.csdn.net/qq_43497887/article/details/122609539

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有