The Artima Developer Community
Sponsored Link

Java Answers Forum
loops?

1 reply on 1 page. Most recent reply: Oct 25, 2004 2:00 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
james swanson

Posts: 1
Nickname: jimi642
Registered: Oct, 2004

loops? Posted: Oct 23, 2004 2:39 PM
Reply to this message Reply
Advertisement
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;


rand1 = numberGenerator.nextInt(9);
rand2 = numberGenerator.nextInt(9);
rand3 = numberGenerator.nextInt(9);
rand4 = numberGenerator.nextInt(9);


yours1 = numberGenerator.nextInt(9);
yours2 = numberGenerator.nextInt(9);
yours3 = numberGenerator.nextInt(9);
yours4 = numberGenerator.nextInt(9);




System.out.println ("The winning Lotto numbers are: " + rand1 + rand2 + rand3 + rand4);
System.out.println ("Your numbers cam up: " + yours1 + yours2 + yours3 + yours4);



if (rand1 != yours1)
done = false;
else if (rand2 != yours2)
done = false;
else if (rand3 != yours3)
done = false;
else if (rand4 != yours4)
done = false;
else
win = true;
}
}

can anyone tell me what I am doing wrong it will start and compile but i can't get it to loop
thanks to anyone who reply


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: loops? Posted: Oct 25, 2004 2:00 AM
Reply to this message Reply
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);

Flat View: This topic has 1 reply on 1 page
Topic: Singleton classes and Threadsafe Previous Topic   Next Topic Topic: looking for project idea plesae

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use