30天学会JAVA—练习题(2021韩顺平)——Day6

9.代码

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


11.代码

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;                                                                             
                                                                                                      
/* 请编写一个猜拳的游戏 有个人 Tom,设计他的成员变量. 成员方法, 可以电脑猜拳. 电脑每次都会随机生成 0, 1, 2 0 表示 石头 1 表示剪刀 2 表示 布 并要可以显示 Tom的输赢次数(清单), 假定 玩三次. */ 
 // 测试类,主类
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 + "\t\t" + comGuess + "\t\t" + t.vsComputer());
            System.out.println("=========================================");                          
            System.out.println("\n\n");                                                               
            isWinCount = t.winCount(isWin);                                                           
        }    
        scanner.close();
                                                                                                      
        // 对游戏的最终结果进行输出 
        System.out.println("局数\t玩家的出拳\t电脑的出拳\t\t输赢情况");                                               
        for (int a = 0; a < arr1.length; a++) {
                                                          
            for (int b = 0; b < arr1[a].length; b++) {
                                                   
                System.out.print(arr1[a][b] + "\t\t\t");                                              
            }                                                                                         
                                                                                                      
            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() {
   
		//....
	}
	
    /** * 电脑随机生成猜拳的数字的方法 * @return */                                                                                               
    public int computerNum() {
                                                                           
        Random r = new Random();                                                                      
        comGuessNum = r.nextInt(3);      // 方法 返回 0-2的随机数 
        // System.out.println(comGuessNum); 
        return comGuessNum;                                                                           
    }                                                                                                 
                                                                                                      
    /** * 设置玩家猜拳的数字的方法 * @param tomGuessNum */                                                                                               
    public void setTomGuessNum(int tomGuessNum) {
                                                        
        if (tomGuessNum > 2 || tomGuessNum < 0) {
    
			//抛出一个异常, 李同学会写,没有处理
            throw new IllegalArgumentException("数字输入错误");                                             
        }                                                                                             
        this.tomGuessNum = tomGuessNum;                                                               
    }                                                                                                 
                                                                                                      
    public int getTomGuessNum() {
                                                                        
        return tomGuessNum;                                                                           
    }                                                                                                 
                                                                                                      
    /** * 比较猜拳的结果 * @return 玩家赢返回true,否则返回false */                                                                                               
    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 "你输了";                                                                             
        }                                                                                             
    }                                                                                                 
                                                                                                      
    /** * 记录玩家赢的次数 * @return */                                                                                               
    public int winCount(String s) {
                                                                      
        count++;    //控制玩的次数 
        if (s.equals("你赢了")) {
        //统计赢的次数 
            winCountNum++;                                                                            
        }                                                                                             
        return winCountNum;                                                                           
    }                                                                                                 
                                                                                                      
}  

全部评论

相关推荐

01-14 19:01
吉首大学 Java
黑皮白袜臭脚体育生:加个项目吧,一般需要两个项目一业务一轮子呢,简历统一按使用了什么技术实现了什么功能解决了什么问题或提升了什么性能指标来写
点赞 评论 收藏
分享
头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务