2017-09-21 17 views
0

これは私の最初の投稿ですので、フォーマットミスを許しません。異なる計算結果の集計

私のプログラムでは、架空の保険見積もりを表示するために、性別、事故件数、車の年数をリクエストしています。

これらすべての情報に基づいて、保険費用の小計を最後に追加する必要があります。

私は自分のコードをトータルコストのコメントまで作業しています(参照用にすべて投稿しました)。性別が異なる基本金額を持っているので、私はそこに行き着いています。私はそれがユーザーによって入力された性別と一致する場合、1つのif文だけを実行する方法を理解しようとしています。

アイデア?

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

    int currentYear = 2017; //used for calculating the age of the users car 
    int maleGender = 1000; 
    int femaleGender = 500; 

    //Letting user know they are inputting data for car insurance purposes 
    System.out.println("Car insurance questionnaire. Please input correct information when prompted."); 

    // gender information from user 
    System.out.println("What is your gender? m/f"); 
    String gender = scanner.next(); 

    // accident quantity information from user 
    System.out.println("How many accidents have you had?"); 
    int acc = scanner.nextInt(); 

    // car year information from user 
    System.out.println("What year was your car manufactured?"); 
    int carAge = scanner.nextInt(); 

    //if statements which refer to the users data input 
    if (gender.equals("m")) { 
    System.out.println("You are a male.\nThe base cost is $1000."); 
    } else { 
    System.out.println("You are a female.\nThe base cost is $500."); 
    } 


    if (acc == 0) { 
    System.out.println("You have no accidents. Insurance increase is $0."); 
    } else if (acc >= 1) { 
    System.out.println("You have " + acc + " accidents. Insurance increase is $" + acc * 100 + "."); 
    } 


    if (carAge >= 2007) { 
     System.out.println("Your car is " + (currentYear - carAge) + " years old.\nYour car is still in warranty, no savings added."); 
     } else 
     System.out.println("Your car is out of warranty, final cost is halved."); 

     //Total cost 
    /* 
     if (carAge <= 2007) { 
     System.out.println("Your total price is $" + ((acc * 100 + femaleGender)/2) + "."); 
     } else 
     System.out.println("Your total price is $" + (acc * 100 + femaleGender) + "."); 
     */ 


} 
} 
+0

自然言語だけでなく、コード( 'int subtotal = subtotal = 1000;')の意味を書き留めておけば、Javaコンパイラはあなたが望むものを理解しやすくなります。 )。 – blafasel

答えて

0

私はあなたの結果を計算する方法を全くわかりませんが、femaleGenderのすべての時間を使用したくない場合は男女別の値に依存して、このような多分何かが助けることができる:

int baseAmount = gender.equals("m") ? maleGender : femaleGender; 
if (carAge <= 2007) { 
    System.out.println("Your total price is $" + ((acc * 100 + baseAmount)/2) + "."); 
} else 
    System.out.println("Your total price is $" + (acc * 100 + baseAmount) + "."); 
} 
+0

それは完全に機能しました。どうもありがとうございました! – GChittick

0
int genderCost; 

... 

if (gender.equals("m")) { 
    System.out.println("You are a male.\nThe base cost is $1000."); 
    genderCost = maleGender; 
} else { 
    System.out.println("You are a female.\nThe base cost is $500."); 
    genderCost = femaleGender; 
} 

... 

if (carAge <= 2007) { 
    System.out.println("Your total price is $" + ((acc * 100 + genderCost)/2) + "."); 
} else 
    System.out.println("Your total price is $" + (acc * 100 + genderCost) + "."); 
} 

は、性別入力変数が評価されたときに、変数genderCostにおける男女のための量を入れて、あなたは合計を計算するときgenderCostを使用しています。

+0

これもうまくいきました、ありがとう、私はあなたの時間を感謝します! :) – GChittick