LoginSignup
1
1

More than 5 years have passed since last update.

人数と合計金額から100円単位で一人当たりの割り勘計算

Last updated at Posted at 2016-02-27

人数と合計金額を入力すると100円単位での割り勘金額を出力する。

人数と合計金額を入力してください
7 10000
一人あたりの金額は、1428.5714285714287円です。
100円単位では、一人当たり1400円、ひとりだけ1600円です。

Calc.java
import java.util.Scanner;
class Calc{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);

        System.out.println("人数と合計金額を入力してください");
        int i,j,l,n;
        i = scan.nextInt();
        j = scan.nextInt();     

        double f,g;     
        f = (double)j/i;
        System.out.println("一人あたりの金額は、" + f + "円です。");
        l = (int)Math.round(f/100)*100;
        n = j-l*(i-1);
        System.out.println("100円単位では、一人当たり" + l + "円、ひとりだけ" + n + "円です。");
    }
}   

new Scanner(System.in)とかscan.nextInt()とかMath.roundとか理解はあまいがとりあえず進もう。

1
1
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
1
1