0

Hello I am trying to generate a random case to be used to display a string. So far the code below will only output case 2. For example, if the randomNum was 1, I would like it to print out what is in case 1, and if the randomNum was 2, I would like it to print out what is in case 2. Please let me know if this is possible or not, and show me how I can correct my code if it is possible. Thank you for your help!

  String Aries = "";
  Random number = new Random();
  int i = 0;
  int randomNum = number.nextInt(2) + 1;

  switch(randomNum)
  {
    case 1:
      Aries = "On April 2...";
    case 2:
      Aries = "In 2018...";
  }
  System.out.println(Aries);
Sonia
  • 1
  • 1
  • 5
  • 13
    You need a `break` in the cases. [The switch Statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html). – 001 Apr 19 '18 at 21:27
  • 5
    You can simply use a `List` containing your strings, rather than a switch. – Andy Turner Apr 19 '18 at 21:28
  • Please follow Java naming conventions : attributes, variables, parameters, method have to start in lowerCase (Aries => aries) – azro Apr 19 '18 at 21:29

5 Answers5

1

You need to add a break at the end of each case statement. If you don't add this break and enter in one case, every following cases will be executed like shown in this snippet :

int i = 2;

switch(i) {
  case 1 :
    System.out.println("case 1");
    // add "break;" here
  case 2:
    System.out.println("case 2");
    // add "break;" here
  case 3:
    System.out.println("case 3");
    /* Optionally but strongly recommended, 
     * you shall also add a "break;" here
     * so that there is no surprise if you add a case.
     */
}

Output :

case 2
case 3

Also, you can simplify the code by replacing the switch/case with a String array:

String[] strings = new String[]{"case 1", "case 2", "case 3"};
Random random = new Random();
int index = random.nextInt(strings.length);
System.out.println(strings[index]);
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
0

if you want to stop de case you need to use a break sentence, like this...

    Random number = new Random();
    int i = 0;
    int randomNum = number.nextInt(2) + 1;
    String aries;

    switch(randomNum){
        case 1:
            aries = "On April 2...";
            break; //<- stop the switch
        case 2:
            aries = "In 2018...";
            break; //<- stop the switch
    }

    System.out.println(aries);

Hope it helps...

0

switch case statements fall-through by default. Minimal change, add break to at least case 1 (although adding it to case 2 would also be advisible). Like,

String Aries = "";
Random number = new Random();
int randomNum = number.nextInt(2) + 1;
switch (randomNum) {
case 1:
    Aries = "On April 2...";
    break;
case 2:
    Aries = "In 2018...";
    break;
}
System.out.println(Aries);

However, you only have two cases - so a boolean and if-else would be shorter (and more readable). Also, you don't seem to need Aries except for printing. So I would eliminate it. Like,

Random number = new Random();
if (number.nextBoolean()) {
    System.out.println("On April 2...");
} else {
    System.out.println("In 2018...");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

just add break like these and it will work

import java.util.*;
 public class aaaaa {

public static void main(String[] args) {
   String Aries = "";
     Random number = new Random();
     int i = 0;
     int randomNum = number.nextInt(2) + 1;

     switch(randomNum)
     {
       case 1:
         Aries = "On April 2...";
         break;
       case 2:
         Aries = "In 2018...";
         break;
     }
System.out.println(Aries );

}

}

Sideeg MoHammed
  • 375
  • 2
  • 10
-3

I'd create a random number between a range.
Int ran0 = ThreadLocalRandom.current().nextInt(1, 2 + 1);

calmchess
  • 118
  • 8