版权声明:虽然我依旧蒟蒻,但请你尊重我 :D ——陈杉菜 https://blog.csdn.net/qq_44702847/article/details/88552882
初学java 第三个星期
试着封装了一下 也不知道对不对
请求指点
文件:SecondWeekHomework
接下来开始
Dota自定义英雄属性设定
//DotaHero.java文件
public class DotaHero {
//private 私有的
private int magic;//英雄魔法值
private int agility;//英雄敏捷度
private String name;//英雄姓名
private int strength;//英雄体力值
// get和set函数 public 公有的
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getAgility() {
return agility;
}
public void setAgility(int agility) {
this.agility = agility;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMagic() {
return magic;
}
public void setMagic(int magic) {
this.magic = magic;
}
public void introduction() { //英雄性能介绍
System.out.println("The strength value of "+name+" is "+strength+".");
System.out.println("The magic value of "+name+" is "+magic+".");
System.out.println("The agility value of "+name+" is "+agility+".");
}
public void skills(String skill) { //英雄属性介绍
System.out.println("The skill of "+name+" is "+skill+".");
}
public void movements(String movement) { //移动方式
System.out.println("↑ ↓ ← →");
}
}
实现效果
简单计算器
//Calculator.java 文件
public class Calculator {
private static int num1;//第一个运算数
private static int num2;//第二个运算数
private static char op;//运算符
public static int getNum1() {
return num1;
}
public static void setNum1(int num1) {
Calculator.num1 = num1;
}
public static int getNum2() {
return num2;
}
public static void setNum2(int num2) {
Calculator.num2 = num2;
}
public static char getOp() {
return op;
}
public static void setOp(char op) {
Calculator.op = op;
}
public void start() {
System.out.println("Input two arithmetic numbers and an operator to start calculation.");
}
public void calc() {
double result=0.0;
switch(op) {
case '+': result=num1+num2; break;
case '-': result=num1-num2; break;
case '*': result=num1*num2; break;
case '/':
if(num2 != 0) {
result=num1*1.0/num2;
break;
}
else {
System.out.println("Incorrect input!"); //除数为0,提示错误
break;
}
default : System.out.println("Unrecognized Opeerator!");//提示无法识别运算符
}
System.out.println("result = "+result);//输出结果
}
}
实现效果
螺旋数组
//ratate.java文件
//不知道算不算是封装方法了
//快捷方式:选中要封装的代码->Refactor->Extract Method
public class rotate {
public static void rotateArray(int n) {
luoxuansz(n);
}
private static void luoxuansz(int n) {//n为行数
int k=1;
int[][] a =new int [15][15];//设置最多15行
for(int i=0;i<n/2;i++) {
for(int j=i;j<n-1-i;j++) {
a[i][j]=k++;
}
for(int j=i;j<n-1-i;j++) {
a[j][n-i-1]=k++;
}
for(int j=n-i-1;j>i;j--) {
a[n-i-1][j]=k++;
}
for(int j=n-i-1;j>i;j--) {
a[j][i]=k++;
}
}
if(n%2 == 1)a[n/2][n/2]=k;
for(int i=0;i<n;i++) {//格式化输出
for(int j=0;j<n;j++) {
if(j==n-1)System.out.printf("%4d\n",a[i][j]);
else System.out.printf("%4d",a[i][j]);
}
}
}
}
实现效果
嘻嘻嘻肝泄阅读