[英]Having trouble calling method from another class. The program either won't compile or returns a null value
This is how I am calling the method in MovieRatingApp class.这就是我在 MovieRatingApp 类中调用方法的方式。
movie1.getRatingDesc()
This is the getRatingDesc() method in the Movie class.这是 Movie 类中的 getRatingDesc() 方法。
public String getRatingDesc(int reviewNum)
{
String s = "";
switch (reviewNum)
{
case 1:
s = "Terrible";
break;
case 2:
s = "Bad";
break;
case 3:
s = "Ok";
break;
case 4:
s = "Good";
break;
case 5:
s = "Great";
break;
}
return s;
}
Every time I try to call the method I get an error: method getRatingDesc() from class Movie cannot be applied to given types.每次我尝试调用该方法时,我都会收到一个错误:来自类 Movie 的方法 getRatingDesc() 不能应用于给定的类型。
When I change how I call my method to, movie1.getRatingDesc(int numReview) I get a different error.当我改变我调用我的方法的方式时,movie1.getRatingDesc(int numReview) 我得到一个不同的错误。 But what I really want to do is return the value of the variable "s" which describes the rating.
但我真正想做的是返回描述评级的变量“s”的值。
Just pass in the variable in your call.只需在调用中传入变量即可。
int numReview; //declare numReview.
Scanner sc = new Scanner(System.in); //Assuming you're going to have the user type it in.
numReview = sc.nextInt(); //Accept next integer from console, store in variable.
System.out.println(movie1.getRatingDesc(numReview)); //Call it, print result.
Note that error handling is omitted.请注意,省略了错误处理。 If somebody tries to enter text or a number that is unexpected, you'll need to be responsible for how that's treated.
如果有人试图输入意外的文本或数字,您需要对其处理方式负责。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.