繁体   English   中英

为什么当我显示百分比符号时我的程序会崩溃?

[英]Why does my program crash when I display the percentage symbol?

如果我从“toString()”方法中删除“%%”,则该程序可以完美编译和运行。 但是,如果我显示百分比符号,我的程序会在调用“toString()”方法时立即崩溃(在请求输入之后)。 我没能解决这个问题。 有谁能够帮我?

我在 VSCODE 上运行它。

// FA2022_Investor_Souza.java
// Data type class

public class FA2022_Investor_Souza {

    // Fields
    private String name;
    private int shareAmount;
    private float sharePrice;
    private float yearlyDividend;

    // No-arg constructor
    public FA2022_Investor_Souza() 
    {
        name = "";
        shareAmount = 0;
        sharePrice = 0f;
        yearlyDividend = 0f;
    }

    // Parameterized constructor
    public FA2022_Investor_Souza(String n, int a, float p, float y)
    {
        name = n;
        shareAmount = a;
        sharePrice = p;
        yearlyDividend = y;
    }

    // Method to calculate the savings money
    public float savingsMoney()
    {
        float moneyInvested = sharePrice * shareAmount;
        float interestAmount = moneyInvested * yearlyDividend * 0.01f;
        float moneyReturned = moneyInvested + interestAmount;

        return moneyReturned;
    }

    // Method to display the information
    public String toString ()
    {
        float moneyInvested = sharePrice * shareAmount;
        float interestAmount = moneyInvested * yearlyDividend * 0.01f;

        return "-------------------------------------------------------\n" + 
               "FA2022_ShareInvestmentCalculator_Souza.java\n" +
               "FALL 2022 semester - MATHEUS SOUZA\n" + 
               "-------------------------------------------------------\n" +
               String.format("%-32.32s%15s\n", "Name of investor: ", name) +
               String.format("%-32.32s%15d\n", "Number of shares: ", shareAmount) +
               String.format("%-32.32s%15.2f\n", "Price of each share: ", sharePrice) +
               String.format("%-32.32s%15.2f%%\n", "Percentage of yearly dividend: ", yearlyDividend) +  
               String.format("%-32.32s%15.2f\n", "Money Invested: ", moneyInvested) +
               String.format("%-32.32s%15.2f\n", "Interest Amount: ", interestAmount) +
               "-------------------------------------------------------\n" +
               String.format("%-32.32s%15.2f\n", "Total money at the end of year:   ", savingsMoney());
    }
}

// FA2022_ShareInvestmentCalculator_Souza.java
// Driver class

// Importing Scanner class
import java.util.Scanner;

public class FA2022_ShareInvestmentCalculator_Souza {
    public static void main (String [] args)
    {
        // Creating object for Scanner class
        Scanner scanner = new Scanner(System.in);

        // Asking for the user's info
        System.out.printf("What is your name? ");
        String name = scanner.nextLine();
        System.out.printf("What is the number of shares? ");
        int number = scanner.nextInt();
        System.out.printf("What is the price of a share? ");
        float price = scanner.nextFloat();
        System.out.printf("What is the yearly dividend? ");
        float dividend = scanner.nextFloat();

        // Creating object for FA2022_Investor_Souza class using parameterized constructor
        FA2022_Investor_Souza investor = new FA2022_Investor_Souza(name, number, price, dividend);

        // Displaying the result by accessing the method toString()
        System.out.printf(investor.toString());
    }
}

关于这行代码

String.format("%-32.32s%15.2f%%\n", "Percentage of yearly dividend: ", yearlyDividend)           

你想在 yearlyDividend 之后添加另一个 % 作为字符串,对吗?

).然后你需要在格式字符串中告诉另一个字符串在后面( )。 然后将“%%”作为字符串添加为另一个参数,因为 % 需要被另一个 % 转义才能显示为 % 符号。

所以工作代码看起来像这样:

String.format("%-32.32s%15.2f%s\n", "Percentage of yearly dividend: ", yearlyDividend, "%%")           

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM