如何使用字符串和joptionpane输出计算数字



我正在尝试获取每个星球名称旁边显示的数据,我尝试了几种不同的方法,但无法让我的脚本显示计算的数字。

import java.awt.*;
import javax.swing.*;
class planetAgeCalculator  {
public static void main(String[] args) {
JOptionPane box = new JOptionPane();
String firstNumber = 
box.showInputDialog(null, "Enter your age to find out how old you would be on the 9 planets ");
int number1 = Integer.parseInt( firstNumber);

    Runnable r = new Runnable() {
        public void run() {
            String pt1 = "<html><body width='";
            String pt2 =
                "'><h1>Your age on different Planets: </h1>" +
                "<p>Mercury: " +
                "<p>Venus: " +
                "<p>Earth: " +
                "<p>Mars: " +
                "<p>Jupiter: " +
                "<p>Saturn: " +
                "<p>Uranus: " +
                "<p>Neptune: " +
                "<p>Pluto: " +
                "";

            JPanel p = new JPanel( new BorderLayout() );
            int width = 0;
            String s = pt1 + width + pt2 + width ;
            JOptionPane.showMessageDialog(null, s);
        }
    };
    SwingUtilities.invokeLater(r);
}
}

除此之外,它运行良好....

是否可以像我尝试使用此方法一样合并数据?

我已经弄清楚了我无法显示数字的计算。例如,当我输入时..

"<p>Venus: " + (number1 * 5 / 3) + 

我只是遇到了很多错误...这甚至可能像我试图这样做的方式一样?

谢谢

我已经弄清楚了我无法将其显示 数字..例如输入时。

您需要在+之后添加""。或在下一行中删除+

尝试这个

+ "<p>Venus: "+(number1 * 5 / 3) +"" 

最新更新