Java 为什么赢了';这种方法不叫工作吗?
我正在创建一个方法来接受用户的输入并验证它,以确保它是正确的。如果它是正确的,它将调用一个方法并将用户的输入输入其中。但由于某些原因,方法调用无法工作。它不会产生任何错误,只是不会产生错误。我在代码的末尾放了一个print语句,以确保它确实到达了那里,并且确实到达了那里,但是出于某种原因,它只是没有像预期的那样调用该方法。如果我自己调用另一个方法并通过参数输入一个字符串,那么它就可以正常工作 代码是:Java 为什么赢了';这种方法不叫工作吗?,java,methods,Java,Methods,我正在创建一个方法来接受用户的输入并验证它,以确保它是正确的。如果它是正确的,它将调用一个方法并将用户的输入输入其中。但由于某些原因,方法调用无法工作。它不会产生任何错误,只是不会产生错误。我在代码的末尾放了一个print语句,以确保它确实到达了那里,并且确实到达了那里,但是出于某种原因,它只是没有像预期的那样调用该方法。如果我自己调用另一个方法并通过参数输入一个字符串,那么它就可以正常工作 代码是: public void getGetScheduledShowByFilmInput()///
public void getGetScheduledShowByFilmInput()////new - omar////
{
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
String filmInput;
filmInput = "";
boolean foundFilm;
foundFilm = false;
System.out.println("Here is a list of films that are currently showing:");
for(Film film : films){
System.out.println(film.getFilmName());
}
System.out.println("");
System.out.println("Please type the film name that you wish to view the corresponding shows for and press enter.");
System.out.println("Type 'exit' and press enter to exit this process.");
while(foundFilm == false){
try{
filmInput = reader.readLine();
}
catch (IOException e){
System.out.println("Error");
}
//If user enters "exit" then return.
if(filmInput.equals("exit")){
return;
}
//Check to see if the film name input by the user corresponds to any film showing.
for(Film film : films){
if(film.getFilmName() == filmInput){
foundFilm = true;
break;
}
}
if(foundFilm = true){
System.out.println("Film found.");
}
else{
System.out.println("The film name you entered has not been recognised. Please try again.");
}
}
//Call the function and input the film name input by the user.
getScheduledShowsByFilm(filmInput); ////This is the code that seems to be the problem.
System.out.println("reached bottom");
}
第二种方法是:
public void getScheduledShowsByFilm(String inputFilmName)
{
ArrayList<Show> scheduledShows;
scheduledShows = new ArrayList<Show>();
for(Film film : films){
if(inputFilmName == film.getFilmName()){
for(Schedule schedule : schedules){
scheduledShows.add(schedule.getShowsOfFilm(film));
if(scheduledShows.get(scheduledShows.size() - 1) == null){
scheduledShows.remove(scheduledShows.size() - 1);
}
}
}
}
for(Show show : scheduledShows){
System.out.println("**********************************");
show.getShowDetails();
System.out.println("**********************************");
}
}
public void getScheduledShowsByFilm(字符串inputFilmName)
{
ArrayList scheduledShows;
scheduledShows=新的ArrayList();
用于(电影:电影){
如果(inputFilmName==film.getFilmName()){
对于(时间表:时间表){
scheduledShows.add(schedule.getShowsOfFilm(film));
if(scheduledShows.get(scheduledShows.size()-1)=null){
scheduledShows.remove(scheduledShows.size()-1);
}
}
}
}
用于(显示:scheduledShows){
System.out.println(“**********************************************”);
show.getShowDetails();
System.out.println(“**********************************************”);
}
}
当我自己调用第二个方法并手动输入参数时,第二个方法非常有效
这可能是我不理解的非常简单的事情!哈哈,谢谢你的帮助:)foundFilm永远不会是假的,因为你总是给它赋值为真:
if(foundFilm = true){
System.out.println("Film found.");
}
尝试将其更改为:
if(foundFilm)
{
System.out.println("Film found.");
}
在
GetScheduledShowByFilmInput()
和getScheduledShowsByFilm(字符串)
中,避免使用相等运算符(=)进行字符串比较。==运算符测试对象相等性,但您希望测试两个字符串是否包含相同的字符序列。因此,请使用等于
:
//Check to see if the film name input by the user corresponds to any film showing.
for(Film film : films){
if(film.getFilmName().equals(filmInput)){
foundFilm = true;
break;
}
}
及
你怎么知道没有人叫它?从我看到的情况来看,您声明并初始化了BufferedReader,但是您没有在中读取任何内容(除非还有更多的代码没有发布到那里。另外,你是否尝试过在函数中打印测试消息以查看是否到达?你能发布
getScheduledShowsByFilm
?没关系@my comment。只是注意到了你的编辑。啊,对了,哈哈,谢谢你:)是的,当然两秒钟:)虽然这是个问题,这根本不能解释他的问题。诚然,他的问题有点模糊,但似乎他是说没有调用getScheduledShowsByFilm
。@Omar:你真的想做:if(foundFilm==true)
带双等号,但正如亨特所示,if(condition==true)
与更简单的if(condition)是一样的
这不是问题所在,代码的这一部分工作正常。我在开始时将其指定为false,并在while循环期间更改。它确实可以工作,我放在代码底部的print语句打印出“到达底部”,意思是它到达了它,代码的这一部分工作正常。感谢您的输入:)@Omar:同样,您的while(foundFilm==false)
传统上会被写成while(!foundFilm)
。while循环只执行一次,这可能不是@OmarQassem想要的。非常感谢!这就解决了一切:)非常感谢。我是这个网站的新手,你能告诉我如何给你+1吗?@OmarQassem既然你已经接受了Hunter的答案,你就不能接受我的答案。(我必须承认,我不确定你是否能改变被接受的答案,因为我自己从来没有问过这样的问题……)。不过,您可以使用答案左侧的箭头向上投票。FAQ中关于如何提问()的部分有其他详细信息。
for(Film film : films){
if(inputFilmName.equals(film.getFilmName())){
for(Schedule schedule : schedules){
scheduledShows.add(schedule.getShowsOfFilm(film));
if(scheduledShows.get(scheduledShows.size() - 1) == null){
scheduledShows.remove(scheduledShows.size() - 1);
}
}
}
}