我是一名初学者程序员,现在已经玩了几天了......我似乎找不到一种方法来存储一个 double 通过一系列 if 语句运行它,然后在System.out.println("...")
.
我尝试过使用数组、开关等。我需要通过更多的东西来运行这个 BaseRate,这取决于我需要修改 BaseRate 并让它存储之前的修改的用户输入。
我想出的最接近的方法是通过几个 if 语句存储 BaseRate,但最终结果仍然是原始数字 (4000.00)。我真的不太懂这门语言,当你不太了解这门语言及其可能性时,编程会很困难。提前感谢您的帮助。
import java.util.Scanner;
public class example
{
public static double BaseRate = 4000.00; //really not familiar with this
public static void main(String[] args)
{
Scanner keyboardInput = new Scanner( System.in);
double BaseRate2 = BaseRate; //probably not necessary
//Name
System.out.println("\rPlease enter your name:");
String UserName = keyboardInput.nextLine();
//Gender
System.out.println("Are you male or female? Answer m or f:");
String input;
char UserSex;
input = keyboardInput.nextLine();
//If the applicant is female, apply a 5 % discount to the base rate.
UserSex = input.charAt(0);
if (UserSex == 'f')
{
System.out.println("Discount 5%");
double BaseRate = BaseRate2 * 0.95;
System.out.println(BaseRate);
}
else
{
System.out.println("Rate is now " + BaseRate);
}
//Experience Driving
System.out.println("How many years have you been driving?");
int UserExperience = keyboardInput.nextInt();
//------------------------------------------------------------------
//If driving more than one year and less than 5,
//then apply a 5% discount for each year of driving.
//----------------------------------------------------------------
if ( UserExperience == 0 )
{
System.out.println("No Discount");
}
else if ( UserExperience < 5 )
{
System.out.println("Discount 5% per year");
double BaseRate = BaseRate2 - ((UserExperience * 0.05)* BaseRate2);
System.out.println(BaseRate);
}
//Quote
System.out.println(UserName + " , your rate is a follows:");
System.out.println(BaseRate); //here is the issue
}
}
问题是最后的 //quote BaseRate 未修改并保持在 4000.0 我真的不想为每种可能性输入一个新的变量名(因为不仅仅是“m or f”以及“experience”驾驶”。我需要一种方法来根据用户输入在整个 if 语句中进行变量更改,以保持该数字(也不要像这个人想要的那样返回)。这些东西应该很简单......我真的沮丧的!