Java – 为什么我的变量在Java中显示为0?

huangapple go评论72阅读模式
英文:

Java - Why is my variable showing up as 0 In Java?

问题

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("请输入鞋子的成本。");
        Cost = input.nextDouble();
        System.out.println("请输入未计算手续费的售价。");
        SoldPrice = input.nextDouble();
        System.out.println("请输入运费。");
        ShippingCost = input.nextDouble();
        System.out.println("你是通过哪个网站出售的?[ Stockx , Ebay , Goat , Local , Grailed , Paypal ] ?");
        // 用户必须完全按照提供的选项输入。
        {
            String choice = input.next();
            if (choice.equals("Stockx")) {
                Fee = 0.09;
                // 根据出售的网站确定手续费的值。
            } 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;
                // 尝试测试变量是否被舍入为零,然而当Fee为5时,输出为0。
            }
            System.out.println("手续费为:" + Fee);
        }
    }
}
英文:

I'm a new programmer doing an assignment for class. I'm trying to calculate the profit of selling a shoe on different websites. However, when I try to get variable Fee the output comes back as 0. Please help thanks!

I attempted to make Fee = 5; however, the output was still 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 );
}
}
}

答案1

得分: 1

Federico Klez Culloca 是正确的。

去除围绕测试值的空格,它将会起作用。

为了安全起见,甚至可以在输入上使用 trim() 方法,这样即使您键入 " Ebay ",它也会起作用。

英文:

Federico klez Culloca is right.

Remove the spaces surrounding the values to test and it will work.

For safety, you can even use the trim() method on your input so even if you type " Ebay ", it will work.

huangapple
  • 本文由 发表于 2020年9月2日 16:00:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63701155.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定