0

我的计数器与主计数器采用不同的方法。我正在尝试累积多个剪刀石头布游戏中的赢、输和平局的数量。计数器只加值一次;一旦我再次玩游戏,计数器就会重置并且不包括上一场比赛的结果......

    do{
System.out.println("\n1=Rock\n2=Paper\n3=Scissors\n===========\nChoose:");

choice = Integer.parseInt(myInput.readLine());

determineOutcome();
System.out.println("\nYou have chosen " + ans);
System.out.println("The computer has chosen " + cans);
System.out.println("\nYOU'VE" + result);
System.out.println("\nWINS: " + win);
System.out.println("LOSSES: " + loss);
System.out.println("TIES: " + tie);

System.out.println("\nPress 1 to play again.");
loop = myInput.readLine();
}while("1".equals(loop));
} // end main method



public static void determineOutcome(){

// Randomize computer's choice (# between 1 and 3)
cchoice = (int)(Math.random()*3)+1;

// User's choice
switch ((int)choice)
{
    case 1: ans = (" Rock.");
        break;
    case 2: ans = (" Paper.");
        break;
    case 3: ans = (" Scissors.");
        break;
}
// Assign computer's number choice to a string
switch ((int)cchoice)
{
    case 1: cans = (" Rock.");
        break;
    case 2: cans = (" Paper.");
        break;
    case 3: cans = (" Scissors.");
        break;
}

win = 0;
loss = 0;
tie = 0;

if (choice == 1 && cchoice==3 || choice == 2 && cchoice == 1 || choice == 3 && cchoice == 2){
    result = (" WON");
    win++;
}

else if (choice == cchoice){
    result = (" TIED");
    tie++;
}
else if (choice == 1 && cchoice==2 || choice == 2 && cchoice == 3 || choice == 3 && cchoice == 1){
    result = (" LOST");
    loss++;
}
} // end determineOutcome(); method

我需要在确定结果()中添加一个循环吗?方法?如果有,在哪里?我试着把它放在整个东西上,但没有用。

4

1 回答 1

1

每次调用该方法时都这样做:

win = 0;
loss = 0;
tie = 0;

我假设你在调用方法之前已经初始化了变量,所以每次调用方法时不要将它们设置为 0。删除这段代码,它可能会正常工作:)

于 2013-06-08T18:06:53.283 回答