我的方法似乎无法正确打印到printstream
。仅显示isOutComeNew()
的部分似乎正在打印某些内容。但是,即使是这个似乎也要打印最后输入的数字。 randOm()
方法似乎也是唯一可以正常工作的方法。
我不知道为什么会这样。
public class Assignment3 {
public static int number1, number2, result, newResult, newNumber1, newNumber2;
public static int winner1 = 5, winner2 = 7, winner3 = 12;
public static int loser1 = 2, loser2 = 4, loser3 = 11;
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws Exception {
PrintStream ps = new PrintStream("Assignment3.txt");
promptUser();
number1 = input.nextInt();
System.out.println("Please enter another integer between 1-6 ");
number2 = input.nextInt();
do {
ps.println("You rolled a" +number1 + "and a " +number2);
isValid();
result = outCome(number1, number2);
ps.printf("The outcome is %d \n", result);
isOutcome();
newNumber1 = number1 + 2;
newNumber2 = number2 + 3;
newNumbers();
result = outCome(newNumber1, newNumber2);
ps.printf("The outcome is %d \n", result);
isOutcomeNew();
promptUser();
number1 = input.nextInt();
System.out.println("Please enter another integer between 1-6 ");
number2 = input.nextInt();
}while ((number1 != 99) && (number2 != 95));
randOm();
}
public static void promptUser() {
System.out.println("In order to stop please enter 99 and 95 to end the game. ");
System.out.println("Please enter an integer between 1-6. ");
}
public static int outCome(int x, int y) {
int sum;
sum = x + y;
return (sum);
}
public static void isValid()throws Exception {
PrintStream ps= new PrintStream("Assignment3.txt");
while ((number1 < 1 || number1 > 6) || (number2 < 1 || number2 > 6)) {
System.out.println("This set of dice rolls is invalid. Please enter another set. \n");
System.out.println("Please reenter a number between 1 and 6. ");
number1 = input.nextInt();
System.out.println("Please reenter a number between 1 and 6. ");
number2 = input.nextInt();
System.out.printf("You rolled a %d and a %d\n", number1, number2);
}
}
// method for determining winner, loser ,or draw
public static void isOutcome() throws Exception {
PrintStream ps = new PrintStream("Assignment3.txt");
// integer value for winner is 1, int value for loser is -1 and int value for draw is 0
int winner = 1;
int loser = -1;
int draw = 0;
int oldCount = 0, oldLose = 0, oldDraw = 0;
if ((result == winner1) || (result == winner2) || (result == winner3)) {
System.out.println(winner);
oldCount++;
} else if ((result == loser1) || (result == loser2) || (result == loser3)) {
System.out.println(loser);
oldLose++;
} else {
System.out.println(draw);
oldDraw++;
}
System.out.printf("The amount of wins are %d \n", oldCount);
System.out.printf("The amount of losses are %d \n", oldLose);
System.out.printf("The amount of draws are %d \n", oldDraw);
}
public static void isOutcomeNew() throws Exception {
int winner = 1;
int loser = -1;
int draw = 0;
int newCount = 0, newLose = 0, newDraw = 0;
PrintStream ps = new PrintStream("Assignment3.txt");
if ((result == winner1) || (result == winner2) || (result == winner3)) {
System.out.println(winner);
newCount++;
} else if ((result == loser1) || (result == loser2) || (result == loser3)) {
System.out.println(loser);
newLose++;
} else {
System.out.println(draw);
newDraw++;
}
System.out.printf("The amount of computer wins are %d \n", newCount);
System.out.printf("The amount of computer losses are %d \n", newLose);
System.out.printf("The amount of computer draws are %d \n", newDraw);
}
//
public static void newNumbers() throws Exception {
PrintStream ps = new PrintStream("Assignment3.txt");
while (newNumber1 > 6 || (newNumber2 > 6)) {
if (newNumber1 > 6) {
newNumber1 = (newNumber1 - 6);
System.out.printf("The computer rolled %d \n", newNumber1);
}
if (newNumber2 > 6) {
newNumber2 = (newNumber2 - 6);
System.out.printf("The computer rolled %d \n", newNumber2);
}
}
System.out.printf("The computer rolled %d and a %d\n", newNumber1, newNumber2);
}
答案 0 :(得分:0)
只有说isOutComeNew()的部分似乎正在打印某些东西。
您有许多方法可以做到这一点:
PrintStream ps = new PrintStream("Assignment3.txt");
这些调用中的每个调用都将打开一个新流,以输出文件并截断该文件。这意味着您先前编写的输出将被删除。
解决方案:
仅打开PrintStream
一次,然后将ps
作为方法参数或通过实例或类变量传递给方法。
实例化PrintStream
时以“追加”模式打开它。您无法直接执行此操作,但是可以通过在附加模式下创建FileOutputStream
,然后将其传递给PrintStream
构造函数来实现。