简体   繁体   中英

Can you use a for loop in a switch case?

I am making a program where I choose 4 alternatives from a menu. When I press "1" I want the program to print out different numbers. Can you use for loops in a switch case? I was thinking of an array and when I press 1 I want the program to print out these numbers that I have stored in the array. Can I use a method? What do I do?

Here is a example: switch (number) {

        case "1":
            System.out.println(");
            break;
        case "2":
            System.out.println(");
            break;
        case "3":
            System.out.println(");
            break;
        case "4":
            System.out.println(");
            break;

        case "e":
            break;

So when i press alternative 1, I want these numbers to print out: int[] prices = {85, 83, 86, 84, 83, 92, 107, 407, 420, 393, 369, 345, 299, 268, 276, 281, 337, 450, 482, 416, 321, 88}; Where should i put these? Should i use a for loop? And when these numbers have been printed out, the program should ask what value I am writing.

Try this if you want array elements to be displayed

  public class Main {
      public static void main(String[] args) {
     int[] prices = {85, 83, 86, 84, 83, 92, 107, 407, 420, 393, 369, 345, 299, 268, 276, 281, 337, 450, 482, 416, 321, 88};
        for (int i = 0; i < prices.length; i++) {
          System.out.println(prices[i]);
        }
      }
    }

I didn't quite get your question though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM