mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-24 20:10:15 -08:00
Added java version of mathdice game
This commit is contained in:
53
61 Math Dice/java/MathDice.java
Normal file
53
61 Math Dice/java/MathDice.java
Normal file
@@ -0,0 +1,53 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class MathDice {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner in = new Scanner(System.in);
|
||||
Die dieOne = new Die();
|
||||
Die dieTwo = new Die();
|
||||
int guess = 1;
|
||||
int answer;
|
||||
|
||||
System.out.println("Math Dice");
|
||||
System.out.println("https://github.com/coding-horror/basic-computer-games");
|
||||
System.out.println();
|
||||
System.out.print("This program generates images of two dice.\n"
|
||||
+ "When two dice and an equals sign followed by a question\n"
|
||||
+ "mark have been printed, type your answer, and hit the ENTER\n" + "key.\n"
|
||||
+ "To conclude the program, type 0.\n");
|
||||
|
||||
while (true) {
|
||||
dieOne.printDie();
|
||||
System.out.println(" +");
|
||||
dieTwo.printDie();
|
||||
System.out.println(" =");
|
||||
int tries = 0;
|
||||
answer = dieOne.getFaceValue() + dieTwo.getFaceValue();
|
||||
|
||||
while (guess!=answer && tries < 2) {
|
||||
if(tries == 1)
|
||||
System.out.println("No, count the spots and give another answer.");
|
||||
try{
|
||||
guess = in.nextInt();
|
||||
} catch(Exception e) {
|
||||
System.out.println("Thats not a number!");
|
||||
in.nextLine();
|
||||
}
|
||||
|
||||
if(guess == 0)
|
||||
System.exit(0);
|
||||
|
||||
tries++;
|
||||
}
|
||||
|
||||
if(guess != answer){
|
||||
System.out.println("No, the answer is " + answer + "!");
|
||||
} else {
|
||||
System.out.println("Correct");
|
||||
}
|
||||
System.out.println("The dice roll again....");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user