我该如何使这个计数器工作?

时间:2013-06-08 17:40:57

标签: java counter do-while

我的计数器与主要方法不同。我试图在多个Rock-Paper-Scissors游戏中积累赢,亏和关系的数量。计数器只增加一次值;一旦我再次玩游戏,计数器就会被重置,并且不包括上一个游戏的结果......

    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

我是否需要在determineOutcome()中添加一个循环;方法?如果是的话,在哪里?我尝试将它放在整个事物上并且它不起作用。

1 个答案:

答案 0 :(得分:1)

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

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

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