一个很小很小的JAVA程序
来源:百度知道 编辑:UC知道 时间:2024/05/21 22:13:01
写一个JAVA的程序, 它可以自己从1到20中选出一个数字 然后user有3次机会猜。 每次输入时 电脑会告诉你高了还是低了。 猜了3次后 还没答对 会显示电脑赢 游戏结束。
如果user输入了20以上的数 会出现错误提示 必须20以内
import java.util.Random;
import javax.swing.JOptionPane;
public class Guess {
int []a=new int[3];//存放随机数
int count=0;//猜对次数
Random r=new Random();
Guess(){
MyThread mt=new MyThread();
mt.start();//启动线程
for(int i=0;i<a.length;i++){
String input=JOptionPane.showInputDialog("请输入一个正整数:");
if("".equals(input)||input==null){
continue;
}
int m=Integer.parseInt(input);
if(m==a[i]){
JOptionPane.showMessageDialog(null, "回答正确");
count++;
}else if(m>a[i]){
JOptionPane.showMessageDialog(null, "大了");
}else {
JOptionPane.showMessageDialog(null, "小了");
}
}
if(count==0){
JOptionPane.showMessageDialog(null, "你输了,游戏结束");
}else{
JOptionPane.showMessageDialog(null, "你猜对了"+count+"次");
}
}