[英]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.