...
Code Block |
---|
Scanner scanner = new Scanner(System.in);
do {
Memory memory = new Memory(); // new game instance
while (true) { // repeat as long as the user does not make a mistake
int nextItem = memory.nextItem(); // extend sequence with another value
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); // scroll previous dialog away
System.out.println("Element nr. " + memory.getItemCount() + " er " + nextItem);
Boolean result = null;
do { // get another number from the user
System.out.println("Gjenta element nr. " + (memory.getAcceptedCount() + 1) + " av " + memory.getItemCount());
int nextInt = Integer.valueOf(scanner.nextLine().trim());
result = memory.acceptItem(nextInt); // check item
} while (result == null); // repeat until there is a definite result, either a mistake or a complete and correct sequence
if (result == Boolean.FALSE) { // if the user made a mistake, break out of loop
System.out.println("Feil, den lengste sekvensen du klarte var på " + (memory.getItemCount() - 1) + " elementer.");
break;
}
}
System.out.println("Vil du prøve på nytt (ja/nei)?");
} while (scanner.nextLine().trim().equals("ja")); // repeat as long as the user wants to play another game
scanner.close(); |
...
Code Block |
---|
public class MemoryProgram { private Memory memory; public void init() { memory = new Memory(); // new game instance } privatepublic void run() { ... // insert the code above } // entry point for Java program public static void main(String[] args) { new MemoryProgram().run(); // instantiate program object and call its run() method } } |
...