0

So yesterday I asked a question about some GUI-ing. I completly threw that over, since I found it a little to complicated for me to actually deal with it. Now I am reworking the thing in the console.

And I got myself stuck again. My problem this time: How can I jump back to a point before a if-command was executed? Direct example:

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

public class HBA {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
    System.out.println("Herzlichen Glückwunsch Anna! Und viel Spaß mit deinem Geschenk!") ;
    System.out.println("Neben diesem Programm befindet sich eine Passwort gesicherte Datei, die einen weiteren Teil deines Geschenks enthällt."
                    + "Um an das Passwort zu gelangen wirst du jedoch ein paar ganz besonders schwierige Fragen beantworten müssen!"
                    + "Wenn du bereit für das Quiz bist, gib in die Eingabe: 'ok' ein.");
    String OK, Q1, Q2, Q3, Q4, Q5, Q6, Q7;
    BufferedReader repo = null;

    OK = scan.next();
    if (OK == "ok") {
            System.out.println("Alles gut, fangen wir mit etwas leichtem an!");
            }
    else {
            System.out.println("Wie... bei so einer einfachen Sache versagst du schon? Versuchs nochmal!");
        }

    System.out.println("Frage 1: Wer ist Supergeil? \n A: Erik \n B: Anna \n C: The J \n D: Friedrich Liechtenstein");
    mark(0);
    Q1 = scan.next();
    if (Q1 == "D") {
            System.out.println("Richtig! Der erste Buchstabe lautet: S");
            }
    else {
            System.out.println("Leider falsch. Versuch es nochmal.");
            reset();
        }
    }
}

The scripted works as expected, besides: If you type something wrong in the last part:

    System.out.println("Frage 1: Wer ist Supergeil? \n A: Erik \n B: Anna \n C: The J \n D: Friedrich Liechtenstein");
    mark(0);
    Q1 = scan.next();
    if (Q1 == "D") {
            System.out.println("Richtig! Der erste Buchstabe lautet: S");
            }
    else {
            System.out.println("Leider falsch. Versuch es nochmal.");
            reset();
        }
    }
}

It just ends the script. Instead it should jump back to the beginning of the if-command. Means: The answer to the question in the System.out.printLn (it is a question) is D and you typ A instead, it should reset the whole thing that you can try it again and answer something different. How can I achieve that? I read that BufferedReader have a mark() and reset() function, but I don't know if they work the way I expect them to or how I would have to integrate them.

I also thought about using a while or a do command. But I haven't found a way for that yet.

Can someone pls enlighten me?

Thanks!

Erik Ro
  • 9
  • 1
  • 3
    Try a loop.....while loop. – brso05 Oct 29 '14 at 19:47
  • 3
    You already thought of your answer: use a while loop. Give yourself a little more credit and try to implement it yourself, but first, go take a break for an hour so that you can approach your problem with a fresh perspective. – MarsAtomic Oct 29 '14 at 19:50
  • 1
    A little off topic: variable names should be lowercase, to avoid confusion when others (or future-you) read the code. – Kayz Oct 29 '14 at 19:54
  • 1
    You can't compare strings with `==`. Use `.equals` instead. – Makoto Oct 29 '14 at 20:15
  • Yeah, I just learned that the hard way :D But thanks ;) I have a new weird problem now though Pressed enter too early... ehm... how can I post my code again? Should I just answer myself? – Erik Ro Oct 29 '14 at 20:18
  • Nah, roleback. It more or less fixed it for myself. But thanks for the help everyone! – Erik Ro Oct 29 '14 at 20:29

0 Answers0