1

this is my java program that should ask you what you want to do with the array 'drugs' which contain some strings. But when I run it the while loop repeats 2 times before ask me to digit 'q' to exit... end when I press q key the condition ignore this. I'm new in java and I'm begin crazy with that, thanks for support.

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("This program will show the array \'drugs\' in different way, using functions\n\n");

    printMenu();
    System.out.print(">>");
    int ans = input.nextInt();

    String[] drugs = {"LSD", "cocaine", "weed", "eroine(white)", "eroine(brown)", "hashish", "extasi"};

    String ans2 = "";
    while(true){
      switch(ans){
        case 1:
          printArray(drugs);
          break;
        case 2:
          printArrayReverse(drugs);
          break;
        case 3:
          shiftOnLeft(drugs);
          break;
        case 4:
          break;
        default:
          System.out.println("Insert a valid number\n");
          break;
      }
      System.out.print("\n\n[Press q to quit or enter key to repeat the loop again]");
      ans2 = input.nextLine();

      if(ans2 == "q"){
        break;
      }
    }
  }
CRL
  • 27
  • 3

0 Answers0