我正在做Java家庭作业。我必须使用if,else if,else语句来输出一条语句。
可以生成的三种可能性:
示例1:用户输入7,输出为“您居住的距离MCTC不到10英里”。
示例2:用户输入11,输出为“您居住的距离MCTC超过10英里”。
示例3。用户输入10,输出为“您距离MCTC恰好10英里”。
这里4 images显示我的问题。
我已经在讲师提供的作业的代码之外创建了自己的版本,并获得了所需的结果。
当我删除行:return null; // TODO delete this line and replace with your code.
时,将鼠标悬停在第二到最后一个花括号上时会收到错误消息:missing return statement
运行该程序时,我发布了一张显示错误的图像。
public class Question_1_Miles_From_MCTC {
public static void main(String[] args) {
double miles = doubleInput("How many miles do you live from MCTC? ");
String response = milesFromMCTC(miles);
System.out.println(response);
}
public static String milesFromMCTC(double miles){
// TODO Use if - else if - else statements to return the correct String
// Return "You live more than 10 miles from MCTC" if they live more than 10 miles away,
// Return "You live exactly 10 miles from MCTC" if they live exactly 10 miles away,
// Return "You live less than 10 miles from MCTC" if they live less than 10 miles away.
if (miles > 10) {
System.out.println("You live more than 10 miles from MCTC");
} else if (miles == 10){
System.out.println("You live exactly 10 miles from MCTC");
} else {
System.out.println("You live less than 10 miles from MCTC");
}
return null; // TODO delete this line and replace with your code.
}
}
答案 0 :(得分:2)
这是在方法不终止的情况下如何返回适当的字符串的方法:
public static String milesFromMCTC(double miles){
if (miles > 10) {
return "You live more than 10 miles from MCTC";
} else if (miles == 10){
return "You live exactly 10 miles from MCTC";
} else {
return "You live less than 10 miles from MCTC";
}
}
然后从调用函数中打印返回的字符串
答案 1 :(得分:0)
如果删除return null;'
,则不会返回任何内容。由于您已在方法中将返回类型声明为String
类型,因此您需要返回一个String
值或null。因为这是家庭作业,应该足以使您前进。
答案 2 :(得分:0)
您要么需要通过将其分配给变量来返回字符串,要么需要在函数声明中列出“ void”而不是String。
答案 3 :(得分:0)
Let's try为刚刚开始的我们中的一些人更详细地解释这一点...
在Java和许多其他强类型语言中,方法(例如milesFromMCTC(double miles)
类中的Question_1_Miles_From_MCTC
)需要具有指定的返回类型,对于您的方法,返回类型为String
–相当于编程术语中的一系列字符/文本。这意味着在结束对该方法的调用之前,必须在其内部返回一个String
,以便其他程序员可以保证他们在使用您的方法时获得正确的值和期望的数据类型。在您的代码中,这由以下语句显示:String response = milesFromMCTC(miles);
,在其中您希望方法的返回值为String。如果该方法未返回任何内容,那么response
变量将存储什么?
您采取的方法尽管对家庭作业显然有效,但它会改变练习的方向-您正在使用System.out.println
打印方法调用的结果,但同时必须返回时间 any 字符串,因此IntelliJ抱怨缺少return语句。考虑到Java中的String甚至null
都可以像it is not a primitive datatype一样被Java接受,而不是其他语言。
另一方面,有一个特殊的返回类型称为void
,它使您可以调用方法并执行操作而不必返回任何内容,例如以下示例:
public class HelloWorldClass {
public static void main (String[] args) {
// Here our method with void return type is called, which prints Hello
sayHello();
}
private static void sayHello() {
System.out.println("Hello!");
}
}
实际上,如果您查看代码,已经有一种方法什么也不返回(因此返回void
的类型)–您的main()
!
因此,既然我们知道我们要么在方法返回类型为String
时必须返回String
,要么在返回类型为void
时不返回任何内容,请尝试思考如何实现修改代码以解决问题并输出正确的结果。然后在下面检查您是否正确:)
您的老师教的解决方案:
// This is Java code so it will not run in StackOverflow snippet
public class Question_1_Miles_From_MCTC {
public static void main(String[] args) {
double miles = doubleInput("How many miles do you live from MCTC? ");
// get the correct answer as String (text) from your method and store it in response
String response = milesFromMCTC(miles);
// print the result here by passing response to System.out.println
System.out.println(response);
}
public static String milesFromMCTC(double miles){
// TODO Use if - else if - else statements to return the correct String
// Return "You live more than 10 miles from MCTC" if they live more than 10 miles away,
// Return "You live exactly 10 miles from MCTC" if they live exactly 10 miles away,
// Return "You live less than 10 miles from MCTC" if they live less than 10 miles away.
if (miles > 10) {
return "You live more than 10 miles from MCTC";
} else if (miles == 10){
return "You live exactly 10 miles from MCTC";
} else {
return "You live less than 10 miles from MCTC";
}
}
}
根据您的方法制定的解决方案,适用于修复丢失的return语句:
// This is Java code so it will not run in StackOverflow snippet
public class Question_1_Miles_From_MCTC {
public static void main(String[] args) {
double miles = doubleInput("How many miles do you live from MCTC? ");
// Now as our method is void we cannot store it in a variable, so we will let it print our answer
milesFromMCTC(miles);
// There are not applicable now
// String response = milesFromMCTC(miles);
// System.out.println(response);
}
// we have to change the return type here to void so that we don't have to return anything
public static void milesFromMCTC(double miles){
// TODO Use if - else if - else statements to return the correct String
// Return "You live more than 10 miles from MCTC" if they live more than 10 miles away,
// Return "You live exactly 10 miles from MCTC" if they live exactly 10 miles away,
// Return "You live less than 10 miles from MCTC" if they live less than 10 miles away.
if (miles > 10) {
System.out.println("You live more than 10 miles from MCTC");
} else if (miles == 10){
System.out.println("You live exactly 10 miles from MCTC");
} else {
System.out.println("You live less than 10 miles from MCTC");
}
}
}
最后一点,在实际的编程场景中,您宁愿使用第一种解决方案,因为它可以让您对答案做任何想做的事情,而不是被迫将其打印到控制台上(在大多数情况下仅打算这样做)用于调试。
希望这有助于了解返回类型。祝您功课顺利!