如何将存储在数组中的值用于另一种方法来进行计算?

时间:2018-03-17 08:41:39

标签: java algorithm

static void diceRoll(int[] val) {

        for (int i=0;i<6;i++) {
            int roll1 = (int) ((Math.random() * 1000 % 6 + 1));
            int roll2 = (int) ((Math.random() * 1000 % 6 + 1));
            int roll3 = (int) ((Math.random() * 1000 % 6 + 1));
            int roll4 = (int) ((Math.random() * 1000 % 6 + 1));
             int total =0;
            if ((roll1 < roll2) && (roll1 < roll3) && (roll1 < roll4)) {
                total= roll2 + roll3 + roll4;
            } else if ((roll2 < roll1) && (roll2 < roll3) && (roll2 < roll4)) {
                total= roll1 + roll3 + roll4;
            } else if ((roll3 < roll1) && (roll3 < roll2) && (roll3 < roll4)) {
                total = roll1 + roll2 + roll4;
            } else if ((roll4 < roll1) && (roll4 < roll2) && (roll4 < roll3)) {
                total = roll1 + roll2 + roll3;
            }
        }
}


static void calculateBonus(int[] bonusVal){
    int bonus=0;
    int[] val= new int[6];

    for (int i=0;i<6;i++)
    for(int j=0;j<6;j++)


        if (val[i] > 10 && val[i] != 11) {
            bonusVal[j] = (val[i] - 10) / 2;
        } else if (val[i] < 10) {
            bonusVal[j] = ((val[i] / 2) - 5);
        } else if (val[i] == 10 || val[i] == 11) {
            bonusVal[j] = 0;
        }
}


public static void main(String[]args){




    Scanner sc = new Scanner(System.in);

    //Declaring variables
    int level;
    String choice = null;

    //Getting the Level value
    System.out.println("Enter the Level value :");
    level = sc.nextInt();
    while ((level<=0)||(level>20)){
        System.out.println("Invalid input.Please enter a number between 1-20.");
        System.out.println("Enter the Level value : ");
        level = sc.nextInt();
    }
    do{
        int[] val= new int[6];
        int _str= val[0];
        int con= val[1];
        int dex= val[2];
        int _int= val[3];
        int wis= val[4];
        int _cha= val[5];


        int [] bonusVal=new int[6];
        int bonus1= bonusVal[0];
        int bonus2= bonusVal[1];
        int bonus3= bonusVal[2];
        int bonus4= bonusVal[3];
        int bonus5= bonusVal[4];
        int bonus6= bonusVal[5];


        //Printing the Level
        System.out.println("\n\n\n\n\nLevel :  [ "+level+" ]");

        //Displaying out put
        System.out.println("_Str  :  ["+_str+" ]"+"["+bonus1+"]");
        System.out.println("Dex  :  ["+dex+" ]"+"["+bonus2+"]");
        System.out.println("Con  :  ["+con+" ]"+"["+bonus3+"]");
        System.out.println("Int  :  ["+_int+" ]"+"["+bonus4+"]");
        System.out.println("Wis  :  ["+wis+" ]"+"["+bonus5+"]");
        System.out.println("_Cha  :  ["+_cha+" ]"+"["+bonus6+"]");

        //Calculating the Hit points
        double hp = (((Math.random()*1000 %6+1)+bonus3)*level);

        //Print the Hit points
        System.out.println("HP : ["+hp+"]");

        //Give the chance to re-roll or continue
        System.out.println("Type r if you want to re-roll or any other character if you want to continue :");
        choice = sc.next();
    }
    while (choice.equals("r"));;

}

}

我认为我在第二种方法中犯了错误。我想知道如何在数组中使用上面存储的值来计算奖金并将奖励的值存储在另一个数组中。这是我到目前为止的程序我仍然没有得到输出我需要从dice roll方法得到6个值并将它们存储在一个数组中。然后我想调用这些值来计算奖金并将这些奖励值存储在另一个数组中。

2 个答案:

答案 0 :(得分:0)

我想你想做类似下面的事情。我没有编译代码,如果您在编译时遇到任何问题,请告诉我。

static int[] calculateBonus(int[] val) {
   int[] bonusVal = new int[val.length];

   for(int j=0; j < val.length; j++) {
      if (val[j] > 10 && val[j] != 11) {
        bonus[j] = (val[j] - 10) / 2;
      } else if (val[j] < 10) {
        bonus[j] = ((val[j] / 2) - 5);
      } else if (val[j] == 10 || val[j] == 11) {
        bonus[j] = 0;
      }
   }

   return bonusVal;
}

答案 1 :(得分:0)

你需要从第一个方法返回一个数组,现在它根本不能用于数组。

将其更改为以下以返回数组并具有新的数组变量

static int[] diceRoll() { 
  final int rolls = 6;
  int[] result = new int[rolls];

  //the for loop...
在for循环结束时

将值存储在数组中,然后在结尾处返回数组,以便方法的结尾看起来像

    result[i] = total;
  }
  return result;
}

calculateBonus将使用此返回的数组作为输入,它也将在内部使用数组,而不是像现在一样使用int。我还补充说它将返回一个数组。

static int[] calculateBonus(int[] val){
    int count = val.length;
    int[] bonus = new int[count];

    for(int j=0;j<count;j++) {
        if (val[j] > 10 && val[j] != 11) {
            bonus[j] = (val[j] - 10) / 2;
        } else if (val[j] < 10) {
            bonus[j] = ((val[j] / 2) - 5);
        } else if (val[j] == 10 || val[j] == 11) {
            bonus[j] = 0;
        }
    }
    return bonus;
}

一个简单的运行

public static void main(String[] args) {
  int[] values = diceRoll();
  int[] bonus = calculateBonus(values);

  for(int i=0; i<bonus.length; i++) {
    System.out.println(bonus[i]);
  }
}

<强>更新

下面是两种方法的新版本,它解决了我在评论中提出的问题,并清理了一些代码。

主要区别在于我在diceRole方法中使用了ArrayList,而calculateBonus现在使用了double。

static int[] diceRoll() {
  int[] result = new int[6];

  for (int i=0;i<6;i++) {
    List<Integer> list = new ArrayList<>();
    for (int j = 0; j < 4; j++) {
      list.add(new Integer((int) ((Math.random() * 1000 % 6 + 1))));
    }

    Collections.sort(list); //list is now in ascending order

    int total =0;   
    for (int j = 1; j < list.size(); j++) {
      total += list.get(j).intValue();
    }
    result[i] = total;
  }
  return result;
}

static double[] calculateBonus(int[] val) {
  int count = val.length;
  double[] bonus = new double[count];

  for(int j=0;j<count;j++) {
    double value = 0.0;
    if (val[j] > 11) {
      value = (val[j] - 10.0) / 2.0;
    } else if (val[j] < 10.0) {
      value = ((val[j] / 2.0) - 5.0);
    }
    bonus[j] = value;
  }
  return bonus;
}