无法多次运行方法,无法编译

时间:2017-06-19 04:47:06

标签: java

我写了一个程序,为我的java类复制狂欢游戏。有5个骰子,4,6,8,12和20个侧面。我必须模拟玩游戏100次并计算胜利。运行它的代码一次正常。我无法让它运行100次。

我在编译时遇到这些错误: [第15行] 错误:Carnival类型中的方法dieRoll(int,int,int,int,int)不适用于参数(int) [行:17] 错误:dieRoll无法解析为变量

我正在使用

尝试使用for功能
for(int i = 0; i < 100; i++){dieRoll(i);} 
{
System.out.println("You rolled a " + dieRoll);
}

任何想法我做错了什么? 这是整个计划:

public class Carnival
{   
public static void main(String[] args)
{     

  int dice1=(int)(4*Math.random()+1);
  int dice2=(int)(6*Math.random()+1);
  int dice3=(int)(8*Math.random()+1);
  int dice4=(int)(12*Math.random()+1);
  int dice5=(int)(20*Math.random()+1);

  System.out.println(dieRoll(dice1, dice2, dice3, dice4, dice5));
  for(int i = 0; i < 100; i++){dieRoll(i);}       //Here is where I have the
                                                  //errors       
  {
  System.out.println("You rolled a " + dieRoll);  //And here
  }

  }//end main

   public static int dieRoll(int dice1, int dice2, int dice3, int dice4, int
   dice5) 
   {
   return dice1 + dice2 + dice3 + dice4 + dice5; 
   }//end method  
   }//end class

2 个答案:

答案 0 :(得分:1)

第一个错误是由于您尝试将1个参数传递给具有多个参数的函数。函数调用语句应该具有函数定义中的确切参数数量。

其次,您正在使用的print语句是直接打印不可能的功能。最好将输出存储在变量中,然后将其打印出来。

答案 1 :(得分:0)

你好如果你通过生成随机数来玩游戏100次,你需要在for循环中添加随机数的计算以生成不同的数字。

for(int i = 0; i < 100; i++)
{
 int dice1=(int)(4*Math.random()+i);
 int dice2=(int)(6*Math.random()+i);
 int dice3=(int)(8*Math.random()+i);
 int dice4=(int)(12*Math.random()+i);
 int dice5=(int)(20*Math.random()+i);
 System.out.println("You rolled : "+dieRoll(dice1,dice2,dice3,dice4,dice5));
}
相关问题