Java Can';t似乎将int转换为string并运行我的程序,使其输出330
我做了一些家庭作业调试,我似乎无法让代码运行。。。java新手,如有任何反馈,将不胜感激Java Can';t似乎将int转换为string并运行我的程序,使其输出330,java,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(a
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.
}
}
有一些错误,但我解决了,下面是答案。非常基本的东西。我希望你能得到它
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.
}
}
更改String ageStr=“24年”代码>到字符串ageStr=“24”代码>,int three=“3”
到String three=“3”代码>,int-answerYears=年龄+三岁代码>至<代码>内回答年份=年龄+三岁字符串代码>应该可以编译,但我强烈建议您花时间学习这些基础知识