java - 声明方法和返回值

标签 java

我有一个完整的程序,但不确定如何调用主方法?我的返回值需要是:

(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;
}
}

最佳答案

实际上并没有将代码放入方法中的模板...您只需让该方法返回您想要的内容,并尝试尽可能高效地做到这一点。

返回多个值的一种方法是返回一个数组(或者您可以使用ArrayList,但在这种情况下这有点过分了。)您可以在方法内进行所有计算,当一切都说完并完成后,返回一个包含您想要的内容的 String 数组。例如:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class methodOne {
    public static void main(String[] args) {
        Scanner keyboardInput = new Scanner(System.in);
        System.out.println("Enter a word: ");
        String word = keyboardInput.next();

        File inputFile = new File("movieReviews.txt");

        String[] data = wordCount(word, inputFile);

        for(int i =0; i < data.length; i++) {
            System.out.println(data[i]);
        }
    }

    public static String[] wordCount(String word, File inputFile) {
        String[] results = new String[2];
        Scanner movieReview = null;
        try {
            movieReview = new Scanner(inputFile);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

        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;
            }
        }

        double average = total/count;
        results[0] = word + " appears " + count + " times";
        results[1] = "The average score for reviews containing the word" + word + " is " + average;
        return results;
    }
}

如果您有任何疑问,请随时告诉我!

关于java - 声明方法和返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40198264/

相关文章:

java - 高质量语言;整数列表作为输入查询

java - Jaxb2 : create separate directorie per namespace

java - 为什么执行这段代码时蛇没有出现?

java - Android:如何防止图像在 ImageView 或 ImageButton 中缩放?

java - 如何在不丢失或更改任何数据的情况下安全地将 RSA 加密和解密的字节转换为字符串?

Java 多播接收器不工作

java - 将 JAR 设置为跳过以加速 Tomcat 7 maven 插件启动

java - Location.getLatitude 方法返回非精度 double ,这对 GPS 坐标不利

java - 在java中获取java.lang.ClassNotFoundException : com. mysql.jdbc.Driver

java - PriorityQueue 在删除元素时更改顺序