Java Programing Hey everyone, I am having two issues getting this code to oppera
ID: 3850172 • Letter: J
Question
Java Programing
Hey everyone, I am having two issues getting this code to opperate the way i would like it to. I will post the code in its entirety, followed by the assignment for those who would like to see it. my issues are as follows:
1. how do i get the random number generatior to mark a selection as used, so that there are no duplicates in the competition (no repeated use of formations)?
2. how do i limit it to one 6 point round per competition?
CODE:
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class Skydiving1
{
public static void main(String[] args)
{
Random myRandom = new Random();
Scanner inputClass = new Scanner(System.in);
ArrayList< Boolean > formationRound = new ArrayList< Boolean >();
int formation = 0;
int point = 0;
int jumpClass = 0;
int roundNumber = 0;
int round = 1;
boolean pickedRandom = false;
boolean A = false;
boolean AA = false;
boolean AAA = false;
for( int i = 0; i < 39; i++)
{
formationRound.add( false );
}
formationRound.set( 0, true );
System.out.print( "1: Class A 2: Class AA 3: Class AAA ");
System.out.print( " Enter number 1-3 depending on class: " );
jumpClass = inputClass.nextInt();
if( jumpClass == 1 )
{
A = true;
round = 6;
formationRound.set( 1, true );
formationRound.set( 3, true );
formationRound.set( 5, true );
formationRound.set( 8, true );
formationRound.set( 10, true );
formationRound.set( 11, true );
formationRound.set( 12, true );
formationRound.set( 14, true );
formationRound.set( 16, true );
formationRound.set( 17, true );
formationRound.set( 18, true );
formationRound.set( 20, true );
formationRound.set( 22, true );
}
else
{
System.out.print( "6 or 10 rounds? ");
round = inputClass.nextInt();
if( jumpClass == 2 )
{
AA = true;
formationRound.set( 3, true );
formationRound.set( 5, true );
formationRound.set( 10, true );
formationRound.set( 12, true );
formationRound.set( 16, true );
formationRound.set( 17, true );
}
else
{
AAA = true;
}
}
for(int i=0; i < round; i++ )
{
System.out.printf(" Round %d: ", i+1);
point = 0;
while(point < 5)
{
pickedRandom=false;
formation = myRandom.nextInt(38) + 1;
while(formation == 31)
{
formation = myRandom.nextInt(38) + 1;
}
if(point == 4 && pickedRandom && formation < 23 )
{
while(formation < 23)
formation = myRandom.nextInt(38) + 1;
}
if(formation < 23)
{
point+=2;
}
else
{
pickedRandom=true;
point++;
}
formationRound.set( formation, true );
if(formation > 22)
System.out.printf(" %s %s ",randomLetter(formation), formationName(formation));
else
System.out.printf(" %d %s", formation, formationName(formation));
}
System.out.println();
System.out.printf( " %s ", point );
}
}
public static String formationName(int g)
{
switch(g)
{
case 1: return "Snowflake Snowflake";
case 2: return "Sidebody Donut Sidebody Donut";
case 3: return "Side Flake Opal Turt";
case 4: return "Monopod Monopod";
case 5: return "Opal Opal";
case 6: return "Stardian Stardian";
case 7: return "Sidebuddies sidebuddies";
case 8: return "CandianTree CandianTree";
case 9: return "CatsAccordian CatsAccordian";
case 10: return "Dimond Bunyid";
case 11: return "Photon Photon";
case 12: return "Bundy Bundy";
case 13: return "Offset Spinner";
case 14: return "Bipole Bipole";
case 15: return "Catipilar Catipilar";
case 16: return "Compressed Box";
case 17: return "DanishTree Murphy";
case 18: return "Zircon Zircon";
case 19: return "Ritz Icepick";
case 20: return "Piver Viper";
case 21: return "ZigZag Marguis";
case 22: return "ZigZag Marguis";
case 23: return "Unipod";
case 24: return "Stairstep Diamond";
case 25: return "MurphyFlake";
case 26: return "Yuan";
case 27: return "Meeker";
case 28: return "Open Accordian";
case 29: return "Cataccord";
case 30: return "Bow";
case 31: return "";
case 32: return "Donut";
case 33: return "Hook";
case 34: return "Adder";
case 35: return "Star";
case 36: return "Crank";
case 37: return "Satelit";
case 38: return "Sidebody";
case 39: return "Phalanx";
default: return"";
}
}
public static String randomLetter (int f)
{
if (f>22)
switch (f)
{
case 23: return "A";
case 24: return "B";
case 25: return "C";
case 26: return "D";
case 27: return "E";
case 28: return "F";
case 29: return "G";
case 30: return "H";
case 31: return "I";
case 32: return "J";
case 33: return "K";
case 34: return "L";
case 35: return "M";
case 36: return "N";
case 37: return "O";
case 38: return "P";
case 39: return "";
default: return "";
}
return"";
}
}
ASSIGNMENT:
and so on.......................................
Explanation / Answer
To avoid duplicates when calling the random function, you can store as many numbers as you want in an array and then check if the numbers are repeated in that array. Here is a function for this:
public static int[] main(int dig, int max, int min)//Pass the number of random digits that you want, the maximum and minimum range of random numbers
{
Random rand = new Random();
int j,i;
int a[]=new int[dig];
for(i=(dig-1);i>=0;i--)
{
a[i]=rand.nextInt((max - min) + 1) + min;
for(j=(dig-1);j>i;j--)
{
if(a[j]==a[i]){System.out.println(" Duplicate number made. Remaking. "); i++; break;}//Check each element of array with one number. If a number is duplicate, break the loop after incrementing i so that a new number can be inputted into the same place of i.
}
}
return a;//Return array of unique numbers
}
As for the second question. You didn't post the last program requirements that you ammended in this program so it is not at all clear what the program does.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.