我创建的表格无法与其余代码一起执行

huangapple go评论65阅读模式
英文:

The table I created won't execute with the rest of the code

问题

public class BodyMassIndex extends JFrame {
    Scanner in = new Scanner(System.in);

    String user_name;
    String user_height;
    String user_weight;
    Double user_BMI;
    String User_Category;

    JTable jt;

    String[] column_headers = {"Name", "Height (m)", "Weight (Kg)", "BMI", "Category"};
    String[][] user_statistics = {{user_name, user_height, user_weight, String.valueOf(user_BMI), User_Category}};

    public BodyMassIndex() {
        System.out.print("Enter Your Full Name: ");
        user_name = in.nextLine();
        System.out.print("Enter Your Weight in Pounds: ");
        user_weight = in.nextLine();
        System.out.print("Enter Your Height in Inches: ");
        user_height = in.nextLine();

        Double Height_M = (Integer.parseInt(user_height) * 0.0254);
        Double Weight_KG = (Integer.parseInt(user_weight) * 0.453);
        user_BMI = (Integer.parseInt(user_weight) / (Math.pow(Integer.parseInt(user_height), 2)));

        if (user_BMI < 18.5) {
            User_Category = "Underweight";
        } else if (user_BMI <= 24.9 && user_BMI >= 18.5) {
            User_Category = "Normal or Healthy Weight";
        } else if (user_BMI <= 29.9 && user_BMI >= 25.0) {
            User_Category = "Overweight";
        } else {
            User_Category = "Obese";
        }

        user_statistics[0][0] = user_name;
        user_statistics[0][1] = user_height;
        user_statistics[0][2] = user_weight;
        user_statistics[0][3] = String.valueOf(user_BMI);
        user_statistics[0][4] = User_Category;

        jt = new JTable(user_statistics, column_headers);
        jt.setBounds(50, 50, 200, 230);
        JScrollPane js = new JScrollPane(jt);
        this.add(js);
        this.setSize(300, 400);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new BodyMassIndex();
    }
}

Note: I've made the necessary changes to the code to ensure that user input values are properly assigned to the user_statistics array before creating the JTable. The issue with the table not displaying was due to the incorrect assignment of values to the array. Now the code should display the table with the user's input information and calculated BMI category.

英文:
public class BodyMassIndex extends JFrame {
Scanner in = new Scanner (System.in);
String user_name;
String user_height;
String user_weight;
Double user_BMI;
String User_Catagory;
JTable jt;
String[] column_headers = {&quot;Name&quot;,&quot;Height (m)&quot;,&quot;Weight (Kg)&quot;, &quot;BMI&quot;, &quot;Catagory&quot;};
String[][] user_statistics = {{user_name, user_height, user_weight , String.valueOf(user_BMI) , User_Catagory}};
public BodyMassIndex() {
System.out.print(&quot;Enter Your Full Name: &quot;);
user_name = in.nextLine();
System.out.print(&quot;Enter Your Weight in Pounds: &quot;);
user_weight = in.nextLine();
System.out.print(&quot;Enter Your Height in Inches: &quot;);
user_height = in.nextLine();
Double Height_M = (Integer.parseInt(user_height) * 0.0254);
Double Weight_KG = (Integer.parseInt(user_weight) * 0.453);
user_BMI = (Integer.parseInt(user_height)/(Math.pow(Integer.parseInt(user_weight) , 2)));
if (user_BMI &lt; 18.5) {
User_Catagory = &quot;Underweight&quot;;
}
else if (user_BMI &lt;= 24.9 &amp;&amp; user_BMI &gt;= 18.5) {
User_Catagory = &quot;Normal or Healthy Weight&quot;;
}
else if (user_BMI &lt;= 29.9 &amp;&amp; user_BMI &gt;= 25.0) {
User_Catagory = &quot;Overweight&quot;;
}
else {
User_Catagory = &quot;Obese&quot;;
}
jt = new JTable(user_statistics, column_headers);
jt.setBounds(50,50,200,230);
JScrollPane js = new JScrollPane(jt);
this.add(js);
this.setSize(300,400);
this.setVisible(true);
}
public static void main(String[] args) {
new BodyMassIndex();
}
}

I imported everything needed and typed the code above, but when it runs, the program only asks the three questions then it just stops. A table is supposed to display. It's not. How do I make it work? Am I not allowed to use JTable with Scanner? I need to get information from the user then display it in a table.

答案1

得分: 2

我已经复制粘贴了你的代码。
它对我来说显示了一个空表格。
这是因为你的 user_statistics 字段。
你使用其他字段(user_name、user_height 等)为它赋值。

请注意,所提到的字段在那个时候没有任何值,因此当你在 JTable 构造函数中使用它时,你的 user_statistics 是空的。
你应该在从用户那里获取数据后将这些值赋给它。

简单来说,在调用 JTable 构造函数之前,你可以复制这行代码:

String[][] user_statistics = {{user_name, user_height, user_weight, String.valueOf(user_BMI), User_Catagory}};
jt = new JTable(user_statistics, column_headers);

在我这样做之后,它对我显示了数据。

我创建的表格无法与其余代码一起执行

英文:

I have copy pasted your code.
It displayed an empty table for me.
The reason for this is your user_statistics field.

You assigned values to it using your other fields (user_name, user_height etc.).

Note that the mentioned fields do not have any value at the time thus your user_statistics is empty when you use it in the JTable constructor.
You should assign these values to it after you got the data from the user.

Simply put you can copy this line just before you are calling JTable constrcutor:

String[][] user_statistics = {{user_name, user_height, user_weight , String.valueOf(user_BMI) , User_Catagory}};
jt = new JTable(user_statistics, column_headers);

After I did this it displayed the data fine for me.

我创建的表格无法与其余代码一起执行

huangapple
  • 本文由 发表于 2020年10月6日 23:09:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64228664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定