アットウィキロゴ

条件判定

条件判断

if

if-else
  1. import java.io.*;
  2.  
  3. class sample01{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. while(true){
  9. String a = br.readLine();
  10. int b = Integer.parseInt(a);
  11.  
  12. if(b == 10){
  13. System.out.println("10が入力");
  14.  
  15. }else if(b == 99){
  16. break;
  17.  
  18. }else{
  19. System.out.println("それ以外が入力");
  20. }
  21. }
  22.  
  23. }catch(Exception ex){
  24. ex.printStackTrace();
  25.  
  26. }
  27.  
  28. }
  29. }
  30.  

多分岐

switch

int、charなど
  1. import java.io.*;
  2.  
  3. class sample02{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. while(true){
  9. String a = br.readLine();
  10. int b = Integer.parseInt(a);
  11. boolean c = false;
  12.  
  13. switch(b){
  14. case 10:
  15. System.out.println("10が入力");
  16. break;
  17.  
  18. case 20:
  19. System.out.println("20が入力");
  20. break;
  21.  
  22. case 30:
  23. System.out.println("30が入力");
  24. break;
  25.  
  26. case 99:
  27. System.out.println("システム終了");
  28. c = true;
  29. break;
  30.  
  31. default:
  32. System.out.println(b + "が入力");
  33. break;
  34. }
  35.  
  36. if(c == true){
  37. break;
  38. }
  39. }
  40.  
  41. }catch(Exception ex){
  42. ex.printStackTrace();
  43.  
  44. }
  45. }
  46. }
  47.  




最終更新:2012年09月02日 10:14