📄 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("你现在的余额为:"+money);
if(money!=0)
{
System.out.println("是否继续游戏(1为是,0为否)?\n你的选择:");
valid=false;
while(!valid)
{
try
{
c=(new Integer(in.readLine())).intValue();
if(c!=0 && c!=1)
{
System.out.println("输入错误!请重新输入(1为继续游戏,0为退出游戏):");
continue;
}
valid=true;
}catch(NumberFormatException e)
{
System.out.println("输入错误!请重新输入(1为继续游戏,0为退出游戏):");
}
}
}
else
{
System.out.println("欢迎下次再来!");
break;
}
}
if(c==0)System.out.println("欢迎下次再来!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -