?? slotmachine.java
字號:
import java.io.*;
public class SlotMachine
{
public static void main(String[ ] args)throws IOException
{
int money = 0;
int bet = 0;
int x;
int y;
int answer;
int c=1;
boolean valid;
BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
System.out.println("你有多少本金?");
valid=false;
while (! valid)
{
try
{
money=(new Integer(in.readLine())).intValue();
valid=true;
}catch(NumberFormatException e)
{
System.out.println("輸入錯誤!請重新輸入:");
}
}
while(c==1)
{
System.out.println("你想賭多少?");
valid=false;
while (! valid)
{
try
{
bet=(new Integer(in.readLine())).intValue();
if(bet>money)
{
System.out.println("你的本金不夠!請重新輸入:");
continue;
}
valid=true;
}catch(NumberFormatException e)
{
System.out.println("輸入錯誤!請重新輸入:");
}
}
x=(int) (Math.random()*5*money)+1;
System.out.println("猜一猜比"+x+"大還是小(1為大,0為小)?\n你的選擇:");
valid=false;
while (! valid)
{
try
{
answer=(new Integer(in.readLine())).intValue();
if(answer!=0 && answer!=1)
{
System.out.println("輸入錯誤!請重新輸入(1為比"+x+"大,0為比"+x+"小):");
continue;
}
valid=true;
y=(int) (Math.random()*5*money)+1;
System.out.println("結果是:"+y);
if( (y>x && answer==1) || (y<x && answer==0))
{
System.out.println("恭喜你贏了!");
money=money+bet;
}
if( (y>x && answer==0) || (y<x && answer==1) || x==y )
{
System.out.println("很遺憾你輸了.");
money=money-bet;
}
}catch(NumberFormatException e)
{
System.out.println("輸入錯誤!請重新輸入(1為比"+x+"大,0為比"+x+"小):");
}
}
System.out.println("你現(xiàn)在的余額為:"+money);
if(money!=0)
{
System.out.println("是否繼續(xù)游戲(1為是,0為否)?\n你的選擇:");
valid=false;
while(!valid)
{
try
{
c=(new Integer(in.readLine())).intValue();
if(c!=0 && c!=1)
{
System.out.println("輸入錯誤!請重新輸入(1為繼續(xù)游戲,0為退出游戲):");
continue;
}
valid=true;
}catch(NumberFormatException e)
{
System.out.println("輸入錯誤!請重新輸入(1為繼續(xù)游戲,0為退出游戲):");
}
}
}
else
{
System.out.println("歡迎下次再來!");
break;
}
}
if(c==0)System.out.println("歡迎下次再來!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -