2016-09-04 10 views
-3

10個のパッケージを入れると正しい結果が得られますが、58個を入れると間違った結果になります。間違った結果になる

マイ出力:それは持つべき何

Enter the number of packages purchased: 58 
Your total without discount: 5742.0 
The discount is: 1148.4 
The discounted total: 4593.6 

Enter the number of packages purchased: 58 
Your total without discount: 5742.0 
The discount is: 2411.6 
The discounted total: 3330.3 

package pkg; 

import java.util.Scanner; 

public class Hello { 
    public static void main(String[] args) { 
     double packageCost = 99.00; 
     Scanner user_input = new Scanner(System.in); 
     System.out.print("Enter the number of packages purchased: "); 
     int askPackage = user_input.nextInt(); 
     double withoutDiscount = packageCost * askPackage; 
     double firstDiscount = (packageCost * askPackage) - (withoutDiscount * .20); 
     double secondDiscount = (packageCost * askPackage) - (withoutDiscount * .33); 
     double thirdDiscount = (packageCost * askPackage) - (withoutDiscount * .42); 
     double fourthDiscount = (packageCost * askPackage) - (withoutDiscount * .49); 
     System.out.println("Your total without discount: $" + withoutDiscount); 
     if (askPackage >= 10 || askPackage <= 19) { 
      System.out.println("The discount is: $" + withoutDiscount * .20); 
      System.out.println("The discounted total: $" + firstDiscount); 
     } else if (askPackage >= 20 || askPackage <= 49) { 
      System.out.println("The discount is: $" + withoutDiscount * .33); 
      System.out.println("The discounted total: $" + secondDiscount); 
     } else if (askPackage >= 50 || askPackage <= 99) { 
      System.out.println("The discount is: $" + withoutDiscount * .42); 
      System.out.println("The discounted total: $" + thirdDiscount); 
     } else if (askPackage >= 100) { 
      System.out.println("The discount is: $" + withoutDiscount * .49); 
      System.out.println("The discounted total: $" + fourthDiscount); 
     } 
    } 
} 
+5

'if'では' || 'の代わりに' && '演算子が必要です。 –

+0

気を散らす空の行がなくても、より良い書式のコード、コードを投稿しようとしてください。今回はあなたのコードを修正しましたが、やはりこれからも自分自身でこの努力をしてください。あなたの努力は非常に高く評価されます。 –

+0

@AndrewLygin私はあなたのコメントは答えであると信じています。 – naXa

答えて

-1

あなたは&&の代わりに使用する必要がありますif else文でを参照してください。可能なすべての入力は、最初の条件askPackage >= 10 || askPackage <= 19で処理されます。従って、10から19の間の入力のみがあなたに真の出力を与えるでしょう。

P.S.あなたはそのような割引割り当てを行うことができます:

double firstDiscount = (withoutDiscount * .80); 

またはそれ以上のソリューション;共通のdiscount変数を作成し、if-elseステートメントに代入します。

+0

私の答えは何が間違っていますか?私は訂正を書いてダウン投票を得た:) – Raptor