0

I cant figure out how to input all the money amounts,which is stored in the array (case_value), into the other array, (cases).I am trying to do this randomly. My program does run but never finishes. This is what i have so far. In case you couldn't guess, I am trying to copy the format of deal or no deal. Also, if there is any way to make my code more efficient, I would appreciate it. Thank you for the help.

import java.util.Scanner;
import java.lang.Math;
public class DOND
{
public static void main (String [] args)
{
Scanner sc = new Scanner (System.in);

int [] cases=new int [26]; //array for the cases
int [] case_value=new int [26]; //array for the money amounts
int b=0;

case_value[0]=1; // all the money amounts
case_value[1]=2;
case_value[2]=5;
case_value[3]=10;
case_value[4]=25;
case_value[5]=50;
case_value[6]=75;
case_value[7]=100;
case_value[8]=200;
case_value[9]=300;
case_value[10]=400;
case_value[11]=500;
case_value[12]=750;
case_value[13]=1000;
case_value[14]=5000;
case_value[15]=10000;
case_value[16]=25000;
case_value[17]=50000;
case_value[18]=75000;
case_value[19]=100000;
case_value[20]=200000;
case_value[21]=300000;
case_value[22]=400000;
case_value[23]=500000;
case_value[24]=750000;
case_value[25]=1000000;

for (int i=0;i<26;i++) //distributing the money
{
while (cases[b]==0); //checks if case is filled 
{
int r=(int)(Math.random()*26); //randomizes money amounts in case
if (cases[r]==0)
{
b=r;
cases[r]=case_value[i]; //inserts money amount in case
}
} 
}

for (int i=0;i<26;i++) //outputs value in cases
{
System.out.println(cases[i]); 
}

}
}
  • Hi Sonu, you want to copy the contents in case_values as is to cases or you want to randomize the contents in case_values to cases ?. Also, i suggest you to use ArrayLists = new ArrayList(addArrayList here). So this basically adds the contents of one object into other. – Anvesh Vejandla Nov 01 '14 at 21:22
  • I want to randomize the contents of case_values to cases. I am a grade 11 student so i am very new to java. I have not learned ArrayLists yet and I am only supposed to use what we learned in class to do this project. Thank you for the help. – Sonu Chakraborty Nov 01 '14 at 21:31

2 Answers2

0

If you just want the input shuffled, why not use the built in Collection.shuffle method?

// Fill up array with input
final List<Integer> list = new ArrayList<>(array.length);
for (int i = 0; i < array.length; i++) {
    list.add(array[i]);
}

Collections.shuffle(list);
// Work with this shuffled list
Dan W
  • 5,718
  • 4
  • 33
  • 44
0

Since you need to shuffle the contents in your array and need to store those in other array. I suggest you to use the below method.

Step1) Put data into one array. Step2) shuffle the contents in that array. Step3) for each item now add that into different array.

To shuffle the contents you can use.

// Implementing Fisher–Yates shuffle
  static void shuffleArray(int[] ar)
  {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--)
    {
     int index = rnd.nextInt(i + 1);
      // Simple swap
     int a = ar[index];
     ar[index] = ar[i];
     ar[i] = a;
    }
  }

You can find this in Random shuffling of an array Please don't duplicate questions. This is a possible duplicate.

Community
  • 1
  • 1
Anvesh Vejandla
  • 221
  • 1
  • 2
  • 11