的结果这是我的第一篇文章在这里,所以请原谅我对任何格式错误。汇总不同的计算
因此,大家可以看到我的程序要求性别,事故和汽车的年#显示虚构保险报价。
基于所有这些信息,我需要保险费用的小计添加到末尾。
我有我的代码工作,直到总成本评论(贴吧所有以供参考)。我被困在那里,因为性别有不同的基数。我试图找出一种方法来只做一个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) + ".");
*/
}
}
这将是更容易为Java编译器来了解你想要什么,如果你写下你的意思(“基本费用为$ 1000”),不仅在自然语言,但在代码('INT小计;小计= 1000;' )。 – blafasel