【发布时间】:2016-10-23 00:06:32
【问题描述】:
我有一个完整的程序,但不确定如何在主程序中调用该方法?我的返回值需要是:
(word + " appears " + count + " times " );
和
("The average score for reviews containing the word horrible is " + average);
所有代码都需要保留在 methodOne 中。我只需要知道,如何调用该方法并返回正确的值?
注意:我非常是 Java 新手,所以请不要使用复杂的想法
当前代码: 导入 java.util.Scanner; 导入java.io.File; 导入 java.io.FileNotFoundException;
public class ReactionAnalysis {
public static void main (String [] args)
{
System.out.println("Call Method One:");
}
public static String methodOne() throws FileNotFoundException
{
Scanner keyboardInput = new Scanner(System.in);
System.out.println("Enter a word: ");
String word = keyboardInput.next();
File inputFile = new File("movieReviews.txt");
Scanner movieReview = new Scanner(inputFile);
String reviewText;
int reviewScore;
int count = 0;
double total = 0;
while (movieReview.hasNext())
{
reviewScore = movieReview.nextInt();
reviewText = movieReview.nextLine();
if (reviewText.contains(word))
{
count++;
total = total + reviewScore;
}
}
String str = (word + " appears " + count + " times " );
double average = (total / count );
String str2 = ("The average score for reviews containing the word horrible is " + average);
return str + str2;
}
}
【问题讨论】:
标签: java