为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

白猪掌柜的 2023-03-23 16:57:39
我进行了一些调试作业,但似乎无法运行代码……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 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

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


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.

    }


}


查看完整回答
反对 回复 2023-03-23
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信