java 制作小游戏_如何用java制作小游戏
展開全部
import?javax.swing.Icon;
import?javax.swing.JOptionPane;
public?class?CaiShuZi4JOptionPane?{
/**
*?@param?args
*/
public?static?void?main(String[]?args)?{
Icon?icon?=?null;
boolean?bl?=?false;
int?put?=?0;
int?c?=?(int)?(((Math.random())*100)+1);??//獲取一個1-100的隨32313133353236313431303231363533e78988e69d8331333363393739機數
System.out.println("你獲取的隨機數是:"+c);??//打印你的隨機數字
String?str1??=?(String)?JOptionPane.showInputDialog(null,"請輸入你的猜測數字(1-100):\n","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");?//第一次輸入你的猜測數字
if(str1==null){
JOptionPane.showMessageDialog(null,?"你已經取消了本次游戲");?//如果你點取消那么本次游戲結束
}else{
bl?=?num(str1);??//判斷是輸入的是不是數字或者是整數
if(true==bl){??????//如果是數字的話進入與隨機數比較的程序
System.out.println("你輸入的數字是:"+str1);??//打印你輸入的數字
put?=?Integer.valueOf(str1);
for(int?i?=?4;i?>?0;i--){????//i是你可以猜測的次數
if(put==c){
JOptionPane.showMessageDialog(null,?"恭喜你猜對了,正確答案是:"+c+"。");??//如果你猜對了就直接結束循環
break;
}else?if(put>c){??//如果輸大了就讓你再次從新輸入
str1??=?(String)?JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請重新輸入:\n","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null,?"你已經取消了本次輸入");
break;
}else{
bl?=num(str1);
if(true==bl){
put?=?Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null,?"你的輸入不正確,請重新輸入");
}
}
}else?if(put
str1??=?(String)?JOptionPane.showInputDialog(null,"你的輸入過小。你還有"+i+"次機會,請重新輸入:\n","猜數字游戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
if(str1==null){
JOptionPane.showMessageDialog(null,?"你已經取消了本次輸入");
break;
}else{
bl?=num(str1);
if(true==bl){
put?=?Integer.valueOf(str1);
}else{
JOptionPane.showMessageDialog(null,?"你的輸入不正確,請重新輸入");
}
}
}
}
}else?if(bl==false){???//這個?是你第一次如果填寫的不是數字的話也會結束本次游戲
JOptionPane.showMessageDialog(null,?"請您下次按要求填寫。本次游戲結束");
}
if(true==bl?&&?c!=put){??//如果你i次都沒猜對,那么就直接告訴你這個數十什么
JOptionPane.showMessageDialog(null,?"很遺憾你沒能猜對,這個數字是:"+c+".");
}
}
}
public??static?boolean?num(String?value){??//一個靜態方法,判斷你輸入的是不是數字
try?{
Integer.parseInt(value);
return?true;
}?catch?(Exception?e)?{
return?false;
}
}
}
前天寫的猜數字游戲,yong?i控制猜測次數,有詳細解析,用黑窗口可以直接運行,
總結
以上是生活随笔為你收集整理的java 制作小游戏_如何用java制作小游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 扑克牌排序_java 怎么写c
- 下一篇: go 用 mysql web开发环境_简