2013-04-15 63 views
0

好的,所以,我对java很新。
我正在设计一个分数计算器,这个计划对于我一直搁在一旁的项目来说。不过,我想知道如何做到这一点。得分计算器

该程序应该要求掷骰子,并将其添加到前面的每个玩家。
我假设一个while循环会完成这个任务,但是每次它通过循环时,都会将变量重置为当前的卷。因此,我不能让一个总...

下面是一些代码:

static int players; 
    static String p1; 
    static String p2; 
    static String p3; 
    static String p4; 
    static int maxScore; 
    static int roll1; 
    static int roll2; 
    static int roll3; 
    static int roll4; 
    static int total1; 
    static int total2; 
    static int total3; 
    static int total4; 
    public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    System.out.print("Enter number of players: "); 
    players=keyboard.nextInt(); 
    System.out.print("Enter Maximum Score: "); 
    maxScore=keyboard.nextInt(); 
    if(players==2){     //will add more ifs when i get the code right 
     System.out.println("Please enter players names."); 
     System.out.print("Player 1: "); 
     p1=keyboard.next(); 
     System.out.print("Player 2: "); 
     p2=keyboard.next(); 
     System.out.println(p1 + "\t \t " + p2 + "\n" 
     + "_______ \t _______ \n"); //displays scorecard look with players names 

     { 


     while (total1 < maxScore && total2<maxScore) { 
     //scorecard quits when maxScore is reached by either player 
     int roll; 
     total1=(roll+roll1); 

     System.out.print(""); 
     roll=keyboard.nextInt(); //asks for current roll 

     System.out.print("\n"+"_____"+"\n"); 
     System.out.print(roll+"+"+"\n"+roll1+"\n"+"_____"+"\n"+(roll+roll1)+"\n"); 
     /*i want this to display total score + last roll and then 
     *total it again on the next line*/ 
     roll1=roll; 
     } 
+1

看起来像是将'roll1'分配给'roll'而不是添加其值。你可以试试:'roll1 + = roll;' – jcern

+1

while循环之前的大括号是什么?什么变量应该保持总量?总数是1吗?如果是这样,只需将卷装入卷1,然后执行总计+ = roll1(+ =表示总数=总计+卷) – Ben313

回答

1

在Java编程进度的一些提示:

  1. 变量roll没有任何意义。如roll1等等,将存储每个玩家的最后一卷。

  2. 如果可能,初始化您的变量。应该避免依赖默认值,因为它可能会在学习中给您带来问题(NullPointerException会在某个时间访问您)。

  3. 在你的循环中,你有total1=(roll+roll1);。这是错误的。您的变量total1,roll,roll1在程序到达此点时未进行初始化。因为它们是整数,所以它们(默默)初始化为0,所以total1在这一点上产生0,这并没有太大的成就。在此之后,您继续检索卷。尝试另一种方式,先卷,然后合起来。

  4. 你提到你是Java的新手,但是在将来某个时候,你可能会考虑在数组中实现同样的程序。您会注意到它可以节省您现在编写的大量重复代码。

总结,并翻译成代码准则(2个玩家):

public class MyScoreCalculator { 
    static String p1 = ""; 
    static String p2 = ""; 
    static int maxScore = 0; 
    static int roll1 = 0; 
    static int roll2 = 0; 
    static int total1 = 0; 
    static int total2 = 0; 

    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 
     // Dialogue to get data... 
     // Display scorecard look with players names 

     while (total1 < maxScore && total2 < maxScore) { 
      //scorecard quits when maxScore is reached by either player 
      roll1 = keyboard.nextInt(); // ask for current roll 

      System.out.println(total1 + "+"); 
      System.out.println(roll1); 
      System.out.println("_____"); 
      System.out.println(roll1 + total1); 

      total1 = total1 + roll1; 

      // Do the same for next player. 
     } 
    } 
} 
+0

谢谢。我终于意识到我在循环中过早地定义了总数。我知道我需要学习很多东西。这是我的第一个编程语言,我只待了一个星期。感谢您的帮助 – derek

+0

@derek缓慢而稳定,这就是每个人在学习编程时都会如何去做的。这没什么错。我指出的第四个暗示迟早会在你的学习路线中显现出来,以及查尔斯在他的回答中提出的建议,虽然对于初学者来说这是一些更高级的东西。如果这些答案中的任何一个解决了您的问题,请不要忘记接受它。 – afsantos

1

如果我正确地读你的问题,然后解决的办法是

total1+=(roll+roll1); 

这是同样的事情as

total1= total1+(roll+roll1); 

您只是不将卷加入总价值!

同样值得注意的是,将实例变量设置为公共和静态并不是一个好主意。如果他们是私人的而不是静态的,会更好。例如

private int players; 

希望的答案可以帮助

+0

感谢您的答复。在我原来的代码中,我确实已经添加到下一卷。我必须以某种方式将它排除在外。我遇到的问题是,当它显示总数时,显示'0' – derek

0

你共1计算应total1 += roll和新的轧辊输入后发生。如果roll1表示最后一个卷,则相应地命名该变量,它的可读性更高。

由于您有很多玩家,请尝试抽象概念并将输入和“会计”分开。例如,您可以创建一个包含总数和最后一个输入(以及玩家名称)的PlayerScore类,并使用一种方法来负责添加和保存最后一次输入以及打印相应的信息。然后,您可以收集PlayerScore并迭代它,询问当前的滚动并更新信息。