簡體   English   中英

線程“ main”中的異常java.lang.NumberFormatException:對於輸入字符串:“ 0.06”

[英]Exception in thread “main” java.lang.NumberFormatException: For input string: “0.06”

我一直在嘗試為學校項目制作一個簡單的日食計划,但是在輸入利率后,我一直得到這個計划。 一般而言,我對編碼和編程還比較陌生,而從本月開始,java對我來說還是陌生的,因此不勝感激。 代碼是這樣的:

import java.util.Calendar;
import java.util.Locale;
import java.util.Scanner;


public class Interest {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);

    //Input ============================

    System.out.println("Initial loan total:");     //cost
    String cost;
    cost = in.nextLine();

    System.out.println("Down payment:");     //down
    String down;
    down = in.nextLine();

    System.out.println("Length of term:");     //term
    String term;
    term = in.nextLine();

    System.out.println("Interest rate (decimal form):");     //rate
    String rate;
    rate = in.nextLine();

    int principle1 = Integer.parseInt(cost) - Integer.parseInt(down);

    String hundred;
    hundred = "100";

    int interest = Integer.parseInt(rate) * Integer.parseInt(hundred);

    //Output ===========================

    Calendar c = Calendar.getInstance();
    System.out.format("%tB %td, %tY", c,c,c);
    System.out.println("");
    System.out.println("The initial cost of the loan is $" + cost);
    System.out.println("The down payment is $" + down);
    System.out.println("The principle is $" + principle1);
    System.out.println("The term is " + term + " months");
    System.out.println("The interest rate is " + interest + "%");
    System.out.println("The monthly patments are $");

    in.close();
}

}

當我運行該程序時,它讓我投入了初始貸款,預付定金和期限,但是一旦我輸入0.06作為利率,它就會給我錯誤消息。 我還想指出的是,我對代碼中的數學工作原理了解有限。

問題是您正在嘗試將0.06解析為Integer,而0.06是float 使用Float.parseFloat(rate); 您的興趣應該是float interest

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM