Need Help
:confused: :confused: Hello to all:
I need set up the java program for the "while statements" and the "for statements". I know how to do the "do while statements" as follows:(Note this "do while" is just a example of what I need to do with the "while" and "for" statements).
import javax.swing.*; //shows path to newer swing classes
import java.awt.*; //shows path to older AWT classes
public class extends JFrame
{
public () // method name with same name as class.
{
int correctNumber = (int)(Math.random()*100+1);//comp picks number 1-100.
int userAnswer =0; //userAnswer initialized to 0
int tally = 1;// this is for the tally count.
System.out.println("The random number generated was " + correctNumber);
do
{
String guess = JOptionPane.showInputDialog
("Pick a number between 1 and 100 - your guess? ");
userAnswer = Integer.parseInt(guess);
if (userAnswer < 1 || userAnswer > 100)//sets the limit to the numbers between 1 and 100.
}while (userAnswer != correctNumber);
{// the curly brackets are needed because more than one statement for "if" are used.
But I also need to set this same program with the "while" only statements and then I need to set another program for the "for" only statements. In other words what would I change in the "do while" statements so I can use the "while statements" and what would I change so I can use the "for statements" Any help. Thankyou.
[1579 byte] By [
Recency] at [2007-11-11 8:26:10]

# 2 Re: Need Help
while
import javax.swing.*;
import java.awt.*;
public class extends JFrame
{
public ()
{
boolean StopLoop=false;
int correctNumber = (int)(Math.random()*100+1);
int userAnswer =0;
int tally = 1;
System.out.println("The random number generated was " + correctNumber);
while (!StopLoop){
String guess = JOptionPane.showInputDialog
("Pick a number between 1 and 100 - your guess? ");
userAnswer = Integer.parseInt(guess);
if (userAnswer < 1 || userAnswer > 100){
StopLoop=false;
}
else{
StopLoop=true;
}
}
}
}
for loop
import javax.swing.*;
import java.awt.*;
public class extends JFrame
{
public ()
{
int correctNumber = (int)(Math.random()*100+1);
int userAnswer =0;
int tally = 1;
System.out.println("The random number generated was " + correctNumber);
for (int i=0; i <1; i++){
String guess = JOptionPane.showInputDialog
("Pick a number between 1 and 100 - your guess? ");
userAnswer = Integer.parseInt(guess);
if (userAnswer < 1 || userAnswer > 100){
i--;
}
}
}
}
hope it works.
major at 2007-11-11 22:36:27 >

# 4 Re: Need Help
The "for loop" is a little tricky for this. I don't know of a good way to do this, right now. Your present loop is evidence of how challenging it is.[/QUOTE]
Yes it is challenging, but I have completed the doWhile statement without help. I have enclosed the completed copy of the dowhile, so you can get an idea how challenging the dowhile was.
import javax.swing.*; //shows path to newer swing classes
import java.awt.*; //shows path to older AWT classes
public class GuessingGamedw extends JFrame
{
public GuessingGamedw() // method name with same name as class.
{
int correctNumber = (int)(Math.random()*100+1);//comp picks number 1-100.
int userAnswer =0; //userAnswer initialized to 0
int tally = 1;// this is for the tally count.
System.out.println("The random number generated was " + correctNumber);
do
{
String guess = JOptionPane.showInputDialog
("Pick a number between 1 and 100 - your guess? ");
userAnswer = Integer.parseInt(guess);
if (userAnswer < 1 || userAnswer > 100)//sets the limit to the numbers between 1 and 100.
{
JOptionPane.showMessageDialog
(null," You entered an incorrect number. You have taken " + tally
+ " guesses. Please try again.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (userAnswer > correctNumber)
{
JOptionPane.showMessageDialog
(null, "You guessed too high. Pick a number less than " + userAnswer
+ ". You have taken " + tally + " guesses.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (userAnswer < correctNumber)
{
JOptionPane.showMessageDialog
(null, "You guessed too low. Pick a number greater than " + userAnswer
+ ". You have taken " + tally + " guesses.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
}while (userAnswer != correctNumber);
{// the curly brackets are needed because more than one statement for "if" are used.
if (tally <= 5)
{
JOptionPane.showMessageDialog
(null, " WOW!! You guessed the correct number and your " + tally + " guesses was under 6 "
+ " tries, which is well above average. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (tally <= 10)
{
JOptionPane.showMessageDialog
(null, " Good!! You guessed the correct number and your " + tally + " guesses was under 11 "
+ " tries, which is an average score. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (tally >= 10)
{
JOptionPane.showMessageDialog
(null, " You guessed right, but I hope you pick my number sooner. It took you " + tally
+ " tries, which is way to many. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}//ends the last block JP statments.
} // ends the multi-statements for if.
}// ends GuessingGame constructor
public static void main(String args [] )
{
GuessingGamedw application = new GuessingGamedw();
System.exit(0);
}//ends main.
}// ends class (program).
// The above should compile and run.