课程理解
在上课之后对类的理解和用处有了更多的理解,知道类的三个基本组件是类名、属性、方法,以及类与类之间有着六种关系泛化,实现,依赖,关联,聚合,组合,其中依赖是运用最广的。在这一次作业上运用之后感觉受益匪浅,有了类图在写代码的时候逻辑比较不会混乱,知道这一步以及下一步要做些什么,和以前像无头蚂蚁一样完全不同。
设计实现
首先在控制台实现是三个类:Dice,Gift,Test,基于面向对象将Gift分开写,Dice类、Gift类与Test类之间是继承关系。后面添加了GUI类实现图形化界面。
代码说明
码云地址:https://gitee.com/cyx5369/GuZi
Dice.java
import java.util.Random;
import java.util.Scanner;
public class Dice {
private int faceValue;
private int maxValue;
public void roll() {
this.faceValue = (int) (Math.random()*maxValue+1);
}
public int getfaceValue(){
return faceValue;
}
public void setMaxValue(int maxValue){
this.maxValue = maxValue;
}
public void outValue(){
System.out.println(faceValue);
}
}
Gift.java
import java.util.Random;
public class gift {
private int num;
public void rollGift() {
this.num = (int) (Math.random()*4+1);
}
public int getNum(){
return num;
}
}
Test.java
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Test {
public String j;
public String s;
public static void main(String[] args) throws FileNotFoundException{
GUI one = new GUI();
one.launchGui();
int flag=0;
Scanner input = new Scanner(System.in);
Test test = new Test();
System.out.println(test.Student());
test.question();
flag = input.nextInt();
if(flag == 1){
System.out.println(test.gift());
}else {System.out.println("答错啦!");} //1为正确,其他数字为错误。
}
public String Student(){
Dice a = new Dice();
a.setMaxValue(40);
a.roll();
s= "请"+a.getfaceValue()+"号同学回答问题";
return s;
}//选择学生
public void question(){
Dice q =new Dice();
q.setMaxValue(3);
q.roll();
if(q.getfaceValue() == 1){
System.out.println("如果我和你妈同时掉河里你救谁?");
}
else if(q.getfaceValue() == 2){
System.out.println("你妈知道你是傻子吗");
}
else if(q.getfaceValue() == 3){
System.out.println("今天看十九大的直播了吗");
}
}//选择问题
public String gift(){
gift g = new gift();
g.rollGift();
if(g.getNum()==1){
j= "奖你一个平底锅";
}
else if(g.getNum()==2){
j="奖你一把大砍刀";
}
else if(g.getNum()==3){
j="奖你一把98k";
}
else if(g.getNum()==4){
j="奖你10Q币";
}
return j;
}
}//选择奖励
GUI.java
import java.awt.;
import java.awt.event.;
import javax.swing.;
import javax.swing.event.;
import java.util.*;
import java.util.List;
public class GUI implements MouseMotionListener, MouseListener {
private JFrame f;
private JButton b1;
private JButton b2;
private TextField inf;
private TextField ouf;
private JLabel l;
public String fs;
public String el;
public String i;
public GUI () {
// 构造Gui
f = new JFrame("提问");
b1 = new JButton("抽礼物");
b2 = new JButton("抽同学");
inf = new TextField("",30);
ouf = new TextField("",50);
l = new JLabel(" 请单击");
}
public void launchGui() throws java.io.FileNotFoundException {
f.add(b1,BorderLayout.EAST);
f.add(b2,BorderLayout.WEST);
f.add(inf, BorderLayout.NORTH);
f.add(ouf, BorderLayout.SOUTH);
f.add(l, BorderLayout.CENTER);
// 添加监听
b1.addMouseMotionListener(this);
b1.addMouseListener(this);
b2.addMouseMotionListener(this);
b2.addMouseListener(this);
// 设定大小尺寸
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭构架时退出运行
f.setSize(600, 500);
f.setVisible(true);
//分别对按钮b1,b2创建监听
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s = "已完成" ;
b1.setText(s);
fs = inf.getText();
ouf.setText(i);
Test g = new Test();
i = g.gift();
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s = "已完成" ;
b2.setText(s);
fs = inf.getText();
ouf.setText(i);
Test g = new Test();
i = g.Student();
}
});
}
// 建立鼠标点击监听
public void mouseClicked(MouseEvent e) {
fs = inf.getText();
ouf.setText(i);
}
/* private static class AlphaComparator implements Comparator {
public int compare(Object e1, Object e2) {
String word1 = (String) ((Map.Entry) e1).getKey();
String word2 = (String) ((Map.Entry) e2).getKey();
return word1.compareTo(word2);
}
}
private static class FreqComparator implements Comparator {
public int compare(Object e1, Object e2) {
Integer freq1 = (Integer) ((Map.Entry) e1).getValue();
Integer freq2 = (Integer) ((Map.Entry) e2).getValue();
return freq2.compareTo(freq1);
}
} */
// 除了需使用的监听方法,都必须写入
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
}
测试运行
小结
这一次的作业控制台方面不是很难,花了30分钟左右就完成了。但在GUI方面因为时间紧迫(拖延症)在最后才决定用上学期java写过的GUI带入,没有实现将骰子具体化但是也将将完成了作业的目标。