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

我进行了一些调试作业,但似乎无法运行代码……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.

    }


}


白猪掌柜的
浏览 90回答 1
1回答

繁星淼淼

有一些错误,但我解决了,这就是答案。很基本的东西。我希望你能明白。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.    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java