第一题:
class ArraysApps{
public static int selMin(int[] arr1){ //找最小值
int min=arr1[0];
for (int i = 1; i < arr1.length; i++) {
if(arr1[i]<min){
min=arr1[i];
}
}
return min;
}
public static void delete(int[] arr){
//先排序
Arrays.sort(arr);
System.out.println("升序排序后的数组:"+Arrays.toString(arr));
//删除相同元素
int len= arr.length;
int i=0; //i和j类似于双指针的思想进行遍历
int j=1; //i是用来赋值的指针,j是用来探索的指针
while(j<len){ //遍历原数组
if(arr[i]==arr[j]){ //如果元素相同
j++; //继续往前探索
}
else{ //元素不相同
arr[i+1]=arr[j]; //赋值
i++; //两个指针都加1
j++;
}
}
//打印
System.out.print("删除相同元素后的数组:");
for (int k = 0; k < i+1; k++) {
System.out.print(arr[k]+" ");
}
System.out.println();
}
public static void insert(int[] arr,int le,int x){
//先排序
Arrays.sort(arr);
System.out.println("升序排序后的数组:"+Arrays.toString(arr));
//插入元素
int[] arr1=new int[arr.length+1]; //创建比原数组大1的新数组
int flag=1; //用来判断是是否已经插入,1代表没插,0代表已经插入了
double sum=0.0; //求平均数的总和
for (int i = 0; i < arr1.length; i++) {
if(i==le){ //如果遍历到要插入的下标
arr1[i]=x;
sum+=arr1[i];
flag=0; //修改flag表示已经插入了
}
else if(flag==1){ //还没有插入
arr1[i]=arr[i];
sum+=arr1[i];
}
else{ //已经插入了
arr1[i]=arr[i-1]; //说明原数组少插入了一个
sum+=arr1[i];
}
}
System.out.println("插完后的数组:"+Arrays.toString(arr1));
String s1=String.format("%.2f",sum/arr1.length); //保留两位小数
System.out.println("平均数是:"+s1);
}
public static int[] arrSum(int[][] arr2) {
int[] arrSum = new int[arr2.length]; //将每行求和的值赋给这个新数组
int sum = 0;
int i=0;
for (int[] arr1 : arr2) { //遍历
for (int x : arr1) {
sum += x;
}
arrSum[i++] = sum;
sum = 0;
}
return arrSum;
}
}
public class J54 {
public static void main(String[] args) {
int[] arr1 = new int[]{5, 3, 1, 2, 5, 3, 3};
int[][] arr2 = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 9}};
//用哪个就解除注释
//System.out.println("最小数是:" + ArraysApps.selMin(arr1)); //求最小值
//ArraysApps.delete(arr1); //删除相同元素
ArraysApps.insert(arr1,2,9); //插入元素
//System.out.println(Arrays.toString(ArraysApps.arrSum(arr2)));//求二维每行的和
}
}
第二题:
class WuMingFen{
//成员变量
private String ma;
private int quantity;
private boolean soup;
//构造方法
public WuMingFen(String ma,int quantity,boolean soup){ //带3个参数的构造方法
this.ma=ma;
this.quantity=quantity;
this.soup=soup;
}
public WuMingFen(String ma,int quantity){ //带2个参数的构造方法
this.ma=ma;
this.quantity=quantity;
}
//成员方法
public void check(){
System.out.println("面码:"+ma+",粉量:"+quantity+",是否带汤:"+soup);
}
}
public class J54 {
public static void main(String[] args) {
WuMingFen f1=new WuMingFen("牛肉",3,true);
WuMingFen f2=new WuMingFen("牛肉",2);
System.out.print("f1:");
f1.check();
System.out.print("f2:");
f2.check();
}
}
第三题:
class AddCalculator{
//成员变量
private int num1;
private int num2;
private static int problem_num;
//构造方法
public AddCalculator(int num1,int num2,int problem_num){
this.num1=num1;
this.num2=num2;
this.problem_num=problem_num;
}
//成员方法
public int getNum1(){ //得到第一个操作数
return this.num1;
}
public int getNum2(){ //得到第二个操作数
return this.num2;
}
public int add(){ //求和函数
return this.num1+this.num2;
}
public static void show(){ //静态方法打印题目总量
System.out.println("题目的数量:"+problem_num);
}
}
public class J54 {
public static void main(String[] args) {
Random r=new Random(); //创建随机数的对象
int problem_num=r.nextInt(9)+1; //生成1-10的随机数
for (int i = 1; i <= problem_num; i++) { //自动求每一道的和
AddCalculator a=new AddCalculator(r.nextInt(10),r.nextInt(10),problem_num); //随机生成两个随机数并传入题目总量
System.out.println("两个操作数分别为:"+a.getNum1()+","+a.getNum2());
System.out.println("第"+i+"道的和为:"+a.add());
}
AddCalculator.show(); //静态的不依赖于对象,所以我们可以直接通过类名调用静态方法
}
}
第四题:
class Employee{
//成员变量
private String no;
private String name;
private String birthPlace;
private String onDutyStart;
private String birthYear;
private String department;
//构造方法
public Employee(String no,String name,String birthPlace,String onDutyStart,String birthYear,String department){
this.no=no;
this.name=name;
this.birthPlace=birthPlace;
this.onDutyStart=onDutyStart;
this.birthYear=birthYear;
this.department=department;
}
//成员方法
public int compareByno1(Employee dstob){
if(this.birthPlace.compareTo(dstob.birthPlace)==0){ //如果相等
return 0;
}
else if (this.birthPlace.compareTo(dstob.birthPlace)>0) { //如果this的比dstob大
return 1;
}
else { //如果this的比dstob小
return -1;
}
}
public int compareByno2(Employee dstob){ //按照任职起始年份比较
if (Integer.parseInt(this.onDutyStart)>Integer.parseInt(dstob.onDutyStart)){ //转化为整数,先任职的排前面
return 1;
}
else {
return 0;
}
}
public void show(){ //打印各个成员变量
System.out.println(this.no);
System.out.println(this.name);
System.out.println(this.birthPlace);
System.out.println(this.onDutyStart);
System.out.println(this.birthYear);
System.out.println(this.department);
}
}
public class J54 {
public static void main(String[] args) {
Employee a=new Employee("45","zhangsan","Guangdong","2017","1976","IT");
Employee b=new Employee("44","lisi","Guangxi","2019","1971","HR");
if(a.compareByno1(b)==0){ //如果出生地一样就继续比较就职年份
int ret=a.compareByno2(b);
if(ret==1){ //如果a任职比b晚
System.out.println("b:"); //先打印b再打印a
b.show();
System.out.println("a:");
a.show();
}
else { //a任职比b早
System.out.println("a:"); //先打印a再打印b
a.show();
System.out.println("b:");
b.show();
}
}
else if(a.compareByno1(b)>0){ //如果a的出生地大于b的出生地
System.out.println("a:"); //先打印a再打印b
a.show();
System.out.println("b:");
b.show();
}
else { //如果a的出生地小于b的出生地
System.out.println("b:"); //先打印b再打印a
b.show();
System.out.println("a:");
a.show();
}
}
}