【问题标题】:Accepted as string despite being inputted as integer through nextInt()尽管通过 nextInt() 作为整数输入,但仍被接受为字符串
【发布时间】:2020-05-21 23:32:16
【问题描述】:
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 声明为整数。

【问题讨论】:

  • markupRatetaxRate 被声明为 int 的类型,但是您尝试将 Strings 分配给它们,这没有意义。您可能需要单独的变量来存储Strings 的格式化结果
  • 对不起,但我忘了告诉我,我必须严格按照我的指示将它们初始化为整数。
  • 可以,所以使用不同的变量来存储格式化结果

标签: java string input integer decimalformat


【解决方案1】:

您必须知道在将其分配给变量时调用的方法的返回类型(如果不是您创建的,您可以在文档中查找)。在这种情况下,您尝试将 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");
    }
}

【讨论】:

  • 最好改为String sMarkupRate = decimal.format(markupRate);,即声明最接近您使用的地方
  • 是的。尽可能使用局部变量而不是全局变量
  • 与局部变量和全局变量无关。我是说,当您一次性声明和初始化时,在方法的顶部声明变量是没有意义的。使用您的代码,代码的读者必须搜索变量sMarkupRate 以找出它是什么类型的变量。
  • 对不起,我忘了包括我的变量 taxRate、markupRate 和 price 的初始化应该严格地是整数。我尝试做markupRate = Integer.parseInt(decimal.format(markupRate));,但它仍然有点不起作用,仍然被认为是一个字符串作为返回
猜你喜欢
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多