これは私の最初の投稿ですので、フォーマットミスを許しません。異なる計算結果の集計
私のプログラムでは、架空の保険見積もりを表示するために、性別、事故件数、車の年数をリクエストしています。
これらすべての情報に基づいて、保険費用の小計を最後に追加する必要があります。
私は自分のコードをトータルコストのコメントまで作業しています(参照用にすべて投稿しました)。性別が異なる基本金額を持っているので、私はそこに行き着いています。私はそれがユーザーによって入力された性別と一致する場合、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) + ".");
*/
}
}
自然言語だけでなく、コード( 'int subtotal = subtotal = 1000;')の意味を書き留めておけば、Javaコンパイラはあなたが望むものを理解しやすくなります。 )。 – blafasel