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?

会計額を表示する表示するプログラムを作成しよう

Posted at
CalcCakeSum
class CalcCakeSum{
	public static void main (String[] args){
		
		double cakeTotalAmount = 0; // ケーキの合計金額
		
		for ( int i = 0 ; i < args.length ; i+=2){
			
			String cakeName = args[i]; // ケーキ名
			int count = Integer.parseInt(args[i + 1]); // 個数
			int cakePrice = 0; // ケーキの値段
			
			// 商品ごとの価格を判定
			if (cakeName.equals("ショートケーキ")){
				
				cakePrice = 320;
				
			}else if (cakeName.equals("モンブラン")){
				
				cakePrice = 350;
			
			}else if (cakeName.equals("チョコレートケーキ")){
				
				cakePrice = 370;
				
			}else if (cakeName.equals("いちごのタルト")){
				
				cakePrice = 400;
				
			}else if (cakeName.equals("チーズケーキ")){
				
				cakePrice = 300;
			
			}
			
			// ケーキの合計金額
			cakeTotalAmount += cakePrice * count;
			
		}
			
		// 合計金額が1000円以上の時、20%引き
		if ( cakeTotalAmount >= 1000 ){
			
			cakeTotalAmount  = cakeTotalAmount * 0.8;
			
		}
		
		// 税込みの値段(8%)
		cakeTotalAmount  = cakeTotalAmount * 1.08;
		// 小数点以下切り捨て
		cakeTotalAmount  = Math.floor(cakeTotalAmount);
		System.out.println("合計金額は" + (int)cakeTotalAmount + "円です。");
		
	}
}

今回は、ケーキの注文を受け付け、コマンドライン引数で入力したケーキの合計金額を出すプログラムに挑戦した。

~~~今回の学び~~~
・ケーキの合計金額をcakeTotalAmountという変数を使って、int型ではなくdouble型を使用した。
└なぜなら「合計金額が1000円以上の時、20%引き」と「税込みの値段(8%)」を適用させないといけなかったから。そして最後に出力するときには1000.0円のような表記だとおかしいので少数から整数に直す必要があった。

四捨五入
cakeTotalAmount  = Math.round(cakeTotalAmount);
小数点以下を切り捨て
cakeTotalAmount  = Math.floor(cakeTotalAmount);

Math.floor()を使って四捨五入しないようにして、System.out.println()の中に(int)を付けることで整数とすることに成功。

・==と.equals()の違い
└int型などの基本データ型の場合、値が等しいかどうかは==演算子を使用する。配列や文字列などの参照型の場合、==演算子は同じオブジェクトを参照している場合にtrueとなるため、例えば、2つの文字列が同じ値であっても、参照している文字列のオブジェクトが異なる場合はfalseとなる。

String msg1 = "Hello";
String msg2 = new String(msg1);

System.out.println(msg1 == msg2);  // false

2つの文字列が同じ文字列オブジェクトを参照している場合は==演算子はtrueとなる。

String msg1 = "Hello";
String msg2 = msg1;

System.out.println(msg1 == msg2);  // true

2つの文字列に格納されている値が等しいかどうかを調べるには、Stringクラスで用意されているequalsメソッドを使う。以下は2つの文字列が格納している値が同じなのでtrueが返されました。このように文字列と文字列の値を比較する場合には、Stringクラスのequalsメソッドを使用したほうがいい。

String msg1 = "Hello";
String msg2 = new String(msg1);

System.out.println(msg1.equals(msg2));  // true

2つの変数に同じ文字列リテラルを格納した場合は==演算子でもtrueとなりますが、それ以外の方法で文字列を作成した場合にはfalseとなるためです。

char[] c = {'A', 'B', 'C'};
String msg1 = new String(c);
String msg2 = new String(c);

System.out.println(msg1 == msg2);       // false
System.out.println(msg1.equals(msg2));  // true

参考:==と.equals()の違いhttps://www.javadrive.jp/start/string/index4.html#section1

0
0
2

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?