public class A{
Random ra=new Random();
private boolean a(){
//Random ra=new Random();
int a=Math.abs((ra.nextInt()))%6+1,
b=Math.abs((ra.nextInt()))%6+1;
System.out.println(""+a+"————————"+b);
//System.out.println(""+this.hashCode());
if(a==6)
return (b==a);
return false; }
private void b(){
System.out.println("You win");
}
private void c(){
System.out.println("You lost");
} public static void main(String[] args){
A aa=new A(); if(aa.a()){
aa.b();
}else{
for(int i=0 ; i<6 ; i++){ if(aa.a()){
aa.b();
return;
}
}
aa.c(); }
}}
给你参考一下
Random ra=new Random();
private boolean a(){
//Random ra=new Random();
int a=Math.abs((ra.nextInt()))%6+1,
b=Math.abs((ra.nextInt()))%6+1;
System.out.println(""+a+"————————"+b);
//System.out.println(""+this.hashCode());
if(a==6)
return (b==a);
return false; }
private void b(){
System.out.println("You win");
}
private void c(){
System.out.println("You lost");
} public static void main(String[] args){
A aa=new A(); if(aa.a()){
aa.b();
}else{
for(int i=0 ; i<6 ; i++){ if(aa.a()){
aa.b();
return;
}
}
aa.c(); }
}}
给你参考一下
import java.awt.*;
import java.awt.event.*;
/*
* Created on 2004-3-26
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*//**
* @author pan
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class TestApplet extends Applet implements ActionListener { /* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
static int counter = 0;
public void init(){
Button button = new Button("Check me");
button.addActionListener(this);
add(button); }
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
methodA();
}
public void methodA(){
int i=0,j=0;
i = (int)(Math.random() *6);
j = (int)(Math.random() * 6); if ((i==j)&(i==6))
System.out.println("You win!!");
else
counter++;
if (counter==6){
System.out.println("You Lost!!");
System.exit(0);
}
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class ramondtest extends JFrame implements ActionListener{
int number1=0;
int number2=0;
static int num=0;
JButton button = new JButton("are you ok!");
static Random rand = new Random();
Container container;
JLabel label1 = new JLabel("number1");
JLabel label2 = new JLabel("number2");
JTextField field1 = new JTextField(4);
JTextField field2 = new JTextField(4);
public ramondtest(){
super("RANDOM NUMBER");
getUI();
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void getUI(){
container =this.getContentPane();
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(label1,BorderLayout.WEST);
panel1.add(field1,BorderLayout.CENTER);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(label2,BorderLayout.WEST);
panel2.add(field2,BorderLayout.CENTER);
container.add(button,BorderLayout.NORTH);
container.add(panel1,BorderLayout.CENTER);
container.add(panel2,BorderLayout.SOUTH);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==button)
SetNumder();
} private void SetNumder(){
if(num<7){
number1 = getnumber();
number2 = getnumber();
String Num1=String.valueOf(number1);
String Num2=String.valueOf(number2);
field1.setText(Num1);
field2.setText(Num2);
if(number1==6||number2==6){
field1.setText("you wind");
field2.setText("you wind");
}
num++;
}
else{
field1.setText("you fail");
field2.setText("you fail");
}
}
private int getnumber(){
return (rand.nextInt(7));
}
public static void main(String args[]){
new ramondtest();
}
}
swing线程与非swing线程的理解,大概是考察的主要因素吧