package 案例;
import java.util.Random;
import javax.swing.JOptionPane;
public class GuessNumber {
public static void main(String[] args){
int start=JOptionPane.showConfirmDialog(null,"猜数字,游戏开始","猜数字游戏?",JOptionPane.YES_NO_OPTION);
if(start==JOptionPane.YES_OPTION){
int num=(new Random().nextInt(100));
String inputValue;
int inputNum;
int i=1;
while(i<=8){
inputValue = JOptionPane.showInputDialog(null,"请输入一个1-100的整数\n共八次机会,这是第"+i+"次");
inputNum=Integer.parseInt(inputValue);
if(inputNum==num){
JOptionPane.showMessageDialog(null,"恭喜您,猜对啦!","猜数字游戏",JOptionPane.INFORMATION_MESSAGE);
break;
}
else if(inputNum>num)
JOptionPane.showMessageDialog(null,"您猜的数字偏大","猜数字游戏", JOptionPane.WARNING_MESSAGE);
else
JOptionPane.showMessageDialog(null,"您猜的数字骗小","猜数字游戏", JOptionPane.WARNING_MESSAGE);
i++;
}
if(i>8)
JOptionPane.showMessageDialog(null,"游戏结束","猜数字游戏",JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"退出游戏","猜数字游戏",JOptionPane.INFORMATION_MESSAGE);
}
}
import java.util.Random;
import javax.swing.JOptionPane;
public class GuessNumber {
public static void main(String[] args){
int start=JOptionPane.showConfirmDialog(null,"猜数字,游戏开始","猜数字游戏?",JOptionPane.YES_NO_OPTION);
if(start==JOptionPane.YES_OPTION){
int num=(new Random().nextInt(100));
String inputValue;
int inputNum;
int i=1;
while(i<=8){
inputValue = JOptionPane.showInputDialog(null,"请输入一个1-100的整数\n共八次机会,这是第"+i+"次");
inputNum=Integer.parseInt(inputValue);
if(inputNum==num){
JOptionPane.showMessageDialog(null,"恭喜您,猜对啦!","猜数字游戏",JOptionPane.INFORMATION_MESSAGE);
break;
}
else if(inputNum>num)
JOptionPane.showMessageDialog(null,"您猜的数字偏大","猜数字游戏", JOptionPane.WARNING_MESSAGE);
else
JOptionPane.showMessageDialog(null,"您猜的数字骗小","猜数字游戏", JOptionPane.WARNING_MESSAGE);
i++;
}
if(i>8)
JOptionPane.showMessageDialog(null,"游戏结束","猜数字游戏",JOptionPane.ERROR_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"退出游戏","猜数字游戏",JOptionPane.INFORMATION_MESSAGE);
}
}