アットウィキロゴ

演算子

四則演算

和、差、積、商

  1. import java.io.*;
  2.  
  3. class sample03{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. System.out.print("a=");
  9. String a = br.readLine();
  10.  
  11. System.out.print("b=");
  12. String b = br.readLine();
  13.  
  14. int c = Integer.parseInt(a);
  15. int d = Integer.parseInt(b);
  16.  
  17. System.out.printf("a + b = %d\n", c + d);
  18. System.out.printf("a - b = %d\n", c - d);
  19. System.out.printf("a * b = %d\n", c * d);
  20. System.out.printf("a / b = %d\n", c / d);
  21.  
  22. }catch(Exception ex){
  23. ex.printStackTrace();
  24.  
  25. }
  26. }
  27. }
  28.  

インクリメント・デクリメント

加算、減算

  1. import java.io.*;
  2.  
  3. class sample04{
  4. public static void main(String[] args){
  5.  
  6. try{
  7.  
  8. // インクリメント数を設定
  9. System.out.print("インクリメント数=");
  10. String a = br.readLine();
  11. int c = Integer.parseInt(a);
  12.  
  13. System.out.print("デクリメント数=");
  14. String b = br.readLine();
  15. int d = Integer.parseInt(b);
  16.  
  17. // インクリメント加算
  18. for(int i = 0;i < c;i++){
  19. System.out.println(i + "回目のループ");
  20. }
  21.  
  22. // デクリメント減
  23. for(int i = d;i > 0;i--){
  24. System.out.println( i + "回目のループ");
  25. }
  26.  
  27. }catch(Exception ex){
  28. ex.printStackTrace();
  29.  
  30. }
  31.  
  32. }
  33. }
  34.  
変数名++:処理後に加算
++変数名:処理前に加算
変数名--:処理後に減算
--変数名:処理前に減算
 



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