我是刚学JAVA不久,在写小练习的时候碰到这个问题我说一下我要实现的功能:就是一开始一段代码(后面我称之为父片段)提供几个选项,从键盘读入,不同的选项执行不同的子代码片段,不同的子代码片段里面又要求从键盘输入相应的信息。当执行完某一个子片段片段之后,我想要回到父片段处重新读取键盘输入选择其他的选项执行相应的子片段,所以我在子片段中调用了父片段的方法,就在这个地方出问题了,当执行之后就出现
“java.io.IOException: Stream closed”,这个错误了,耽误大家一点时间帮我看一下,请看附件图片和下文代码(最好是直接复制粘贴到eclipse或者netbeans中,容易看一点):
import java.io.*;
public class LotterySystem {
boolean buyFlag = false; //判断是否已经选号
int lotteryCount = 0; //一共选号多少组
static int j = 0;
boolean resultFlag = false; //本期开奖结果是否已知
boolean []blue = null; //每组选号蓝球是否匹配
int []count = null; //每组选号红球匹配个数
int[] nbs = new int[7]; //系统随机生成一组号
int[][] number = new int[10][8]; //二维数组用于存放不同组选号,第8位存放该组选号的下注数
public static void main(String[] args) {
LotterySystem ls = new LotterySystem();
ls.chooseList();
}
//主菜单选择
private void chooseList() {
if(buyFlag == false && resultFlag == false) {
String space = " ";
System.out.println("* * * * * 欢迎进入双色球彩票系统 * * * * *");
System.out.println(space + "1、购买彩票\n" + space + "2、查看开奖\n" + space + "3、退出");
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择主菜单:");
try(BufferedReader buff = new BufferedReader(new InputStreamReader(System.in))) {
String line = buff.readLine();
j = Integer.parseInt(line);
if(j < 1 || j > 3) {
chooseList();
}else if(j == 1)
if(resultFlag == false)
buyLottery();
else {
System.out.println("\n本期开奖结果已知,请选择 2、查看开奖 3、退出系统");
chooseList();
}
else if(j == 2)
if(buyFlag == false)
checkLottery();
else {
System.out.println("本期您已经完成选号,请选择 2、查看开奖 3、退出系统");
chooseList();
}
else if(j == 3) {
System.out.println("系统退出!");
System.exit(0);
}
buff.close();
} catch(IOException e) {
e.printStackTrace();
}
}
private void buyLottery() {
StringBuffer st = new StringBuffer();
String[] s1,s2,s3 = null;
System.out.println("\n* * * * * * * * * * * * * * * * * * * * * *");
System.out.println("温馨提示:1注双色球包括6个红球和1个蓝球,红球号码范围为1-33,蓝球号码范围为1-16;\n"
+ "红球号码在前,蓝球号码在后,每个数字之间请用' '隔开,多组号码之间用','隔开,\n每组号码"
+ "投注的注数请在每组号码后面加上'*投注数量'。");
System.out.println("* * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请输入您要投注的号码:");
try{
BufferedInputStream buf = new BufferedInputStream((System.in));
int a = buf.read();
while(a != -1) {
if(a == '\n')
break;
char c = (char)a;
st.append(c);
a = buf.read();
}
s1 = st.toString().split(",");
lotteryCount = s1.length;
for(int i=0;i<s1.length;i++) {
s2 = s1.split(" ");
s3 = s2[s2.length-1].split("\\*");
for(int j=0;j<s2.length-1;j++) {
number[j] = Integer.parseInt(s2[j]);
}
for(int k=0;k<s3.length;k++) {
number[s2.length-1+k] = Integer.parseInt(s3[k]);
}
}
buf.close();
} catch(IOException e) {
e.printStackTrace();
}
System.out.println("\n您本期购买的彩票为:");
for(int i=0;i<lotteryCount;i++) {
for(int j=0;j<number.length-1;j++) {
System.out.print(number[j] + " ");
}
System.out.println(" (" + number[7] + "注)");
}
buyFlag = true;
chooseList();//这里执行就出错了!
}
private void checkLottery() {
//重新启动一次系统才重新产生新的开奖号码
if(nbs[0] == 0) {
for(int i=0;i<nbs.length-1;i++) {
nbs = (int)(Math.random() * 33) + 1;
}
nbs[6] = (int)(Math.random() * 16) + 1;
}
System.out.println("\n本期开奖号码为:");
System.out.print("红球:");
for(int a=0;a<nbs.length-1;a++) {
System.out.print(nbs[a] + ",");
}
System.out.print("蓝球:");
System.out.println(nbs[6]);
for(int i=0;i<lotteryCount;i++) {
if(number[6] == nbs[6])
blue = true;
}
for(int i=0;i<nbs.length-1;i++) {
for(int j=0;j<lotteryCount;j++) {
for(int k=0;k<number[j].length-3;k++) {
if(number[j][k] == nbs)
count[j] ++;
}
}
}
if(buyFlag == true)
result();
resultFlag = true; //本期开奖结果已知
chooseList();
}
//开奖结果核对
private void result() {
int res = 0;
for(int i=0;i<count.length;i++) {
if(blue == true)
res = 1;
if(count != 0)
res = 1;
}
if(res ==1) {
System.out.println("恭喜您中奖!您的中奖号码为:");
for(int i=0;i<lotteryCount;i++) {
for(int j=0;j<number.length-1;j++)
System.out.print(number[j] + " ");
System.out.print(",中奖注数为:" + number[7] + "注,中奖金额为:" + bonus(i));
}
}else {
System.out.println("很遗憾,您没有中奖");
}
}
//计算奖金
private int bonus(int i) {
int money = 0;
int k = number[7];
if(blue == true) {
switch(count) {
case 0:
money = 5 * k;
break;
case 1:
money = 15 * k;
break;
case 2:
money = 50 * k;
break;
case 3:
money = 1000 * k;
break;
case 4:
money = 10000 * k;
break;
case 5:
money = 200000 * k;
break;
case 6:
money = 5000000 * k;
break;
}
}else {
switch(count) {
case 4:
money = 1000 * k;
break;
case 5:
money = 10000 * k;
break;
case 6:
money = 200000 * k;
break;
}
}
return money;
}
}
“java.io.IOException: Stream closed”,这个错误了,耽误大家一点时间帮我看一下,请看附件图片和下文代码(最好是直接复制粘贴到eclipse或者netbeans中,容易看一点):
import java.io.*;
public class LotterySystem {
boolean buyFlag = false; //判断是否已经选号
int lotteryCount = 0; //一共选号多少组
static int j = 0;
boolean resultFlag = false; //本期开奖结果是否已知
boolean []blue = null; //每组选号蓝球是否匹配
int []count = null; //每组选号红球匹配个数
int[] nbs = new int[7]; //系统随机生成一组号
int[][] number = new int[10][8]; //二维数组用于存放不同组选号,第8位存放该组选号的下注数
public static void main(String[] args) {
LotterySystem ls = new LotterySystem();
ls.chooseList();
}
//主菜单选择
private void chooseList() {
if(buyFlag == false && resultFlag == false) {
String space = " ";
System.out.println("* * * * * 欢迎进入双色球彩票系统 * * * * *");
System.out.println(space + "1、购买彩票\n" + space + "2、查看开奖\n" + space + "3、退出");
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择主菜单:");
try(BufferedReader buff = new BufferedReader(new InputStreamReader(System.in))) {
String line = buff.readLine();
j = Integer.parseInt(line);
if(j < 1 || j > 3) {
chooseList();
}else if(j == 1)
if(resultFlag == false)
buyLottery();
else {
System.out.println("\n本期开奖结果已知,请选择 2、查看开奖 3、退出系统");
chooseList();
}
else if(j == 2)
if(buyFlag == false)
checkLottery();
else {
System.out.println("本期您已经完成选号,请选择 2、查看开奖 3、退出系统");
chooseList();
}
else if(j == 3) {
System.out.println("系统退出!");
System.exit(0);
}
buff.close();
} catch(IOException e) {
e.printStackTrace();
}
}
private void buyLottery() {
StringBuffer st = new StringBuffer();
String[] s1,s2,s3 = null;
System.out.println("\n* * * * * * * * * * * * * * * * * * * * * *");
System.out.println("温馨提示:1注双色球包括6个红球和1个蓝球,红球号码范围为1-33,蓝球号码范围为1-16;\n"
+ "红球号码在前,蓝球号码在后,每个数字之间请用' '隔开,多组号码之间用','隔开,\n每组号码"
+ "投注的注数请在每组号码后面加上'*投注数量'。");
System.out.println("* * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请输入您要投注的号码:");
try{
BufferedInputStream buf = new BufferedInputStream((System.in));
int a = buf.read();
while(a != -1) {
if(a == '\n')
break;
char c = (char)a;
st.append(c);
a = buf.read();
}
s1 = st.toString().split(",");
lotteryCount = s1.length;
for(int i=0;i<s1.length;i++) {
s2 = s1.split(" ");
s3 = s2[s2.length-1].split("\\*");
for(int j=0;j<s2.length-1;j++) {
number[j] = Integer.parseInt(s2[j]);
}
for(int k=0;k<s3.length;k++) {
number[s2.length-1+k] = Integer.parseInt(s3[k]);
}
}
buf.close();
} catch(IOException e) {
e.printStackTrace();
}
System.out.println("\n您本期购买的彩票为:");
for(int i=0;i<lotteryCount;i++) {
for(int j=0;j<number.length-1;j++) {
System.out.print(number[j] + " ");
}
System.out.println(" (" + number[7] + "注)");
}
buyFlag = true;
chooseList();//这里执行就出错了!
}
private void checkLottery() {
//重新启动一次系统才重新产生新的开奖号码
if(nbs[0] == 0) {
for(int i=0;i<nbs.length-1;i++) {
nbs = (int)(Math.random() * 33) + 1;
}
nbs[6] = (int)(Math.random() * 16) + 1;
}
System.out.println("\n本期开奖号码为:");
System.out.print("红球:");
for(int a=0;a<nbs.length-1;a++) {
System.out.print(nbs[a] + ",");
}
System.out.print("蓝球:");
System.out.println(nbs[6]);
for(int i=0;i<lotteryCount;i++) {
if(number[6] == nbs[6])
blue = true;
}
for(int i=0;i<nbs.length-1;i++) {
for(int j=0;j<lotteryCount;j++) {
for(int k=0;k<number[j].length-3;k++) {
if(number[j][k] == nbs)
count[j] ++;
}
}
}
if(buyFlag == true)
result();
resultFlag = true; //本期开奖结果已知
chooseList();
}
//开奖结果核对
private void result() {
int res = 0;
for(int i=0;i<count.length;i++) {
if(blue == true)
res = 1;
if(count != 0)
res = 1;
}
if(res ==1) {
System.out.println("恭喜您中奖!您的中奖号码为:");
for(int i=0;i<lotteryCount;i++) {
for(int j=0;j<number.length-1;j++)
System.out.print(number[j] + " ");
System.out.print(",中奖注数为:" + number[7] + "注,中奖金额为:" + bonus(i));
}
}else {
System.out.println("很遗憾,您没有中奖");
}
}
//计算奖金
private int bonus(int i) {
int money = 0;
int k = number[7];
if(blue == true) {
switch(count) {
case 0:
money = 5 * k;
break;
case 1:
money = 15 * k;
break;
case 2:
money = 50 * k;
break;
case 3:
money = 1000 * k;
break;
case 4:
money = 10000 * k;
break;
case 5:
money = 200000 * k;
break;
case 6:
money = 5000000 * k;
break;
}
}else {
switch(count) {
case 4:
money = 1000 * k;
break;
case 5:
money = 10000 * k;
break;
case 6:
money = 200000 * k;
break;
}
}
return money;
}
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择主菜单:");
try(BufferedReader buff = new BufferedReader(new InputStreamReader(System.in))) {
String line = buff.readLine();
j = Integer.parseInt(line);
switch(j) {
case 1:
if(resultFlag == false || buyFlag == false)
ls.buyLottery();
else if(resultFlag == true)
System.out.println("\n本期开奖结果已知,请选择 2、查看开奖 3、退出系统");
else if(buyFlag == true)
System.out.println("\n您本期已经购买彩票,请选择 2、查看开奖 3、退出系统");
break;
case 2:
ls.checkLottery();
break;
case 3:
System.out.println("系统退出!");
System.exit(0);
}
//buff.close();
} catch(IOException e) {
e.printStackTrace();
}
}