patternjavaModerate
Monty Hall Paradox in Java
Viewed 0 times
paradoxhalljavamonty
Problem
I was wondering if this code looks neat, and if I should change anything. I was planning to implement Frames (or
```
import java.lang.Math.*;
import java.util.Random;
import java.util.Scanner;
public class Paradox {
public static void main(String[] args){
System.out.println("\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.");
Door A = new Door();
Door B = new Door();
Door C = new Door();
A.open=0;
B.open=0;
C.open=0;
Random rand = new Random();
int i = rand.nextInt(3);
/System.out.println(i);/
if(i==0) {
A.goatorcar="Car";
B.goatorcar="Goat";
C.goatorcar="Goat";
}
else if (i==1){
A.goatorcar="Goat";
B.goatorcar="Car";
C.goatorcar="Goat";
}
else if (i==2) {
A.goatorcar="Goat";
B.goatorcar="Goat";
C.goatorcar="Car";
} else { System.out.println("Unexpected error"); }
int pickedA=0;
int pickedB=0;
int pickedC=0;
Scanner sc = new Scanner(System.in);
String pickedDoor = sc.nextLine();
System.out.println("\n");
if (pickedDoor.equals("A") || pickedDoor.equals("1") || pickedDoor.equals("a")){
pickedA = 1;
}
if (pickedDoor.equals("B") || pickedDoor.equals("2") || pickedDoor.equals("b")){
pickedB = 1;
}
if (pickedDoor.equals("C") || pickedDoor.equals("3") || pickedDoor.equals("c")){
pickedC = 1;
}
if(pickedA==1) {
if (A.goatorcar.equals("Car")){
i = rand.nextInt(2);
if (i==0){ System.out.println("The door B contains a Goat"); B.open=1; }
else if (i==1){ System.out.println("The door C contains a Goat"); C.open=1; }
}
else {
if (B.goatorcar=="Goat") { System.out.println("The door B contains a Goat"); B.open=1; }
else if (C.goatorcar=="Goat") { System.out.println("The door C co
JFrame) tomorrow on it. Would it be a good idea to use functions?```
import java.lang.Math.*;
import java.util.Random;
import java.util.Scanner;
public class Paradox {
public static void main(String[] args){
System.out.println("\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.");
Door A = new Door();
Door B = new Door();
Door C = new Door();
A.open=0;
B.open=0;
C.open=0;
Random rand = new Random();
int i = rand.nextInt(3);
/System.out.println(i);/
if(i==0) {
A.goatorcar="Car";
B.goatorcar="Goat";
C.goatorcar="Goat";
}
else if (i==1){
A.goatorcar="Goat";
B.goatorcar="Car";
C.goatorcar="Goat";
}
else if (i==2) {
A.goatorcar="Goat";
B.goatorcar="Goat";
C.goatorcar="Car";
} else { System.out.println("Unexpected error"); }
int pickedA=0;
int pickedB=0;
int pickedC=0;
Scanner sc = new Scanner(System.in);
String pickedDoor = sc.nextLine();
System.out.println("\n");
if (pickedDoor.equals("A") || pickedDoor.equals("1") || pickedDoor.equals("a")){
pickedA = 1;
}
if (pickedDoor.equals("B") || pickedDoor.equals("2") || pickedDoor.equals("b")){
pickedB = 1;
}
if (pickedDoor.equals("C") || pickedDoor.equals("3") || pickedDoor.equals("c")){
pickedC = 1;
}
if(pickedA==1) {
if (A.goatorcar.equals("Car")){
i = rand.nextInt(2);
if (i==0){ System.out.println("The door B contains a Goat"); B.open=1; }
else if (i==1){ System.out.println("The door C contains a Goat"); C.open=1; }
}
else {
if (B.goatorcar=="Goat") { System.out.println("The door B contains a Goat"); B.open=1; }
else if (C.goatorcar=="Goat") { System.out.println("The door C co
Solution
A few thoughts:
-
Some of your lines are very long. While it may seem convenient that they fit on your screen, code gets more difficult to understand as lines become longer. Many people have good reasons to desire an eighty char line limit. This is my personal preference; and many people will disagree; but i find
more amiable than
... mainly because then I don't need to devote much of my screen real estate to a single file. If I am working on multiple files in multiple windows at once, or if I am using a Linux TTY, long lines become a pain if I need to scroll in four directions instead of two. As pointed out by astute readers, this practice is moreover archaic. These days, you can easily get away with lines longer than 80 characters.
-
Writing modular code is an essential step towards ensuring your code isn't a hack. A hack is basically code that doesn't give freely to modification. When code is split up into methods, it becomes easier to understand, modify, and debug. For example, if you have instead a
in one place. You can also probably get away with enumerating the result of that method; that way you can refer to the results by name:
Modular code is code that is divided into modules; each module does a specific task. According to Wikipedia,
Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
-
You have a lot of hard-coded strings. According to Google:
[Hard coding is to] fix (data or parameters) in a program in such a way that they cannot be altered without modifying the program.
You can probably get away with making those into
-
I would look into making your code more maintainable before making a GUI. The process shouldn't be overly involved. I usually try to keep all the GUI methods in a separate class.
-
Some of your lines are very long. While it may seem convenient that they fit on your screen, code gets more difficult to understand as lines become longer. Many people have good reasons to desire an eighty char line limit. This is my personal preference; and many people will disagree; but i find
System.out.println(INTRODUCTION);more amiable than
System.out.println("\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.");... mainly because then I don't need to devote much of my screen real estate to a single file. If I am working on multiple files in multiple windows at once, or if I am using a Linux TTY, long lines become a pain if I need to scroll in four directions instead of two. As pointed out by astute readers, this practice is moreover archaic. These days, you can easily get away with lines longer than 80 characters.
-
Writing modular code is an essential step towards ensuring your code isn't a hack. A hack is basically code that doesn't give freely to modification. When code is split up into methods, it becomes easier to understand, modify, and debug. For example, if you have instead a
doorOption method that determines what door was chosen based on a String, you can simply call that method whenever you want the user to pick a door. The method can do the task ofif (pickedDoor.toLowerCase().equals("a") || pickedDoor.equals("1"))in one place. You can also probably get away with enumerating the result of that method; that way you can refer to the results by name:
DOOR_ONE DOOR_TWO, et cetera. Writing modular code can be a little daunting; but the rewards are quite desirable.Modular code is code that is divided into modules; each module does a specific task. According to Wikipedia,
Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
-
You have a lot of hard-coded strings. According to Google:
[Hard coding is to] fix (data or parameters) in a program in such a way that they cannot be altered without modifying the program.
You can probably get away with making those into
static final fields. Something likestatic final String INTRODUCTION = "\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.";final means the variable can not be modified after assignment.-
I would look into making your code more maintainable before making a GUI. The process shouldn't be overly involved. I usually try to keep all the GUI methods in a separate class.
Code Snippets
System.out.println(INTRODUCTION);System.out.println("\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.");if (pickedDoor.toLowerCase().equals("a") || pickedDoor.equals("1"))static final String INTRODUCTION = "\n\nYou have got three doors, there is a price inside each of them. Pick a door: A, B, or C.";Context
StackExchange Code Review Q#84085, answer score: 10
Revisions (0)
No revisions yet.