| Switch.java |
1 /**
2 * Ohjelma esitellään switch-lauseen toimintaa
3 * @author Vesa Lappalainen
4 * @version 1.0, 07.02.2003
5 */
6 public class Switch {
7
8 public static int swicth_testi(int x,int operaatio) {
9 switch (operaatio) {
10 case 5: /* Operaatio 5 tekee saman kuin 4 */
11 case 4: x *= 2; break; /* 4 laskee x=2*x */
12 case 3: x += 2; /* 3 laskee x=x+4 */
13 case 2: x++; /* 2 laskee x=x+2 */
14 case 1: x++; break; /* 1 laskee x=x+1 */
15 default: x=0; break; /* Muut nollaavat x:än */
16 }
17 return x;
18 }
19
20 private static void testaa(int x, int operaatio) {
21 int tulos = swicth_testi(x,operaatio);
22 System.out.println("x = " + x + " operaatio = " + operaatio +
23 " => " + tulos);
24 }
25
26 public static void main(String[] args) {
27 for (int i=0; i<7; i++) testaa(3,i);
28 }
29 }
30 | Switch.java |