Java - 使用If语句打印顺序错误的主方法

时间:2015-12-11 15:54:36

标签: java

如果这是任何类型的重复,我会提前预约,但我无法找到解决我特定问题的任何内容。

这是我的计划:



import java.util.Random;

public class CarnivalGame{

    public static void main(String[] args){

        int count = 0;
        int wins = 0;
        for (int i = 0; i < 101; i++){
             System.out.println("Roll " + count);
             count = count + 1;
             int die1=(dieRoll(6));
             int die2=(dieRoll(20));
             int die3=(dieRoll(8));
             int die4=(dieRoll(4));
             int die5=(dieRoll(12));
             int sum = die1+die2+die3+die4+die5;
             System.out.println("Total:    " +  sum + "\n");

             if ((sum >= 35) || (sum < 20)){
             System.out.println("Player wins!\n");
             wins = wins + 1;
     }
     if (count == 100){
    //PROBLEM AREA
     System.out.printf("After 100 rolls, the player has won for a total of %d times!\n", wins); 
     }
    }//end for loop
  }//end main

public static int dieRoll(int sides){ 
   int num = 0;
   int roll = 0;
   Random  rng = new Random(); 
   if(sides >=4){ 
      for(int i = 0; i < 1; i++){ 
         roll = rng.nextInt(sides)+1;
         System.out.println("Roll is:  " + roll);
         num = num + roll; 
      }//end for loop
   }//end if
 return num;
  }//end method   
}//end class
&#13;
&#13;
&#13;

目前打印出来的内容:

此示例中的滚动计数= 100

For loop (x100):

---> "After 100 rolls, the player has won for a total of (wins) times!" Roll (count)

    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Roll is:  dieRoll(x)
    Total:    sum

    if ((sum >= 35) || (sum < 20))
    "Player wins!"

我试图将其打印出来:

同样的事情,但

 if ((sum >= 35) || (sum < 20))
        "Player wins!"
---> "After 100 rolls, the player has won for a total of (wins) times!

所以基本上我试图弄清楚为什么我的问题区域与滚动计数一起打印,它应该在总和之后打印并且&#34;玩家获胜!&#34;

我假设if语句格式化时出错,但我无法弄清楚如何修复它。谢谢你的时间。

3 个答案:

答案 0 :(得分:2)

你的问题是你的循环有101次迭代,从0到100。 因此,在编号99之后,你的100个骰子卷已结束,然后你又滚了一次。只需将循环更改为0到99(i <100而不是101)。

编辑(主要是因为我无法直接回复你):

当你(尝试)反复100次时,我真的不明白为什么你需要变量count。让你的循环滚动骰子一百次,添加胜利,然后打印你的声明。

答案 1 :(得分:0)

对不起伙计们,我的逻辑错误确实很愚蠢。这是我的问题的解决方案,正如Jesper和Calvin P.建议的那样,我只需要在for循环之后放置问题区域。我以为我必须把它放在循环中。谢谢大家的帮助!

import java.util.Random;

public class CarnivalGame
{
public static void main(String[] args)
{
int count = 0;
int wins = 0;
for (int i = 0; i < 100; i++)
{
System.out.println("Roll " + (i + 1));
count = count + 1;
int die1=(dieRoll(6));
int die2=(dieRoll(20));
int die3=(dieRoll(8));
int die4=(dieRoll(4));
int die5=(dieRoll(12));
int sum = die1+die2+die3+die4+die5;
System.out.println("Total:    " +  sum + "\n");

if ((sum >= 35) || (sum < 20)){
System.out.println("Player wins!\n");
wins = wins + 1;
}
if (count == 100){
//FIXED PROBLEM
System.out.printf("After 100 rolls, the player has won for a total of %d times!\n", wins); 
}
}//end for loop
}//end main

public static int dieRoll(int sides)
{ 
   int num = 0;
   int roll = 0;
   Random  rng = new Random(); 
   if(sides >=4) 
      { 
      for(int i = 0; i < 1; i++)
         { 
         roll = rng.nextInt(sides)+1;
         System.out.println("Roll is:  " + roll);
         num = num + roll; 
   }//end for loop
   }//end if
return num;
}//end method   

}//end class

答案 2 :(得分:0)

您的变量count始终提前一个迭代器变量i,因为您在循环中很早就增加了它。因此,当i仍为99时,您将达到计数== 100。

在计数增加之前移动输出或在循环结束时增加。