import javax.swing.*
import java.awt.BorderLayout
import java.awt.Container
import java.awt.event.*
public class SmallGame extends JFrame {
private Random r
private final String[] box = {"剪刀","石头","布"纯历}
private JComboBox choice
private JTextArea ta
private JLabel lb
private int win=0
private int loss=0
private int equal=0
public SmallGame() {
setTitle("Small Game")
initial()
pack()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
setLocation(400, 300)
setVisible(true)
}
public void initial() {
r = new Random()
choice = new JComboBox()
for(int i=0i<box.lengthi++) {
choice.addItem(box[i])
}
ta = new JTextArea(3, 15)
ta.setEditable(false)
JButton okBut = new JButton("出招")
okBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
ta.setText(getResult())
lb.setText(getTotal())
}
})
JButton clearBut = new JButton("清除分数")
clearBut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
ta.setText("")
win=0
loss=0
equal=0
lb.setText(getTotal())
}
})
lb = new JLabel(getTotal())
JPanel choicePanel = new JPanel()
choicePanel.add(choice)
choicePanel.add(okBut)
choicePanel.add(clearBut)
JScrollPane resultPanel = new JScrollPane(ta)
JPanel totalPanel = new JPanel()
totalPanel.add(lb)
Container contentPane = getContentPane()
contentPane.setLayout(new BorderLayout())
contentPane.add(choicePanel, BorderLayout.NORTH)
contentPane.add(resultPanel, BorderLayout.CENTER)
contentPane.add(totalPanel, BorderLayout.SOUTH)
}
public String getResult() {
String tmp = ""
int boxPeop = choice.getSelectedIndex()
int boxComp = getBoxComp()
tmp += "你出:\t" + box[boxPeop]
tmp += "\并亏n电脑出:做蔽搜\t" + box[boxComp]
tmp += "\n结果:\t" + check(boxPeop, boxComp)
return tmp
}
public int getBoxPeop(String str) {
return choice.getSelectedIndex()
}
public int getBoxComp() {
return r.nextInt(3)
}
public String check(int boxPeop, int boxComp) {
String result=""
if(boxPeop == (boxComp+1)%3) {
result="你赢了!"
win++
}
else if(boxPeop == boxComp) {
result="平"
equal++
}
else {
result="你输了!"
loss++
}
return result
}
public int getPoint() {
return (win-loss)*10
}
public String getTotal() {
return "赢:" + win + " 平:" + equal + " 输:" + loss + " 得分:" + getPoint()
}
public static void main(String[] args) {
SmallGame game = new SmallGame()
}
}
货不多说直接代码public class car {
private String car_number
private float car_price
private float rebate//折扣
public float getRebate() {
return rebate
}
public void setRebate(float rebate) {
this.rebate = rebate
}
public String getCar_number() {
return car_number
}
public void setCar_number(String car_number) {
this.car_number = car_number
}
public float getCar_price() {
return car_price
}
public void setCar_price(float car_price) {
this.car_price = car_price
}
//设置唤碧价格参数
public void carString() {
car_price=1
car_number="100"
rebate=1
}
/差神/修改价格
public void modify(float price){
car car=new car()
car.setRebate(price)
}
public static void main(String agrs[]) {
car car =new car()
car.carString()/虚链亏/设置参数
System.out.println(car.getRebate())
car.modify((float)0.8)//修改
System.out.println(car.getRebate())
}
}
哈哈 我昨天刚做了这个作业 你够幸运import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.math.*
public class Homework10_3 {
public static void main(String args[]){
MathWindow win=new MathWindow()
}
}
class MathWindow extends JFrame{
JTextField text1,text2,text3
JPanel ps,pn
MathWindow(){
text1=new JTextField(10)
text2=new JTextField(10)
text3=new JTextField(10)
JButton button1,button2,button3,button4
button1=new JButton("加")
button2=new JButton("减")
button3=new JButton("乘")
button4=new JButton("除")
ps=new JPanel()
pn=new JPanel()
pn.add(text1)
pn.add(text2)
pn.add(text3)
ps.add(button1)
ps.add(button2)
ps.add(button3)
ps.add(button4)
add(pn,BorderLayout.CENTER)
add(ps,BorderLayout.SOUTH)
setBounds(100,100,370,150)
setVisible(true)
validate()
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.add(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text1.setText(null)
text2.setText(null)
}
}
})
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.subtract(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
n2=n1.multiply(n2)
text3.setText(n2.toString())
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String s1=text1.getText()
String s2=text2.getText()
try{ BigInteger n1=new BigInteger(s1)
BigInteger n2=new BigInteger(s2)
if(n2.toString()=="0"){
text3.setText("除数不能为0")
}
else
{
n2=n1.divide(n2)
text3.setText(n2.toString())
}
}
catch(NumberFormatException ee){
text3.setText("请输入数字字符")
text1.setText(null)
text2.setText(null)
}
}
})
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)