【问题标题】:How would I make this counter work?我将如何使这个柜台工作?
【发布时间】:2013-06-08 17:40:57
【问题描述】:

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

    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() 中添加一个循环;方法?如果有,在哪里?我试着把它放在整个东西上,但没有用。

【问题讨论】:

  • 首先,学习如何格式化代码;很难读!
  • 请分享信息,每场比赛后主线程是否终止,该游戏是否作为主线程的一部分运行。

标签: java counter do-while


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-23
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多