1) How to make in a normal example a double condition in a switch? I tried everything you can.

In this example, the compiler swears.

public class Ivan { public static void main(String[] args) { int a = 5, b = 10; switch(a, b){ case 5: case 10: System.out.println("Правильно"); break; case 6: case 11: System.out.println("Неправильно"); break; default: System.out.println("Потеря потерь"); break; } } } 

2) Is it possible to fill the values ​​in each element into an array with a size of 10 elements through a loop? If so, how?

3) Is it possible to make the array contain both strings and integers and fractional values? If so, how?

 Какой-нибудь_общий_тип array[] = {1, "Hello", 12.5}; 

Thank you in advance kind people!

  • rewrote the answer - michael_best
  • everything is clear painted? - michael_best

1 answer 1

The answer to the first question: Yes, you can.

 switch(a) { case 1: //если а = 1 switch(b) { case 1: System.out.println("1"); break; } break; case 2: если a = 2 switch(b) { case 1: System.out.println("1"); break; } } 

But this operator is not used for this. For this there is an if .

The answer to the second question: Yes, you can.

 int a = 0; for(int i; i < mass.length; i++) { mass[i] = a++; } 

The answer to the third question: Yes, you can. To do this, write:

 Object mass[] = {1, "4", new NewClass(){}, 1.28}; //где NewClass - воображаемый 

anonymous class declared using the interface (just an example)

  • Thank! Do not tell me how to write different values ​​into an array? And not just one thing :) - Petrovchenko Ivan
  • And why such a strange construction of a switch? - Petrovchenko Ivan
  • one of the reasons - it is not used for this - michael_best
  • @PetrovchenkoIvan changed the answer - michael_best