我现在不是太懂random函数的应用,向学下,就设计了个猜拳的代码,一直错,谁有代码借鉴下,谢谢了!!!!!!!
解决方案 »
- tomcat报错
- 再问内部类中的一个例子
- java开发网站用一套什么工具(学生做,不需要太复杂,也不要过简单。)
- 新手----一道小题有点看不懂啊
- 关于class analyzer
- 编译好的java 程序不能运行,惨惨惨!!!!!.请教各位!!(在线)
- 搜索引擎
- 在编译Entity Bean时,总报这个错,请大家帮我一把, (在线等待中)
- 如何获得一个JFrame的大小,不算边框,就是JFrame中空白的大小?
- JAVA插入数据库的一个中文问题!!特急,谢谢各位了
- 下午没事,初学者写了一个操作数据库的类,直接获取记录集,通过设置参数以及调用存储过程等例子,适合初学者~
- java 怎么调用 vf数据库文件
public math2(){n=0;}
public void setn()
{
double x;
x=Math.random();//生成随机数
x=x*10;
n=Math.round(x);
n=n%3;
}
public String caiquan()
{
if(n==0)return("锤子");
else if(n==1)return("剪刀");
else return("布");
}
public static void main(String [] args)
{
math2 m=new math2();
String str=new String();
m.setn();
str=m.caiquan();
System.out.println(str); }}
private static final int SCISSOR = 0;
private static final int STONE = 1;
private static final int FRABRIC = 2;
private JButton scissorButton = new JButton("Scissor");
private JButton stoneButton = new JButton("Stone");
private JButton frabricButton = new JButton("不!");
private JTextArea memo = new JTextArea();
public ScissorStoneFrabric() {
init();
}
private void init() {
setLayout(null);
setVisible(true);
setSize(260, 330);
Container cp = getContentPane();
scissorButton.setVisible(true);
stoneButton.setVisible(true);
frabricButton.setVisible(true);
scissorButton.setSize(80, 20);
stoneButton.setSize(80, 20);
frabricButton.setSize(80, 20);
scissorButton.setLocation(5,5);
stoneButton.setLocation(90, 5);
frabricButton.setLocation(175,5);
JScrollPane sp = new JScrollPane(memo,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
sp.setVisible(true);
sp.setSize(260, 300);
memo.setSize(260, 300);
sp.setLocation(0, 30);
scissorButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int userVal = -1;
if (ae.getSource() == scissorButton) userVal = 0;
if (ae.getSource() == stoneButton) userVal = 1;
if (ae.getSource() == frabricButton) userVal = 2;
// JOptionPane.showMessageDialog(ScissorStoneFrabric.this, ""+userVal);
int comVal = ((int)(Math.random() * 1000)) % 3 ;
if ((userVal == comVal))
memo.append("Draw game!");
else
if (userVal == ((comVal + 1) % 3))
memo.append("You win!");
else
memo.append("You cheap!!!!!!!");
memo.append("\n");
}
});
stoneButton.addActionListener(scissorButton.getActionListeners()[0]);
frabricButton.addActionListener(scissorButton.getActionListeners()[0]);
cp.add(scissorButton);
cp.add(stoneButton);
cp.add(frabricButton);
cp.add(sp);
}
}
花了小小時間幫你寫好了,自己把包加好