大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
第一题: 元素的复制
成都创新互联是一家专业提供南通企业网站建设,专注与网站制作、成都网站设计、HTML5建站、小程序制作等业务。10年已为南通众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
int[] scores = {91,85,98,62,78,93};
int[] newScores=Arrays.copyOfRange(scores, 0, 5);//复制元素, 左开右闭区间[0,5)
System.out.println(Arrays.toString(newScores));//调用数组工具类的方法转成字符串并打印
}
}
第二题: 这题虽然使用集合更方便 , 但却是非常好的一维数组的训练题目.
解法一: 集合解决 随机产生7个不重复的数字很简单
import java.util.HashSet;
import java.util.Random;
public class NumberTest {
public static void main(String[] args) {
HashSetInteger set= new HashSetInteger();//元素不可重复的无序集合
Random rd=new Random();//随机产生器
while(set.size()7) {
set.add(rd.nextInt(36)+1);//产生1~36的随机数
//如果元素重复, 那么添加不上去
}
System.out.println(set);
}
}
解法二:一维数组 ,解决产生7个数字, 并升序排列
int[] nums 数组存储1~36个数组
boolean[] flags 数组存储的是和nums数组一一对应的true或者false,如果使用了就标记为true.,如果没有使用标记为false,
例如 随机产生了一个下标0 ,那么查看flags[0] ,如果是true, 那么说明该元素已经使用了,重新产生一个随机数, 如果是false ,那么表示nums[0]没有被使用
具体代码如下(稍微留个尾巴, 就是中不中的判断, 可以把两个数组都升序排序,然后元素一一比较,全部相同就是中了)
import java.util.Arrays;
import java.util.Random;
public class NumberDemo {
public static void main(String[] args) {
int[] nums= new int[36];//长度为36的数组 ,默认全是0
for (int i = 0; i nums.length; i++) {//利用for循环赋值1~36
nums[i]=i+1;
}
boolean[] flags=new boolean[nums.length];//长度和nums相同的数组,默认值全是false ,表示全部没有使用过
//用boolean值表示对应的nums里的元素是否被使用
int[] result=new int[7];//存储结果
Random rd = new Random();
for (int i = 0; i result.length; i++) {
int temp=rd.nextInt(nums.length);//随机产生下标
//System.out.println(Arrays.toString(result));
if(flags[temp]) {//如果已经被使用,那么i-1,并在此循环
i--;
//System.out.println("号码"+nums[temp]+"已经存在.再次循环");
}else {
result[i]=nums[temp];
flags[temp]=true;//标记true表示已经使用了
}
}
System.out.println("原始排序:"+Arrays.toString(result));
Arrays.sort(result);//升序排列
System.out.println("升序排列:"+Arrays.toString(result));
}
}
下面是一个存储班级三十名学生基本信息的 Java 代码示例:
import java.util.ArrayList;
public class Student {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
public class Classroom {
private ArrayListStudent students;
public Classroom() {
this.students = new ArrayListStudent();
}
public void addStudent(Student student) {
students.add(student);
}
public ArrayListStudent getStudents() {
return students;
}
}
public class Main {
public static void main(String[] args) {
Classroom classroom = new Classroom();
// 添加 30 名学生的基本信息
classroom.addStudent(new Student("Tom", 18, "Male"));
classroom.addStudent(new Student("Alice", 19, "Female"));
// ...
// 此处省略 28 名学生的信息
// 获取所有学生的信息
ArrayListStudent students = classroom.getStudents();
for (Student student : students) {
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Gender: " + student.getGender());
}
}
}
该代码定义了两个类:Student 类表示一个学生,包含了学生的姓名、年龄和性别等信息;Classroom 类表示一个班级,包含了一个学生的列表,并提供了添加学生和获取学生列表的方法。
在 Main 类的 main 方法中,我们首先实例化一个 Classroom 对象,然后依次添加 30 名学生的信息。最后,我们调用 getStudents 方法获取所
完成了,希望能帮到你
刚开始会叫你输入编号选择功能
import java.io.*;
public class student {
public static void main(String args[]) throws IOException{
int[] stud = {77,99,55,46,82,75,65,31,74,85};
System.out.println("请选择功能:");//输入编号选择功能
System.out.println("1、输入学号,查询该学生成绩:");
System.out.println("2、输入成绩,查询学生学号:");
System.out.println("3、输入学号,删除该学生成绩");
System.out.println("请选择编号:");
BufferedReader td = new BufferedReader(new InputStreamReader(System.in));
String temp = td.readLine();
int choice = Integer.valueOf(temp);
if(choice == 1){//一为查询学生成绩
System.out.println("请输入学号:");
BufferedReader sd = new BufferedReader(new InputStreamReader(System.in));
String temp_sd = sd.readLine();
int No = Integer.valueOf(temp_sd);
System.out.print("学号为 "+No+" 的学生成绩为: " + stud[No-1] +"分");
}
if(choice == 2){//二为查询学生编号
System.out.println("请输入成绩:");
BufferedReader sd = new BufferedReader(new InputStreamReader(System.in));
String chengji = sd.readLine();
int temp_cj = Integer.valueOf(chengji);
for(int i=0;istud.length;i++){
if(temp_cj == stud[i]){
System.out.print("成绩为 "+ temp_cj+ "的学生的学号为: "+(i+1));
}
}
}
if(choice == 3){//三为删除操作
System.out.println("请输入学号:");
BufferedReader sd = new BufferedReader(new InputStreamReader(System.in));
String temp_sd = sd.readLine();
int No = Integer.valueOf(temp_sd);
stud[No-1]=0;//直接赋值为0,不删除学生
System.out.print("学号为 "+No+" 的学生成绩为: " + stud[No-1] +"分");
}
}
}
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
Student.java文件代码如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "学号:"+no+",姓名:"+name+",成绩:"+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代码如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","张三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","赵六",73.5);
sArray[4]=new Student("005","孙七",66);
System.out.println("这些学生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
import java.util.Comparator;
public class Student implements ComparableStudent {
private int no;
private String name;
private String sex;
private int roomNo;
private double score;
public Student(int no, String name, String sex, int roomNo, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.roomNo = roomNo;
this.score = score;
}
public Student(int no, String name, String sex, double score) {
this.no = no;
this.name = name;
this.sex = sex;
this.score = score;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getRoomNo() {
return roomNo;
}
public void setRoomNo(int roomNo) {
this.roomNo = roomNo;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public int compareTo(Student o) {
if (this.no o.no) return 1;
else if (this.no o.no) return -1;
else return 0;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", roomNo=" + roomNo +
", score=" + score +
'}';
}
}
//性别比较器类
class SexComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getSex().compareTo(o2.getSex()) 0) return 1;
else if (o1.getSex().compareTo(o2.getSex()) 0) return -1;
else return 0;
}
}
//寝室号比较器类
class RoomNoComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getRoomNo() o2.getRoomNo()) return 1;
else if (o1.getRoomNo() o2.getRoomNo()) return -1;
else return 0;
}
}
//入学成绩比较器类
class ScoreComparator implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
if (o1.getScore() o2.getScore()) return 1;
else if (o1.getScore() o2.getScore()) return -1;
else return 0;
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1, "jack", "boy", 90);
Student s2 = new Student(5, "jack", "boy", 90);
Student s3 = new Student(4, "jack", "boy", 90);
Student s4 = new Student(2, "jack", "boy", 90);
ListStudent studentList=new ArrayList();
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Collections.sort(studentList);
System.out.println(Arrays.toString(studentList.toArray()));
}
}
lListStudent students = new ArrayListStudent();
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 对象的形式放入集合中了 接下来 给分吧 哇咔咔