2014.10.23总结

  1. 分数求+-×÷
    package com.lovo;
    
    /**
     * 分数类
     * @author 何超
     *
     */
    public class Rational {
    	private int num;		// 分子
    	private int den;		// 分母
    	
    	/**
    	 * 构造器
    	 * @param num 分子
    	 * @param den 分母
    	 */
    	public Rational(int num, int den) {
    		this.num = num;
    		this.den = den;
    		normalize();
    	}
    	
    	/**
    	 * 构造器
    	 * @param str 代表一个分数的字符串
    	 */
    	public Rational(String str) {
    		String s1 = str.split("/")[0];	// 分子的字符串
    		String s2 = str.split("/")[1];	// 分母的字符串
    		this.num = Integer.parseInt(s1);
    		this.den = Integer.parseInt(s2);
    		normalize();
    	}
    	
    	/**
    	 * 构造器
    	 * @param x 一个小数
    	 */
    	public Rational(double x) {
    		this.num = (int) (x * 10000);
    		this.den = 10000;
    		simplify();
    		normalize();
    	}
    	
    	/**
    	 * public   Rational add(Rational other) { ... }
    	 * 访问修饰符  返回类型     方法名(参数列表)       { ... }
    	 * 加法
    	 * @param other 另一个分数
    	 * @return 两个分数相加的结果
    	 */
    	
    	public Rational add(Rational other) {
    		return new Rational(this.num * other.den + this.den * other.num, this.den * other.den).normalize();
    	}
    	
    	/**
    	 * 减法
    	 * @param other 另一个分数
    	 * @return 两个分数相减的结果
    	 */
    	public Rational sub(Rational other) {
    		return new Rational(this.num * other.den - this.den * other.num, this.den * other.den).normalize();
    	}
    	
    	/**
    	 * 乘法
    	 * @param other 另一个分数
    	 * @return 两个分数相乘的结果
    	 */
    	public Rational mul(Rational other) {
    		return new Rational(this.num * other.num, this.den * other.den).normalize();
    	}
    	
    	/**
    	 * 除法
    	 * @param other 另一个分数
    	 * @return 两个分数相除的结果
    	 */
    	public Rational div(Rational other) {
    		return new Rational(this.num * other.den, this.den * other.num).normalize();
    	}
    	
    	/**
    	 * 化简
    	 * @return 化简后的分数对象
    	 */
    	public Rational simplify() {
    		if(num != 0) {
    			int divisor = gcd(Math.abs(num), Math.abs(den));
    			num /= divisor;
    			den /= divisor;
    		}
    		return this;
    	}
    	
    	/**
    	 * 正规化
    	 * @return 正规化以后的分数对象
    	 */
    	public Rational normalize() {
    		if(this.num == 0) {
    			this.den = 1;
    		}
    		else if(this.den < 0) {
    			this.den = - this.den;
    			this.num = - this.num;
    		}
    		return this;
    	}
    	//必须这样写,以字符串形式输出,打印对象时自动调用
    	public String toString() {
    		return num + (den != 1? ("/" + den) : "");
    	}
    	/**
    	 * 求公约数
    	 * @param x
    	 * @param y
    	 * @return
    	 */
    	private int gcd(int x, int y) {
    		if(x > y) {
    			int temp = x;
    			x = y;
    			y = temp;
    		}
    		for(int i = x; i > 1; i--) {
    			if(x % i == 0 && y % i == 0) {
    				return i;
    			}
    		}
    		return 1;
    	}
    }
    

  2. 分数测试
    package com.lovo;
    
    import java.util.Scanner;
    
    public class Test01 {
    
    	public static void main(String[] args) {		
    		Scanner sc = new Scanner(System.in);
    		System.out.print("r1 = ");
    		String str1 = sc.next();
    		Rational r2 = new Rational(0.3);
    		
    		Rational r1 = new Rational(str1);
    		
    		System.out.printf("%s + %s + %s = %s\n", r1, r2, r1, r1.add(r2).simplify());
    		System.out.printf("%s - %s = %s\n", r1, r2, r1.sub(r2).simplify());
    		System.out.printf("%s * %s = %s\n", r1, r2, r1.mul(r2).simplify());
    		System.out.printf("%s / %s = %s\n", r1, r2, r1.div(r2).simplify());
    		
    		sc.close();
    	}
    }
    

  3. 时钟
    package com.lovo;
    
    import java.text.DecimalFormat;
    import java.util.Calendar;
    
    /**
     * 时钟类
     * @author 何超
     *
     */
    public class Clock {
    	private int hour;		// 时
    	private int minute;		// 分
    	private int second;		// 秒
    	
    	/**
    	 * 构造器(获取系统时间)
    	 */
    	public Clock() {
    		Calendar cal = Calendar.getInstance();
    		this.hour = cal.get(Calendar.HOUR_OF_DAY);
    		this.minute = cal.get(Calendar.MINUTE);
    		this.second = cal.get(Calendar.SECOND);
    	}
    	
    	/**
    	 * 构造器(获得指定的时间)
    	 * @param hour 时
    	 * @param minute 分
    	 * @param second 秒
    	 */
    	public Clock(int hour, int minute, int second) {
    		this.hour = hour;
    		this.minute = minute;
    		this.second = second;
    	}
    
    	/**
    	 * 走字(走一秒)
    	 */
    	public void go() {
    		++second;
    		if(second == 60) {
    			second = 0;
    			++minute;
    			if(minute == 60) {
    				minute = 0;
    				++hour;
    				if(hour == 24) {
    					hour = 0;
    				}
    			}
    		}
    	}
    	
    	/**
    	 * 倒计时
    	 * @return 倒计时结束返回true 否则返回false
    	 */
    	public boolean countDown() {
    		if(second > 0) {
    			--second;
    		}
    		else {
    			if(minute > 0) {
    				--minute;
    				second = 59; 
    			}
    			else {
    				if(hour > 0) {
    					--hour;
    					minute = 59;
    					second = 59;
    				}
    			}
    		}
    		
    		return hour == 0 && minute == 0 && second == 0;
    	}
    	
    	/**
    	 * 获得时间对象的字符串表示形式
    	 */
    	public String toString() {
    		DecimalFormat df = new DecimalFormat("00");		// 数字格式化器 
    		return df.format(hour) + ":" + df.format(minute) + ":" + df.format(second);
    	}
    }
    

  4. 时钟测试
    package com.lovo;
    
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    
    public class Test01 {
    	
    	private static Timer timer = null;
    
    	public static void main(String[] args) {
    		final Clock c = new Clock(0, 0, 4);
    		
    		JFrame f = new JFrame();
    		f.setTitle("时钟");
    		f.setSize(400, 200);
    		f.setResizable(false);
    		f.setLocationRelativeTo(null);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		final JLabel lbl = new JLabel("时间", JLabel.CENTER);
    		Font font = new Font("微软雅黑", Font.PLAIN, 60);
    		lbl.setFont(font);
    		lbl.setText(c.toString());
    		f.add(lbl);
    		
    		
    		f.setVisible(true);
    		
    		timer = new Timer(1000, new ActionListener() {
    			
    			 
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				boolean isOver = c.countDown();
    				lbl.setText(c.toString());
    				if(isOver) {
    					timer.stop();	// 停止计时器
    					JOptionPane.showMessageDialog(null, "时间到!!!");
    				}
    			}
    		});		// 创建一个计时器对象
    		
    		timer.start();		// 启动计时器
    	}
    }
    

  5. 猜数字游戏
    package com.lovo;
    /**
     * 猜数字游戏
     * @author hechao
     *
     */
    public class GuessMachine {
    	private int answer;
    	private String hint;
    	private int counter;
    	/**
    	 * 构造器
    	 */
    	public GuessMachine() {
    		this.reset();
    	}
    	/**
    	 * 判断输赢
    	 * @param yourAnswer//你的答案
    	 * @return//是否胜利
    	 */
    	public boolean judge(int yourAnswer) {
    		++counter;
    		if(yourAnswer == answer) {
    			hint = "恭喜你猜对了!总共猜了" + counter + "次";
    			return true;
    		}
    		else if(yourAnswer > answer) {
    			hint = "小一点";
    		}
    		else {
    			hint = "大一点";
    		}
    		return false;
    	}
    	/*
    	 * 获得示意
    	 */
    	public String getHint() {
    		return hint;
    	}
    	/**
    	 * 清空
    	 */
    	public void reset() {
    		answer = (int) (Math.random() * 100 + 1);
    		counter = 0;
    	}
    	
    }
    

  6. 猜数字游戏测试
    package com.lovo;
    
    import java.util.Scanner;
    
    public class Test02 {
    
    	public static void main(String[] args) {
    		GuessMachine gm = new GuessMachine();
    		Scanner sc = new Scanner(System.in);
    		boolean isCorrect = false;
    		do {
    			System.out.print("请输入你猜的数字: ");
    			int yourAnswer = sc.nextInt();
    			isCorrect = gm.judge(yourAnswer);
    			System.out.println(gm.getHint());
    		} while(!isCorrect);
    		
    		sc.close();
    	}
    }
    

  7. 作业:人机猜拳
    package com.lovo;
    
    /**
     * 人机猜拳
     * 
     * @author hechao
     *
     */
    public class FingerGuessing {
    	private int answer;
    	private String hint;
    	private String result;
    
    	/**
    	 * 构造器
    	 * 
    	 * @param answer
    	 *            //答案
    	 */
    	public FingerGuessing() {
    		this.reset();
    	}
    
    	/**
    	 * 判断输赢
    	 * 
    	 * @param yourAnswer
    	 *            //你的答案
    	 * @return//输false赢true
    	 */
    	public boolean judge(int yourAnswer) {
    		if (yourAnswer == 1) {
    			result = "剪刀";
    		}
    		if (yourAnswer == 2) {
    			result = "石头";
    		}
    		if (yourAnswer == 3) {
    			result = "布";
    		}
    		if ((yourAnswer == 1 && answer == 3) || (yourAnswer == 2 && answer == 1) || (yourAnswer == 3 && answer == 2)) {
    			hint = "恭喜你,你赢了!";
    			return true;
    		}else if(yourAnswer == answer){
    			hint = "真意外,你们未分胜负!";
    			return false;
    		}
    
    		else {
    			hint = "对不起,你输了!";
    		}
    		return false;
    	}
    
    	/**
    	 * 获得示意
    	 * 
    	 * @return//文字提示
    	 */
    	public String getHint() {
    		return hint;
    	}
    
    	/**
    	 * 获得文字答案
    	 * 
    	 * @return//将1,2,3转换为剪刀,石头,布
    	 */
    	public String getAnswer() {
    		return result;
    	}
    	public int reset() {
    		answer = (int) (Math.random() * 3 + 1);
    		result = "";
    		return answer;
    	}
    }
    

  8. 猜拳游戏测试
    package com.lovo;
    
    import java.util.Scanner;
    
    public class Test04 {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);		
    		FingerGuessing fg = new FingerGuessing();
    		boolean isWin = false;		
    		do {
    			System.out.println("1.剪刀 / 2.石头 / 3.布");
    			System.out.print("请选择:");
    			int yourAnswer = sc.nextInt();
    			isWin = fg.judge(yourAnswer);			
    			System.out.println("你出:" + fg.getAnswer());
    			System.out.println(fg.getHint());
    		} while (!isWin);
    		sc.close();
    	}
    }
    


    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值