-
Vesa Lappalainen authoredVesa Lappalainen authored
Switch.java 1.31 KiB
package ohjausrak;
/**
* Ohjelma esitelln switch-lauseen toimintaa
* @author Vesa Lappalainen
* @version 1.0, 07.02.2003
*/
public class Switch {
/**
* Esimerkki switch-lauseen kytst jossa valutaan seuraavaan
* @param mx muutettava muuttuja
* @param operaatio mik operaatio tehdn
* @return muutettu arvo
*/
@SuppressWarnings("fallthrough")
public static int swicth_testi(int mx,int operaatio) {
int x = mx;
switch (operaatio) {
case 5: /* Operaatio 5 tekee saman kuin 4 */
case 4: x *= 2; break; /* 4 laskee x=2*x */
case 3: x += 2; /* 3 laskee x=x+4 */
case 2: x++; /* 2 laskee x=x+2 */
case 1: x++; break; /* 1 laskee x=x+1 */
default: x=0; break; /* Muut nollaavat x:n */
}
return x;
}
private static void testaa(int x, int operaatio) {
int tulos = swicth_testi(x,operaatio);
System.out.println("x = " + x + " operaatio = " + operaatio +
" => " + tulos);
}
/** @param args ei kytss */
public static void main(String[] args) {
for (int i=0; i<7; i++) testaa(3,i);
}
}