java - 存储一个 double 以便在整个 if 语句中使用

标签 java loops

我是一名初学者程序员,已经玩了几天了......我似乎找不到一种方法来存储 double ,通过一系列 if 语句运行它,然后在 System.out.println("...") 中显示该 double .

我尝试过使用数组、开关等。我需要通过更多操作来运行此 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

}

}

问题是最终的//引用 BaseRate 未修改并保持在 4000.0 我真的不想为每种可能性输入一个新的变量名(因为不仅仅是“m或f”以及“体验驾驶”。我需要一种在整个 if 语句中改变变量的方法,具体取决于在用户输入上保持该数字(而不是像这个人想要的那样返回)。这个东西应该很简单......我只是感到非常沮丧!

最佳答案

The closest I have come up with is storing the BaseRate through a couple of if statements

问题在于,在这些 if 语句中,您将 BaseRate 重新声明为局部变量:

if (UserSex == 'f')
{
    System.out.println("Discount 5%");
    double BaseRate = BaseRate2 * 0.95;
    System.out.println(BaseRate);
}

这根本不会改变静态变量的值...并且当变量在 block 末尾超出范围时,局部变量的值实际上是无用的。

只需更改这样的代码即可为现有变量分配新值:

if (UserSex == 'f')
{
    System.out.println("Discount 5%");
    BaseRate = BaseRate2 * 0.95;
    System.out.println(BaseRate);
}

或者,完全删除静态变量,并在所有 if block 之前在方法内声明一个局部变量 - 目前尚不清楚为什么您需要静态变量。

此外,我建议您使用更传统的大括号样式并使用驼峰命名法作为变量名称。

关于java - 存储一个 double 以便在整个 if 语句中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991931/

相关文章:

jquery - 我如何在此表单中使用 jQuery .each() ?

python - 避免嵌套for循环python

c# - 在 Winforms 窗体中滑动

java - HBase 中要批量删除多少个?

java - ArrayList "cannot be resolved"尝试在方法中访问 ArrayList 时,Java

java - 如何在异常发生后继续循环 do while

java - 在 Java 'for loop' 增量器中什么样的语句是合法的?

java,在大字符串输入的某个字符处计数的代码

java - Arquillian Cube 基本示例/教程 JMX 远程访问超时

java - 自定义安卓组件