}
}
// 掷骰子
diceNum = doDice(); // 判断胜负
success = isSuccess(type,diceNum); // 金钱变化
money = changeMoney(money,success,cMoney); // 游戏结束 if(isEnd(money)){ }
System.out.println(\你输了,bye!\ break;
/**
* 读取用户输入
* @return 玩家输入的整数,如果格式非法则返回0 */
public static int readKeyboard() { } /**
* 押的类型校验
try {
// 缓冲区数组
byte[] b = new byte[1024]; // 读取用户输入到数组b中, // 读取的字节数量为n int n = System.in.read(b); // 转换为整数
String s = new String(b, 0, n - 2); int num = Integer.parseInt(s); return num;
} catch (Exception e) {} return 0;
* @param type 类型
* @return true代表符合要求,false代表不符合 */
public static boolean checkType(int type) { } /**
* 校验下注金额是否合法 * @param money 玩家金钱数 * @param cMoney 下注金额
* @return true代表符合要求,false代表不符合要求 */
public static boolean checkCMoney(int money, int cMoney) { } /** * 掷骰子
* @return 骰子的数值之和 */
public static int doDice() {
int n = (int) (Math.random() * 6) + 1; int n1 = (int) (Math.random() * 6) + 1; int n2 = (int) (Math.random() * 6) + 1; if (cMoney <= 0) { }
return false; return true; return false;
} else if (cMoney <= money) { } else {
if (type == 1 || type == 2) { }
return true; return false; } else {
}
// 输出随机结果
System.out.println(\庄家开:\ return n + n1 + n2;
/**
* 胜负判断
* @param type 用户输入类型 * @param diceNum 骰子点数 * @return true代表赢,false代表输 */
public static boolean isSuccess(int type, int diceNum) { } /**
* 金钱变化
* @param money 用户钱数 * @param success 胜负 * @param cMoney 下注金额 * @return 变化以后的金钱 */
// 计算庄家类型 int bankerType = 0; if (diceNum <= 9) { }
if (bankerType == type) { // 赢 }
return true; return false; } else { // 输
bankerType = 2;
System.out.println(\庄家开小!\ bankerType = 1;
System.out.println(\庄家开大!\
} else {
{ }
public static int changeMoney(int money, boolean success, int cMoney) } /**
* 判断游戏是否结束 * @param money 玩家金钱 * @return true代表结束 */
public static boolean isEnd(int money) { }
return money <= 0; if (success) { }
System.out.println(\剩余金额:\ return money;
money += cMoney; money -= cMoney; } else {
注意问题
上面介绍了IO类的基本使用,熟悉了实体流和装饰流的基本使用,但是在IO类实际使
用时,还是会遇到一系列的问题,下面介绍一些可能会经常遇到的问题。
类的选择
对于初次接触IO技术的初学者来说,IO类体系博大精深,类的数量比较庞大,在实际
使用时经常会无所适从,不知道该使用那些类进行编程,下面介绍一下关于IO类选择的一些技巧。
选择类的第一步是选择合适的实体流。
选择实体流时第一步是按照连接的数据源种类进行选择,例如读写文件应该使用文件流,
相关推荐: