Java—为什么我的变量在Java中显示为0?
我是一名新程序员,正在为课堂做作业。我在计算在不同网站上卖鞋的利润。但是,当我尝试获取可变费用时,输出返回为Java—为什么我的变量在Java中显示为0?,java,Java,我是一名新程序员,正在为课堂做作业。我在计算在不同网站上卖鞋的利润。但是,当我尝试获取可变费用时,输出返回为0。请帮忙,谢谢 我试图使Fee=5
0
。请帮忙,谢谢
我试图使Fee=5但是,输出仍然是0.0
import java.util.Scanner;
public class Shoe
{
private double Cost;
private double SoldPrice;
private double ShippingCost;
private double Fee;
public Shoe()
{
Scanner input = new Scanner(System.in);
System.out.println( " Enter Cost Of Shoe. " );
Cost = input.nextDouble();
System.out.println ( " Enter Sold Price Before Fees. ");
SoldPrice = input.nextDouble();
System.out.println ( " Enter shipping cost. " );
ShippingCost = input.nextDouble();
System.out.println ( " How did you sell the Shoe? [ Stockx , Ebay , Goat , Local , Grailed , Paypal ] ? ");
//user MUST enter the provided sites perfectly.
{
String choice = input.next();
if(choice.equals(" Stockx "))
{
Fee = 0.09;
//determines what the value of Fee should be set to, depending on where the shoe was sold.
}
else if (choice.equals(" Ebay "))
{
Fee = (( 0.029) + 0.3 );
}
else if(choice.equals(" Goat "))
{
Fee = (( 0.095 ) + 5 );
}
else if(choice.equals(" Local "))
{
Fee = 0;
}
else if(choice.equals(" Grailed "))
{
Fee = (( 0.065 ) + 0.3 );
}
else if(choice.equals(" Paypal "))
{
Fee =(( 0.029) + 0.3 );
}
else if (choice.equals(" Test "))
{
Fee = 5;
//attempted to test if it was just rounding down for variables, however, the output was 0 when Fee was 5.
}
System.out.println( +Fee );
}
}
}
费德里科·克莱兹·库洛卡是对的
删除要测试的值周围的空格,它就会工作
为了安全起见,您甚至可以对输入使用trim()
方法,这样即使您键入“Ebay”
,它也会工作。通过正确缩进使代码可读,会增加人们阅读它的机会。您知道,如果您将字符串与“Stockx”
进行比较,则单词周围的空格很重要,对吗?提示:请尝试打印出choice
,这样您就知道“捕获用户输入”部分正在按预期工作。我非常确定Scanner.next()
默认情况下不会返回任何以空格开头或结尾的内容……另外:java中的所有变量名都是camelCase。它应该是费用,或者是成本。对于有经验的java程序员来说,这使得代码更难阅读。