您好,SO社区,我最近将程序从“扫描程序”输入和显示更改为JOptionPane输入和显示,它被设置为显示用户指定年份的投资

当前,如果年数超过3,它将终止程序,我将如何解决该问题和/或创建一个弹出窗口以显示结果?预先谢谢大家。

import java.util.*;
import javax.swing.JOptionPane;

public class Investing {

public static void main (String[] args){

    double amount;

    double principal;

    double rate;

    Scanner input1 = new Scanner(System.in);

    String principals = JOptionPane.showInputDialog("Please Enter The Principal");
    principal = Double.parseDouble(principals);

    String rates = JOptionPane.showInputDialog("Please Enter The Rate Of Interest");
    rate = Double.parseDouble(rates);

    String years = JOptionPane.showInputDialog("Please Enter The Number Of Investment Years");
    double g = Double.parseDouble(years);
    String zs = JOptionPane.showInputDialog( "Please Enter Annual Contribution");
    double z = Double.parseDouble(zs);

    amount = principal;
    for(int year = 1; year <= g; year++){
        amount *= 1 + rate/100;
        amount+= z;
        JOptionPane.showMessageDialog( null, year + "  " + amount, zs, year);
    }
}
}

最佳答案

您错误地使用了JOptionPane

JOptionPane.showMessageDialog( null, year + "  " + amount, zs, year);


您正在以年份作为消息类型,因此它在第3年之后失败。您应该这样做

JOptionPane.showMessageDialog( null, year + "  " + amount, "title", JOptionPane.INFORMATION_MESSAGE );

07-24 09:37
查看更多