i am VERY new to java and I am trying to make a loop in a simple program here is what i've got so far.....
import java.util.*;
public class thelotto{
public static void main (String[] args){
boolean done = false; boolean win = true; boolean playagain = true; int rand1, rand2, rand3, rand4, yours1, yours2, yours3, yours4; Random numberGenerator = new Random(); Scanner scan = new Scanner(System.in);
String playmessage = "Would l you like to play the lotto? (y / n)"; String youlost = "Sorry you did not win, buy another ticket?"; String youwin = "you won the lotto, go quit your job!!!";
System.out.print(playmessage); if (scan.next().equals("n")) playagain = false; else done = false;
It's simple like that: You did not put a loop in this program.
first another hint: Instead of using a lot of variables, use arrays.
public class thelotto {
public void main (String[] args) { final String playmessage = "Would l you like to play the lotto? (y / n)"; final String youlost = "Sorry you did not win, buy another ticket?"; final String youwon = "you won the lotto, go quit your job!!!"; boolean win; boolean playagain; int[] rand = new int[4]; int yours[] = new int[4]; Random numberGenerator = new Random(); Scanner scan = new Scanner(System.in); do { System.out.print(playmessage); if (scan.next().equals("n")) playagain = false; else { for (int i = 0; i < 4; i++) { rand[i] = numberGenerator.nextInt(9); yours[i] = numberGenerator.nextInt(9); } win = true; //changed this for (int i = 0; win && i < 4; i++) win = win && rand[i] != yours[i]; if (win) { System.out.println (youwon); //playagain = false; //if you won, that would make sense } else { System.out.println (youlost); } } } while (playagain); //this is the loop. At this point we'll return to the "do" statement. } }
1. While-Loop: boolean condition = [true/false]; while (condition) { }
2. Do-While-Loop: boolean condition = [true/false]; do { } while (condition);
The difference? Method 1 will not enter the loop, if condition == false. Method 2 will enter the loop, no matter if condition == false or condition == true.
I incluced a For-To-Loop in the upper example. Normally you would write something like :
for (int i = 0; i < 10; i++) {}
But you can do many crazy things with the For-Statement:
The Syntax is for ([do wathever you want]; [condition]; [Block 1: if condition == 2 first execute Block 2, than execute this simple command or method call]) { [Block 2: do other things everytime condition == true] }
So I changed the condition from "i < 4" to "won && i < 4". Why I did that? It stops the execution of the loop when the first non equal number is found.
You have a probability of 1 / 10000 to win. Because you are only using numbers, here's a nice idea: int rand = 0; int yours = 0; for (int i = 0; i < 4; i++) { rand = rand * 10 + numberGenerator.nextInt(9); yours = yours * 10 + numberGenerator.nextInt(9); } win = rand == yours;
even shorter: boolean win = true; for (int i = 0; win && i < 4; i++) win = numberGenerator.nextInt(9) == numberGenerator.nextInt(9);