如何修复税率操作?

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

How do I fix the tax rate operation?

问题

以下是翻译好的内容:

目标是提供总销售额,然而,税率计算不正确,因为它始终输出 0.0。

import java.util.Scanner; // 用于获取用户输入的必要类

public class salesTax {

    public static void main(String[] args) {
        int retailPrice; // 存储零售价
        int taxRate; // 存储销售税率
        double salesTax; // 存储销售税
        double totalSale; // 存储总销售额

        // Scanner 对象以获取用户输入
        Scanner input = new Scanner(System.in);

        // 获取用户输入的零售价
        System.out.println("请输入购买商品的零售价:");
        retailPrice = input.nextInt();

        // 获取用户输入的销售税率
        System.out.println("请输入销售税率:");
        taxRate = input.nextInt() / 100;

        // 显示购买商品的销售税
        salesTax = retailPrice * taxRate; // 计算销售税
        System.out.println("购买商品的销售税为:" + salesTax);

        // 显示销售总额
        totalSale = retailPrice + salesTax; // 计算总销售额
        System.out.println("销售总额为:" + totalSale);
    }
}

是否有方法可以修复税率,以产生准确的计算,考虑到税率是由用户输入的?

英文:

The goal is to provide the total sale, however, the tax rate is not calculating correctly since it keeps outputting 0.0.

import java.util.Scanner; //Required for axquiring user's input

public class salesTax {

	public static void main(String[] args) {
		int retailPrice; //Holds the retail price
		int taxRate; //Holds sales tax rate
		double salesTax; //Holds sales tax
		double totalSale; //Holds total sale
		
		//Scanner object to acquire user's input
		Scanner input = new Scanner(System.in);
		
		//Acquire user's retail price
		System.out.println("What is the retail price of the item being purchased? ");
		retailPrice = input.nextInt();
		
		//Acquire user's sales tax rate
		System.out.println("What is the sales tax rate? ");
		taxRate = input.nextInt() / 100;
		
		//Display the sales tax for the purchase
		salesTax = retailPrice * taxRate; //Calculates the sales tax
		System.out.println("The sales tax for the purchase is: " + salesTax);
		
		//Display the total of the sale
		totalSale = retailPrice + salesTax; //Calculate the total sale
		System.out.println("The total of the sale is: " + totalSale);
	}
}

Is there a way to fix the tax rate to produce accurate calculations, given that the tax rate is inputted by the user?

答案1

得分: 3

taxRate = input.nextInt() / 100;

这会得到 0,因为你正在整数相除。你可以将数字以浮点数的形式输入,然后再除以 100。

float taxRate;
taxRate = input.nextFloat() / 100;
英文:
taxRate = input.nextInt() / 100;

This will give you 0 because you are dividing by an integer. You can take the number in as a float and divide by 100

float taxRate;
taxRate = input.nextFloat() / 100;

答案2

得分: 1

计算税率时,您正在从“System”输入读取一个整数,然后将其除以100,这会得到一个整数结果,我认为您输入的值小于100。您需要从“scanner”以float的形式读取这些值。

请尝试使用以下代码代替:

float taxRate;
taxRate = input.nextFloat() / 100;

编辑:

如评论中所述,taxRate值介于01之间,因此您应将taxRate声明为floatdouble类型。

英文:

To calculate the tax rate you are reading an integer from System input and you are dividing it by 100 which gives an integer result, I think you are entering values less than 100. You need to read the values from the scanner as a float.

try this instead:

float taxRate;
taxRate = input.nextFloat() / 100;

EDIT:

As mentioned in the comments the taxRate value is between 0 and 1, so you should declare the taxRate as float or double.

答案3

得分: 0

您的税率需要是一个double,因为您将其除以100然后赋值给taxRate。由于税率是一个int,它将截断值,只保留整数值。

如果taxRate = 7,然后将其除以100,您会得到0.07。由于taxRate是一个整数值,当Java看到0.07时,它会去掉小数部分,只保留整数部分。因此,假设taxRate = 3.9999,由于taxRate是一个int,它会截断值,只保留3。要解决此问题,请将taxRate更改为double

另外,您需要将taxRate读取为一个双精度值。要使用Scanner读取双精度值,将其改为taxRate = input.nextDouble() / 100;

英文:

Your tax rate needs to be a double since you're dividing it by 100 and then assigning it taxRate. Since tax rate is an int, it will truncate the value leaving only the integer value.

If taxRate = 7 and then you divide it by 100, you get 0.07. Since taxRate is an Integer value, when Java sees said 0.07 it will get rid of the decimal and only leave the whole number. Therefore, say taxRate = 3.9999, since taxRate is an int, it will truncate the value leaving 3. To fix this, change taxRate to a double.

Also, you need to read taxRate as a double value. To read a double value using Scanner, it will be taxRate = input.nextDouble() / 100;

huangapple
  • 本文由 发表于 2020年10月7日 22:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246792.html
匿名

发表评论

匿名网友

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

确定