gpt4 book ai didi

java - 在java中连接计算税率的类

转载 作者:行者123 更新时间:2023-12-01 22:51:27 27 4
gpt4 key购买 nike

此行中的类型不匹配:
final Map<String, Double> taxRates = new Map<>();
显然我做错了什么,无法弄清楚它是什么。
该代码应该能够计算不同国家的税率。谢谢

package tax;

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

public class application{

private static Scanner reader;

public static void main (String[] args) {
//constants
final Map<String, double> taxRates = new Map<>();
taxRates.put( "China", 0.2 );
taxRates.put( "Japan", 0.1 );
taxRates.put( "USA", 0.3 );

reader = new Scanner(System.in);

//Variables
double purchases;
double taxespaid;
double taxRate;
String country;

System.out.print("Enter the country you are purchasing in: ");
country = reader.nextLine();
taxRate = taxRates.get( country );
if( taxRate == null )
{
System.out.println( "Could not find country: " + country );
return;
}

//Request Input
System.out.print("Enter your total amount of purchases in " + country + " :");
purchases = reader.nextDouble();

taxespaid = purchases * taxRate;


//Display Tax
System.out.println("The refund amount you owed is $" + taxespaid);
}

}

最佳答案

java.util.Map是接口(interface),不是类,不能用new实例化。

您将需要选择 Map 的具体实现,例如 HashMap。

但是,您也不能拥有基元的映射,因此需要使用 double 的盒装版本,Double:

final Map<String, Double> taxRates = new HashMap<>(); 

关于java - 在java中连接计算税率的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24678543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com