声明方法和返回值

时间:2016-10-23 00:06:33

标签: java

我有一个完整的程序但不确定如何在主程序中调用该方法?我的返回值必须是:

(word + " appears " + count + " times " );

("The average score for reviews containing the word horrible is " + average);

所有代码都需要保留在methodOne中。我只需要知道,如何调用方法并返回正确的值?

注意:我非常是Java的新手,所以请不要使用复杂的想法

现行守则:     import java.util.Scanner;     import java.io.File;     import 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;
}
}

3 个答案:

答案 0 :(得分:1)

实际上并不是将代码放入方法的模板......你只需让方法返回你想要的东西,并尝试尽可能高效地完成。

您可以返回多个值的一种方法是返回一个数组(或者您可以使用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;
    }
}

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

答案 1 :(得分:0)

将值传递给方法非常简单。例如:

    public class NewClass {
    public static void main(String[] args) {
        // passing two literal strings into the method
        // and then invoking it
        System.out.println(strCombo("Hello, ", "world!"));             
    }

    // if you are going to call a method in main, it must be static
    public static String strCombo(String str, String str2) {        
        return str + str2;
    }
} 

这适用于任何数据类型,您只需要将其设置为正确的数据类型,如果它没有返回任何内容,则需要VOID。

希望这有帮助!

答案 2 :(得分:0)

有很多方法可以改进这些代码,但为了保持简单而有用,我建议你:

  • 创建一个封装整个逻辑的方法:readReviews();
  • 让此方法返回一个包含2条预期行的数组;
  • 从主要方法调用方法:String[] result = program.readReviews();(我建议使用Result个对象,但不适合OP);
  • 遍历结果以打印每一行。

完整代码:

public class methodOne {
    public static void main(String[] args) throws FileNotFoundException {
        methodOne methodOne = new methodOne();
        String[] result = methodOne.readReviews();

        for(String res: result){
            System.out.println(res);
        }

    }

    public String[] readReviews() 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;
            }
        }

        double average = (total / count);
        return new String[]{ 
                word + " appears " + count + " times ", 
                "The average score for reviews containing the word horrible is " + average};
    }
}