简体   繁体   English

如何让用户输入双倍?

[英]how do i make the user input a double?

can somebody correct me what i did wrong here?有人可以纠正我在这里做错了什么吗? my goal was for the user to input a number on the parameter System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));我的目标是让用户在参数 System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1)); 上输入一个数字。 double upgradeAccessories= sc.nextDouble();双升级附件= sc.nextDouble(); but i can't seem to see the issue但我似乎看不到问题

public static void main(String[] args) 
{
    DecimalFormat df = new DecimalFormat("#####");
    Scanner sc = new Scanner(System.in);
    
    double availAmount;
    
    Car owner = new Car();
    
    owner.owner("Marcus Laurence", 2014);
    
    double availAmount1=owner.upgradeAccessories(availAmount);
    double remainAmount=owner.upgradeAccessories(availAmount);
    
    System.out.println("Owner:" + owner.name);
    System.out.println("Model:" + owner.model);
    System.out.print("Available Amount Before Upgrade:" + df.format(availAmount1));
    double upgradeAccessories= sc.nextDouble();
    System.out.println("Installed AC:" + owner.hasAC);
    System.out.println("Installed Leather Seats:" + owner.hasLeatherSeats);
    System.out.println("Installed Back Wipers:"+ owner.hasBackWipers);
    System.out.println("Installed Fog Lights:" + owner.hasFogLights);
    System.out.println("Amount Remaining After Upgrade:" + df.format(remainAmount));
}

public double upgradeAccessories(double availAmount) 
{          

        if(availAmount == 25000)
        {
            availAmount -= 21500;
            hasAC=true;
        }
        
        else if(availAmount == 40000) 
        {
            availAmount -= 21500;
            availAmount -= 14400;
            hasAC=true;
            hasLeatherSeats=true;
        }
        else if(availAmount == 50500) 
        {
            availAmount -=21500;
            availAmount -=14400;
            availAmount -=6250;
            availAmount -=3300;
            hasAC=true;
            hasLeatherSeats=true;
            hasBackWipers=true;
            hasFogLights=true;
        }
        return availAmount;
    }   

In this segment of your code...在这段代码中......

double availAmount;
    
Car owner = new Car();
    
owner.owner("Marcus Laurence", 2014);
    
double availAmount1=owner.upgradeAccessories(availAmount);

... you passed the "availAmount" in to a function as a parameter. ...您将“availAmount”作为参数传递给函数。 You haven't given that variable any value yet.你还没有给那个变量任何值。 You just declared it.你刚刚宣布了。 If you assign a default value to it like 0.0, does it work?如果你给它分配一个默认值,比如 0.0,它会起作用吗?

how do i make the user input a double?如何让用户输入双倍?

You did this part fine - scanner.nextDouble() is all it takes.这部分你做得很好scanner.nextDouble()就足够了。 Here's a really simple standalone example, including output:这是一个非常简单的独立示例,包括输出:

System.out.print("enter a double: " );
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
System.out.println("d: " + d);

enter a double: 123.456
d: 123.456

So reading a double is not the issue.所以阅读双份不是问题。 However, this line is suspicious:但是,这条线是可疑的:

double upgradeAccessories = sc.nextDouble();

Why?为什么? A few reasons..几个原因。。

  • It defines a new variable "upgradeAccessories" which is not used anywhere else – so it's reading a value (from sc.nextDouble() ) and then doing nothing with it.它定义了一个新变量“upgradeAccessories”,它不会在其他任何地方使用——所以它正在读取一个值(来自sc.nextDouble() ),然后什么也不做。 This is something that an IDE (such as IntelliJ or Eclipse) will help you see – it will draw attention to unused code, like declaring a value that is never used.这是 IDE(例如 IntelliJ 或 Eclipse)将帮助您看到的东西——它会引起人们对未使用代码的注意,例如声明一个从未使用过的值。
  • The name of the variable – "upgradeAccessories" – is the same as the method name defined elsewhere in your code, namely:变量的名称 - “upgradeAccessories” - 与代码中其他地方定义的方法名称相同,即:
     public double upgradeAccessories(double availAmount)

So to fix your code, it seems that you probably want to replace this:因此,要修复您的代码,您似乎可能想要替换它:

double upgradeAccessories = sc.nextDouble();

with something like this:像这样:

double next = sc.nextDouble();
owner.upgradeAccessories(next);

Also, the way you're comparing doubles is dangerous.此外,您比较双打的方式是危险的。 Generally, fractional numbers are never exactly equal.通常,小数永远不会完全相等。 Instead, of doing comparisons like this:相反,不要像这样进行比较:

if (availAmount == 25000) { ... }

It's better to do something like greater-than-or-equal:最好做类似大于或等于的事情:

if (availAmount >= 25000) { ... }

Or if you're using only whole-number values, use integer instead of double, and then direct x == y comparisons will work fine.或者,如果您只使用整数值,请使用整数而不是双精度,然后直接x == y比较就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM