似乎无法将int转换为字符串并运行我的程序,因此它输出330

时间:2019-07-13 21:07:01

标签: java

我进行了一些家庭作业调试,但似乎无法运行代码... Java的新手,希望您能收到反馈

public class Errors {

    public static void main(String[] args) {

        System.out.println("Welcome to my first program!\n");

        String ageStr = "24 years";

        int age = Integer.parseInt(ageStr);

        System.out.println("I'm " + age + " years old.");

        int three = "3";
        int threeToString = Integer.parseInt(three);

        int answerYears = age + three;

        System.out.println("Toal number of years: " + answerYears);

        int answerMonths = answerYears * 12;

        System.out.println("In 3 years and 6 months, I'll be " + answerMonths + " months old");

        // Once you've corrected all the errors, the answer should be 330.
    }

}

1 个答案:

答案 0 :(得分:2)

有一些错误,但我解决了,这就是答案。很基本的东西。我希望你能明白。

public class Errors {

    public static void main(String[] args) {

        System.out.println("Welcome to my first program!\n");

        String ageStr = "24";                           //24years is wrong input, enter only 24

        int age = Integer.parseInt(ageStr);

        System.out.println("I'm " + age + " years old.");

        String three = "3";
        int ageToString = Integer.parseInt(ageStr);
        int threeToString = Integer.parseInt(three);
        int answerYears = ageToString + threeToString;

        System.out.println("Toal number of years: " + answerYears);

        int answerMonths = answerYears * 12 + 6;                //you forgot to add 6 months

        System.out.println("In 3 years and 6 months, I'll be " + answerMonths + " months old");

        // Once you've corrected all the errors, the answer should be 330.
    }

}