gpt4 book ai didi

java - 尽管通过 nextInt() 作为整数输入,但仍被接受为字符串

转载 作者:行者123 更新时间:2023-12-02 09:02:06 25 4
gpt4 key购买 nike

import java.lang.String;
import java.lang.System;

import java.text.DecimalFormat;

import java.io.IOException;

import java.util.Scanner;

public class StorePrice
{
public static void main (String args[]) throws IOException
{
int taxRate, markupRate, price;
Scanner prompt = new Scanner(System.in);
DecimalFormat decimal = new DecimalFormat("#,##0.00");

System.out.print("Input the original price: ");
price = prompt.nextInt();
System.out.print("Input the markup rate: ");
markupRate = prompt.nextInt();
System.out.print("Input the tax rate: ");
taxRate = prompt.nextInt();

markupRate = decimal.format(markupRate);
taxRate = decimal.format(taxRate);

System.out.print("Original price: " + price + "\n");
System.out.print("Markup rate: " + markupRate + "\n");
System.out.print("Selling price: " + (price + (price*markupRate)) + "\n");
System.out.print("Tax rate: " + taxRate + "\n");
System.out.print("Tax: " + (price*taxRate) + "\n");
System.out.print("Final price: " + ((price*markupRate) + (price*taxRate)) + "\n");
}
}

您好,我是 OOP 和 Java 本身的新手,我在通过 nextInt(); 输入的数据类型时遇到问题。错误是这样的:

C:\Stuffs\Codes\Java\StorePrice.java:25: error: incompatible types
markupRate = decimal.format(markupRate);
^
required: int
found: String
C:\Stuffs\Codes\Java\StorePrice.java:26: error: incompatible types
taxRate = decimal.format(taxRate);
^
required: int
found: String
2 errors

为什么输入的是整数还是字符串?问题出在哪里?

编辑:我忘记补充一点,我被严格指示在代码中将 pricemarkupRatetaxRate 声明为整数。

最佳答案

在将变量赋值给变量时,您必须了解所调用方法的返回类型(当它不是由您创建时,您可以在文档中查找)。在本例中,您尝试将 INT markupRate 和税率分配给从decimal.format(int) 返回的字符串变量。所以你要做的只是创建一些 String 变量来保存返回的字符串,但你仍然需要 int 作为参数进行格式化。试试这个:

    public static void main (String args[]) throws IOException
{
int taxRate, markupRate, price;
String sMarkupRate, sTaxRate;
String sSellingPrice, sTax, sFinalPrice;
Scanner prompt = new Scanner(System.in);
DecimalFormat decimal = new DecimalFormat("#,##0.00");

System.out.print("Input the original price: ");
price = prompt.nextInt();
System.out.print("Input the markup rate: ");
markupRate = prompt.nextInt();
System.out.print("Input the tax rate: ");
taxRate = prompt.nextInt();

sMarkupRate = decimal.format(markupRate);
sTaxRate = decimal.format(taxRate);
sSellingPrice = decimal.format(price + (price*markupRate));
sTax = decimal.format((price*taxRate));
sFinalPrice = decimal.format((price*markupRate) + (price*taxRate));


System.out.print("Original price: " + price + "\n");
System.out.print("Markup rate: " + sMarkupRate + "\n");
System.out.print("Selling price: " + sSellingPrice + "\n");
System.out.print("Tax rate: " + sTaxRate + "\n");
System.out.print("Tax: " + sTax + "\n");
System.out.print("Final price: " + sFinalPrice + "\n");
}
}

关于java - 尽管通过 nextInt() 作为整数输入,但仍被接受为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60086182/

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