public class Music { String name; int times; public Music(String name, int times){ this.name = name; this.times = times; } public void play(int i){//i为播放时长 System.out.println("音乐"+ name + "正在播放," + "播放时长为" + i + ",剩余时长为" + (times - i)); } public void getInfo(){ System.out.println("音乐名称:"+ name); System.out.println("音乐时长:"+ times); } public static void main(String[] args) { Music m1 = new Music("JAVA进行曲", 12); m1.play(10); m1.getInfo(); } }10. 输出结果为:101,100,101,101
public class Method { public double method(double a, double b){ double c = a + b; return c; } public double method(double a, int b){ double c = a + b; return c; } public static void main(String[] args) { Method m = new Method(); System.out.println(m.method(m.method(10.0, 20.0), 100)); } }12.代码
public class Employee { String name; int sex; int age; String position; double salary; public Employee(String name,int sex,int age,String position,double salary){ this.name = name; this.sex = sex; this.age = age; this.position = position; this.salary = salary; } public Employee(String name,int sex,int age){ this.name = name; this.sex = sex; this.age = age; } public Employee(String position,double salary){ this.position = position; this.salary = salary; } public static void main(String[] args) { Employee e1 = new Employee("张三", 1, 12, "职员", 2000); Employee e2 = new Employee("张三", 1, 12); Employee e3 = new Employee("职员", 2000); } }13. 代码
class Circle { public String findArea(double radius){ return "半径为:"+ radius +",面积为:"+Math.PI * radius *radius; } } public class PassObject { public void printAreas(Circle c, int times){ for(int i = 1; i <= times; i++){ System.out.println(c.findArea(i)); } } public static void main(String[] args) { PassObject p = new PassObject(); p.printAreas(new Circle(), 5); } }自己写的
public class Tom { int sum = 0; public int win(int[] computer, int[] tom){ for(int i = 0; i < tom.length; i++){ System.out.println("电脑:" + computer[i] + " ,Tom:" + tom[i]); if( (computer[i] == 0 && tom[i] == 2) || (computer[i] == 1 && tom[i] == 0)|| (computer[i] == 2 && tom[i] == 1)){ System.out.print("赢一次:"); sum++; } } System.out.println("Tom赢的次数为:" + sum); return sum; } public static void main(String[] args) { Tom t = new Tom(); int[] computer= new int[]{0,1,2,1,2,0}; int[] tom= new int[]{1,2,1,0,1,1}; t.win(computer, tom); } }老师写的
import java.util.Random; import java.util.Scanner; // 测试类,主类 public class MoraGame { // 测试 public static void main(String[] args) { // 创建一个玩家对象 Tom t = new Tom(); // 用来记录最后输赢的次数 int isWinCount = 0; // 创建一个二维数组,用来接收局数,Tom出拳情况以及电脑出拳情况 int[][] arr1 = new int[3][3]; int j = 0; // 创建一个一维数组,用来接收输赢情况 String[] arr2 = new String[3]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { //比赛3次 // 获取玩家出的拳 System.out.println("请输入你要出的拳(0-拳头,1-剪刀,2-布):"); int num = scanner.nextInt(); t.setTomGuessNum(num); int tomGuess = t.getTomGuessNum(); arr1[i][j + 1] = tomGuess; // 获取电脑出的拳 int comGuess = t.computerNum(); arr1[i][j + 2] = comGuess; // 将玩家猜的拳与电脑做比较 String isWin = t.vsComputer(); arr2[i] = isWin; arr1[i][j] = t.count; // 对每一局的情况进行输出 System.out.println("========================================="); System.out.println("局数t玩家的出拳t电脑的出拳t输赢情况"); System.out.println(t.count + "t" + tomGuess + "tt" + comGuess + "tt" + t.vsComputer()); System.out.println("========================================="); System.out.println("nn"); isWinCount = t.winCount(isWin); } scanner.close(); // 对游戏的最终结果进行输出 System.out.println("局数t玩家的出拳t电脑的出拳tt输赢情况"); for (int a = 0; a < arr1.length; a++) { for (int b = 0; b < arr1[a].length; b++) { System.out.print(arr1[a][b] + "ttt"); } System.out.print(arr2[a]); System.out.println(); } System.out.println("你赢了" + isWinCount + "次"); } } // Tom类 class Tom { // 核心代码 // 玩家出拳的类型 int tomGuessNum; //0,1,2 // 电脑出拳的类型 int comGuessNum; //0,1,2 // 玩家赢的次数 int winCountNum; // 比赛的次数 int count = 1; //一共比赛3次 public void showInfo() { //.... } public int computerNum() { Random r = new Random(); comGuessNum = r.nextInt(3); // 方法 返回 0-2的随机数 // System.out.println(comGuessNum); return comGuessNum; } public void setTomGuessNum(int tomGuessNum) { if (tomGuessNum > 2 || tomGuessNum < 0) { //抛出一个异常, 李同学会写,没有处理 throw new IllegalArgumentException("数字输入错误"); } this.tomGuessNum = tomGuessNum; } public int getTomGuessNum() { return tomGuessNum; } public String vsComputer() { //比较巧 if (tomGuessNum == 0 && comGuessNum == 1) { return "你赢了"; } else if (tomGuessNum == 1 && comGuessNum == 2) { return "你赢了"; } else if (tomGuessNum == 2 && comGuessNum == 0) { return "你赢了"; } else if (tomGuessNum == comGuessNum){ return "平手"; } else { return "你输了"; } } public int winCount(String s) { count++; //控制玩的次数 if (s.equals("你赢了")) { //统计赢的次数 winCountNum++; } return winCountNum; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)