编写对文件Java的响应

时间:2017-01-18 22:56:00

标签: java do-while

我写了一个神奇的8球计划,有20种不同的可能反应。每次随机选择响应时,我都会尝试将该响应写入文件。但是,在main方法的do-while循环中,行“writeFile(answer);”因为“回答”超出范围而无法正常工作。我无法修复它。

import java.io.*;
import java.util.*;

public class App {

public static void main(String[] args) {

    String terminate = "Q";
    String Question = "";
    System.out.println("I am the all-knowing Magic 8 Ball!");
    String something = "This is an easter egg!";

    do {
        Scanner scnr = new Scanner(System.in);
        System.out.println("");
        System.out.println("Ask your question here or enter 'Q' to quit:");
        Question = scnr.nextLine();
        continueGame(Question);
        writeFile(answer);
    } while (!(terminate.equals(Question)));
   }

public static void continueGame(String Question) {

    char terminate = 'Q';
    char condition = Question.charAt(0);

    if (condition == terminate) 
    {
        System.out.println("");
        System.out.println("Thanks for playing!");
        System.exit(0);
    }
    try 
    {
        Random rand = new Random();
        int choice;
        choice = 1 + rand.nextInt(20);
        responseOptions(choice, Question);
    }
    catch (Exception e)
    {
        System.out.println("Error: Invalid");
    }
}

public static String responseOptions(int choice, String answer)
{
    switch (choice)
    {
        case 1: answer = "Response: It is certain"; break;
        case 2: answer = "Response: It is decidely so"; break;
        case 3: answer = "Response: Without a doubt"; break;
        case 4: answer = "Response: Yes, definitely"; break;
        case 5: answer = "Response: You may rely on it"; break;
        case 6: answer = "Response: As I see it, yes"; break;
        case 7: answer = "Response: Most likely"; break;
        case 8: answer = "Response: Outlook good"; break;
        case 9: answer = "Response: Yes"; break;
        case 10: answer = "Response: Signs point to yes"; break;
        case 11: answer = "Response: Reply hazy, try again"; break;
        case 12: answer = "Response: Ask again later"; break;
        case 13: answer = "Response: Better not tell you now"; break;
        case 14: answer = "Response: Cannot predict now"; break;
        case 15: answer = "Response: Concentrate and ask again"; break;
        case 16: answer = "Response: Don't count on it"; break;
        case 17: answer = "Response: My reply is no"; break;
        case 18: answer = "Response: My sources say no"; break;
        case 19: answer = "Response: Outlook not so good"; break;
        case 20: answer = "Response: Very doubtful"; break;
    }
    System.out.println("");
    System.out.println(answer);

    return answer;
}

public static String writeFile(String something) {

    try {
        FileWriter fw = new FileWriter("filename.txt", true);
        fw.write(something);
        fw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }   
    return something;
}
}

3 个答案:

答案 0 :(得分:1)

您可以将public static void continueGame(String Question)更改为public static String continueGame(String Question)并发出一条返回语句,例如:return responseOptions(choice, Question);

最后在主要内容你可以写:

writeFile( continueGame(Question) );

而不是:

continueGame(Question);
        writeFile(answer);

答案 1 :(得分:1)

首先,我稍微更改responseOptions方法。它只需要选择参数:

public static String responseOptions(int choice) {
    String answer;
    switch (choice) {
      // ...

然后,我们应该稍微更改continueGame

public static String continueGame(String Question) {
    // ...
        return responseOptions(choice);
    }
    catch (Exception e)
    {
        return"Error: Invalid"
    }
}

最后,在主

// ...
String answer = continueGame(Question);
// ...

所以基本上我们将来自respondOptions方法的答案返回给main,以便能够将它传递给print方法。

答案 2 :(得分:0)

请注意responseOptions正在返回一个值,但您忽略它。

int choice;
choice = 1 + rand.nextInt(20);
responseOptions(choice, Question); // Question is not assigned to the return value

总的来说,我认为你应该试试这个。

public static String responseOptions(int choice)
{
    String [] answers = new String[] {
        "It is certain",
        "It is decidely so",
        "Without a doubt"
    };
    return "Response: " + answers[choice - 1];
}

(理想情况下,answers可以在别处定义为类变量。)

并且这样打电话

// within continueGame()
int choice = 1 + rand.nextInt(20);   
String answer = responseOptions(choice); 
writeFile(answer); // Write it here, where it is in scope