java作业

1.求数组值的和.
2.求数组中的最大值
3.对数组进行升序排序.
4.对数组进行倒序排序(也就是反转数组)
工具类要求:
a.私有化构造方法
b.不希望被继承
分析:对于整形数组和double类型数组的方法:涉及重载
a.私有化构造方法:不能创建对象
b.不希望被继承:final关键字修饰

package HelloWorld;
public final class Array { 
    public static void main(String[] args) {

    }
private Array(){  
    // 类私有化  
}  

// 1---数组求和方法(int 类型)  
public static int arraySum(int[] a) {  
    int sum = 0;
    for (int i = 0; i < a.length; i++) {  
        sum += a[i];  
    }  
    return sum;  
}  

// 1---方法重载:数组求和方法(double类型)  
public static double arraySum(double[] a) {  
    int sum = 0;  
    for (int i = 0; i < a.length; i++) {  
        sum += a[i];  
    }  
    return sum;  
}  

// 2---数组最大值的方法(int 类型)  
public static int arrayMax(int[] a) {  
    int temp = a[0]; // 参量比较  
    for (int i = 1; i < a.length; i++) {  
        if (a[i] > temp) {  
            temp = a[i];  
        }  
    }  
    return temp;  
}  

// 2---重载:数组最大值的方法(double类型),原来对数组元素进行了错误的操作  
public static double arrayMax(double[] a) {  
        double temp = a[0];  
    for (int i = 1; i < a.length; i++) {  
        if (a[i] > temp) {  
            temp = a[i];  
        }  
    }  
    return temp;  
}  

// 3---数组升序排列(int 类型),采用冒泡排序方式  
public static void arraySort(int[] a) {  
    for (int i = 0; i < a.length - 1; i++) {  
        for (int j = i + 1; j < a.length; j++) {  
            if (a[i] > a[j]) {  
                int temp = 0;  
                temp = a[j];  
                a[j] = a[i];  
                a[i] = temp;  
            }  
        }  
    }  
}  

// 3---重载:数组升序排列(double 类型),采用冒泡排序方式  
public static void arraySort(double[] a) {  
    for (int i = 0; i < a.length - 1; i++) {  
        for (int j = i + 1; j < a.length; j++) {  
            if (a[i] > a[j]) {  
                double temp = 0;  
                temp = a[j];  
                a[j] = a[i];  
                a[i] = temp;  
            }  
        }  
    }  
}  

// 4---对数组进行倒序排序(也就是反转数组)int类型  
public static void arrayIeversal(int[] a) {  
    for (int i = 0; i < a.length / 2; i++) {  
        int temp = 0;  
        temp = a[i];  
        a[i] = a[a.length - 1 - i];  
        a[a.length - 1 - i] = temp;  
    }  
}  
// 4---重载:对数组进行倒序排序(也就是反转数组)double类型  
public static void arrayIeversal(double[] a) {  
    for (int i = 0; i < a.length / 2; i++) {  
        double temp = 0;  
        temp = a[i];  
        a[i] = a[a.length - 1 - i];  
        a[a.length - 1 - i] = temp;  
    }  
}  
}  
  1. a.定义一个英雄类 Hero

属性:(全部私有,提供公共方法让外部访问)
年龄, 血量 ,攻击力,防御力
方法:
释放技能,加血.
必须至少包含一个构造方法,且该构造方法可以初始化所有四个成员变量
b.定义一个类BatMan继承Hero类
方法:
飞行(方法中输出一行打印”飞行”)
c.定义一个SuperBatMan类继承 BatMan类
方法:
重写飞行方法(方法中输出一行打印”超级飞行”)
最终分别创建BatMan对象和SuperBatMan对象,并调用飞行方法.

package HelloWorld;

public class SmallGame {
    public static void main(String[] args){
        BatMan a=new BatMan();//创建BatMan的对象
        SuperBatMan b=new SuperBatMan();//创建SuperBatMan的对象
        a.skill();
        b.skill();
    }

}
class Hero{
    private int Age=55;
    private int HP=100;
    private int ATK=30;
    private int DEF=20;
    int getAge(){
        return this.Age;
        }
    int getHP(){
        return this.HP;
    }
    int getATK(){
        return this.ATK;
    }
    int getDEF(){
        return this.DEF;
    }
    void releasing_Skill(){
         HP=HP+20;
    }
    Hero(){           //构造方法,初始化属性
        Age=this.Age;
        HP=this.HP;
        ATK=this.ATK;
        DEF=this.DEF;
    }
}
class BatMan extends Hero{
    void skill(){
        System.out.println("飞行");
    }
}
class SuperBatMan extends BatMan{
    void skill(){       //重写skill方法
        System.out.println("超级飞行");
    }
}

这里写图片描述

  1. 实现一个猜数的小游戏,随机产生一个数(a)。
    Scanner 的方式来输入一个数字,并提供反馈,
    告诉用户该输入的值比a大还是比a小,直到最终用户猜中,显示结果.
    对100以内(包括100)自然数的猜测
package HelloWorld;
import java.util.Scanner;
import java.util.Random;
public class GuessNumber {
    public static void main(String[] args) {  

        Random random = new Random();  

       final int a = random.nextInt(101);//系统随机产生数a
       Scanner sc = new Scanner(System.in); 

       for(int i=0;i<100;i++){
      System.out.println("请输入一个数:");
      int b = sc.nextInt();
  if(a>b){
      System.out.println("输入的数比a小");
  }else if(a<b){
      System.out.println("输入的数比a大");
      }else{
          System.out.println("输入的数和a一样大");
          return;
      }

  System.out.println("请继续输入一个数");

    }  
    }
    }

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值