jeudi 12 décembre 2019

how to save results from game in an "if-else-if"- statement to txt-file? (java)

I'm new to programming and am doing an assignment in my school and came across a minor problem writing a "rock, paper, scissors"-game, where one plays against the computer. I've managed to get the program working fine, and show the results after every round, even managed to save automatically (when the match is over, fist to three) to a .txt file with a specific name (date and time of the game played), but only with a specific line, not all results. The last, and the hard, part is to save the results from every round to this .txt file in the end. And i managed to save every score separately, writing the outputFile.println() after every "if-else-if"-statement, to a .txt file. But i want to save the entire game's result in this .txt file.

So all the results from every round should be displayed in this .txt file after the game i over. How do i do this I'm kindly asking? Do I have to rewrite my "if-else-if"-statement, how in that case, or is there a shortcut?

I will gladly share my code, if necessary, byt here's the statement that writes the filename, and on the third outputFile.println(); i want to write the result statement inside the parenthesis but how do i do this? Sorry if this doesn't make any sense..

import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Random;


public class stenSaxPase {

    public static void main(String[] args) throws IOException  {
        System.out.println("Dags for en omgång 'sten, sax, pase'!");
        firstTry();
        fileName();
        System.out.println("\n");
        System.out.println("Tack for att du spelade!");
    }

    private static void firstTry() throws IOException {
        int computerHand = 0;
        int myHand = 0;
        int counter = 0;

        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();
        System.out.println("Valkommen, vad heter du?");
        String name = scanner.nextLine();


        while (counter == 0) {
            int computerChoice = rand.nextInt(3) + 1;

            System.out.println("Hej " + name + ", Skriv in 1 för sten, 2 för sax eller 3 för pase.");
            int myChoice = scanner.nextInt();

            if (myChoice == computerChoice) {
                System.out.println("ni valde samma, kor igen");
            }
            else if (myChoice == 1) {
                if (computerChoice == 2) {
                    String outcome = "sax - sten";
                    System.out.println("Datorn valde sax ");
                    System.out.println("\n");
                    myHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum1 = outcome + computerHand + myHand;
                }
                else if (computerChoice == 3) {
                    String outcome = "pase - sten ,";
                    System.out.println("Datorn valde pase ");
                    System.out.println("\n");
                    computerHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum2 = outcome + computerHand + myHand;
                }
            }
            else if (myChoice == 2) {
                if (computerChoice == 1) {
                    String outcome = "sten - sax ,";
                    System.out.println("Datorn valde sten ");
                    System.out.println("\n");
                    computerHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum3 = outcome + computerHand + myHand;
                }
                else if (computerChoice == 3) {
                    String outcome = "pase - sax , ";
                    System.out.println("Datorn valde pase ");
                    System.out.println("\n");
                    myHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum4 = outcome + computerHand + myHand;
                }
            }
            else if (myChoice == 3) {
                if (computerChoice == 1) {
                    String outcome = "sten - pase , ";
                    System.out.println("Datorn valde sten ");
                    System.out.println("\n");
                    myHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum5 = outcome + computerHand + myHand;
                }
                else if (computerChoice == 2) {
                    String outcome = "sax - pase , ";
                    System.out.println("Datorn valde sax ");
                    System.out.println("\n");
                    computerHand++;
                    System.out.println("Datorn " + "\t -\t" + name);
                    System.out.println("--------------------");
                    System.out.println(outcome + computerHand + " - " + myHand);
                    String sum6 = outcome + computerHand + myHand;
                }
            }
            if (myHand == 3) {
                System.out.println("Du v a n n !");
                counter++;
            }
            if (computerHand == 3) {
                System.out.println("Du f o r l o r a d e !");
                counter++;
            }
        }
    }

    public static void take() {}

    public static void fileName() throws IOException {
        String fileName = new SimpleDateFormat("yyyyMMdd_HHmmss'.txt'").format(new Date());
        System.out.println("Filnamnet är: " + fileName);

        PrintWriter outputFile = new PrintWriter(fileName);

        outputFile.println("Datorn " + "\t -\t" + "spelare");
        outputFile.println("--------------------");
        outputFile.println();

        outputFile.close();
        System.out.println("Data skrivet till filen");
    }
}

Aucun commentaire:

Enregistrer un commentaire