0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java学習メモ④ 四則演算

0
Last updated at Posted at 2026-05-27

四則演算

public class Main {
    public static void main(String[] args){
        int a = 10;
        int b = 3;

        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
    }
}
結果
13
7
30
3

intとdoubleの違い

  • intの割り算⇒小数点以下切り捨て
  • doubleの割り算⇒小数点以下も必要な時

合計金額を計算するプログラム

public class Main{
    public static void showPayment(int price1, int price2){
        System.out.println((int)((price1 + price2) * 1.1));
    }

    public static void main(String[] args){
        showPayment(100, 200);
        showPayment(500, 300);
        showPayment(1000, 250);
    }
}
結果
330
880
1375
  • ()と演算子の優先順位に気を付ける
  • キャスト演算子を使って、double 型の値を int 型に変換
    ⇒(int)(計算結果)⇒小数点以下切り捨て

戻り値を使う

public class Main{
    public static int payment(int price1, int price2){
        return((int)((price1 + price2) * 1.1));
    }

    public static void main(String[] args){
        int result = payment(100, 200);
        System.out.println("お会計は" + result + "円です");
    }
}
お会計は330円です
  • public static intに変えてreturnで戻り値
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?