如何解决:“必须为数组类型,但必须解析为字符串”

时间:2019-10-21 06:41:54

标签: java

创建一个程序,该程序使用菜单来调用简单健康跟踪器的各个模块,以进行初学者编程课程。 感谢您提供有关数组无法正常工作并“解析为字符串”的确切原因的帮助 在提交程序之前,我还有很多要补充的内容,但这阻碍了我。

它位于Module 3中,该行试图重新调用数组

由于我不了解自己做错了什么,所以我将到目前为止的所有代码都留在了这里,希望这个地方比uni上无用的论坛更有帮助。

public class HealthMate {


    double bmi, bmr, heightM, weightKG;
    int age, week = 7, days = 1;
    int calories[] = new int[days];
    int menuChoiceInt;
    char genderChar;
    boolean male;

    public static void main(String[] args) {
        HealthMate firstObj = new HealthMate(); 
        firstObj.menu();
    }

    public void menu() {
        while (menuChoiceInt != 4) {

            String menu = "HealthMate Alpha 0.1 \n " + "Please make a numerical selection \n";
            menu += "[1] Enter or Update your Details\n";
            menu += "[2] Return BMI and BMR \n"; // menu options call different modules
            menu += "[3] Weekly Tracker and Advice \n";
            menu += "[4] Exit \n";
            String menuChoiceString = JOptionPane.showInputDialog(menu);
            menuChoiceInt = Integer.parseInt(menuChoiceString);//

            if (menuChoiceString != null) {

                if (menuChoiceInt == 1) {

                    genderChar = JOptionPane.showInputDialog("Please Enter your Gender as either M or F").charAt(0);

                    heightM = Double.parseDouble(
                            JOptionPane.showInputDialog("Enter Height in Meters,\n eg 1.73 for 173 cm.: "));
                    if (heightM <= 0) {
                        heightM = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
                    }
                    weightKG = Double.parseDouble(JOptionPane.showInputDialog("Enter Weight in Kilograms"));
                    if (weightKG <= 0) {
                        weightKG = Double.parseDouble(JOptionPane.showInputDialog("Error! Enter a postitive number"));
                    }
                    bmi = weightKG / Math.pow(heightM, 2.0);
                    male = genderChar == 'M';
                    if (male) {
                        bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) + 5; 

                    } else {
                        bmr = (10 * weightKG) + (62.5 * heightM) - (5 * age) - 161; 
                        JOptionPane.showMessageDialog(null,"Your Specific BMI and BMR have been ");
                        menuChoiceInt = Integer.parseInt(menuChoiceString);// recall menu
                    }
                }

                if (menuChoiceInt == 2) if (bmi < 18.5) {
                    JOptionPane.showMessageDialog(null,
                            "Your BMI is " + bmi + ", You are underweight.\n" + "Your BMR is " + bmr);
                } else if (bmi < 25) {
                    JOptionPane.showMessageDialog(null, "Your BMI is " + bmi
                            + ", You are within the healthy weight range.\n" + "Your BMR is " + bmr);
                } else if (bmi < 30) {
                    JOptionPane.showMessageDialog(null,
                            "Your bmi is " + bmi + ", You are overweight\n" + "Your BMR is " + bmr);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Your bmi is " + bmi + ", You are Obese" + "Your BMR is " + bmr);

                }
                JOptionPane.showMessageDialog(null,
                        "This module is supposed to recall your BMI and BMR \n"
                                + "and give general advice on health.");
                {                   
                    menuChoiceInt = Integer.parseInt(menuChoiceString);
                }
                if (menuChoiceInt == 3) {
                    while (days > week) {
                    calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day"[days]);// employee salary
                    days = days + 1;
                    JOptionPane.showMessageDialog(null,
                            "This module is supposed to store data in an array over the course \n"
                                    + "of a week to show you your pattern of intake vs output.");
                    }
                    {   
                        menuChoiceInt = Integer.parseInt(menuChoiceString);
                    }
                } else if (menuChoiceInt == 4) {

                }
            }
        }
    }
}

我正在尝试在7天内保存卡路里输入,以便将其平均化,将其与BMRActivity的水平进行比较,并就是否您提供一般建议卡路里摄入过多或不足。

PS:也许如果您有多年的经验,请不要以“很明显……”开始回答,并继续嘲笑不到一个月前开始编程的人,就像您经常看到的那样。这个网站。

1 个答案:

答案 0 :(得分:1)

您有int days = 1,但您将其用作[day]-在java中不正确:

calories[week] = Integer.parseInt(JOptionPane.showInputDialog("Enter Calories for day " + days));