0

I am having one of the most frustrating moments in my life (second semester coding challenge). I am utilizing two lists to obtain three random numbers, and then I want those three random numbers to be assigned to their proper logo like a slot machine. However, my for loops are not behaving like I want them to... It is spitting out three results per random number.

`import java.util.Scanner; import java.util.Random; import java.util.Arrays;

public class LabProgram {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in);

  final int numReels = 3;
  Random random = new Random(); 

  int[ ] symbols = new int[numReels];
  String [ ] logo = new String [numReels];
  
  
  for (int i = 0; i < symbols.length; ++i) {
     symbols[ i ] = random.nextInt(5);
  //System.out.println(symbols[i]);
  }
  
  for (int k = 0; k < symbols.length; ++k)
     for (int j = 0; j < 3; ++j) {
     
     if (symbols[k] == 0) {
         logo[j] = "Cherry";
         
     }
     else if (symbols[k] == 1) {
         logo[j] = "7";
         
     }
     else if (symbols[k] == 2) {
         logo[j] = "Grape";
         
  }
     else if (symbols[k] == 3) {
         logo[j] = "Orange";
         
  }
     else if (symbols[k] == 4) {
         logo[j] = "Lemon";
         
  }
  System.out.println(logo[j] + " ");
     }
     

System.out.println("symbol #: " + symbols[0] + " logo: " + logo[0]); System.out.println("symbol #: " + symbols[1] + " logo: " + logo[1]); System.out.println("symbol #: " + symbols[2] + " logo: " + logo[2]);`

2 Answers 2

2

You should not have a nested loop here. Use just a single loop, accessing elements from both arrays at the same index on each iteration.

for (int i = 0; i < symbols.length; i++) {
    if (symbols[i] == 0) {
        logo[i] = "Cherry";
    } else if 
       // other checks...
}

Since all the possible symbol values form a consecutive range, it is simpler to index into an array of all the symbols, avoiding a long chain of if statements. (If they were not consecutive, a Map can be used instead.)

final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon"};
for (int i = 0; i < symbols.length; i++) {
    logo[i] = allSymbols[symbols[i]];
}
Sign up to request clarification or add additional context in comments.

2 Comments

I appreciate this! As I posted this question I actually realized this method and how much simpler it is... I was just being extremely stubborn and was trying to make my original idea work. Thank you though!!
@Elijah-AmirHodges Happy to help. If you want to stick to your original method with the chain of ifs, then the first part of my answer addresses that (i.e. use a single loop and use the same index to access both arrays).
0

You don't need the second inner for loop. You can also use a enhanced switch:

for (int k = 0; k < symbols.length; ++k) {
  switch (symbols[k]) {
    case 0 -> logo[k] = "Cherry";
    case 1 -> logo[k] = "7";
    case 2 -> logo[k] = "Grape";
    case 3 -> logo[k] = "Orange";
    case 4 -> logo[k] = "Lemon";
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.