• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can't Break Out of a Loop

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Allows for input from user.
import java.util.Scanner;
// Allows for use of the printf method.
import java.io.PrintStream;

/*
* Class that uses several different types of loops to find the factorial
* of a given number and print a table of dimensions for several rectangles.
*/
public class LoopExamples {

public boolean replay;

// Uses a while loop to find the factorial of a given integer.
public int factorial(int i){

int factorial = 1;
do{

while(i > 0){

factorial = factorial * i;
i--;

}
return factorial;

}while(replay = true);

}

/*
* Asks the user if he or she would like to calculate another factorial.
* The factorial method will either quit or re-run depending on the answer.
*/
public void again(boolean replay){

System.out.println("Would you like to calculate another factorial?");
System.out.println("Please enter Y or N:");
Scanner s = new Scanner(System.in);
String answer = s.nextLine();
if(answer.equals("Y") || answer.equals("y"))
replay = true;
else if(answer.equals("N") || answer.equals("n"))
replay = false;
else
System.out.println("Please enter Y or N:");

LoopExamples a = new LoopExamples();
a.ifTrue(replay);
}

/* If the user wants to calculate another factorial, the user is asked for an integer,
* then the factorial method is called and the factorial given. If the user does not want
* to proceed further, then the program quits and moves on.
*/
public void ifTrue(boolean replay){

while (replay = false) {
System.out.println("Thanks for using the Factorial Calculator!");
break;}

while(replay = true){

Scanner s = new Scanner(System.in);
System.out.println("Please enter the integer to find the factorial of:");
int i = s.nextInt();

LoopExamples l = new LoopExamples();
int factorial = l.factorial(i);

System.out.println("The factorial of " + i + " is " + factorial + ".");

LoopExamples a = new LoopExamples();
a.again(replay);

}
}

// Method main that calls the other methods.
public static void main(String[] args) {

System.out.println("Welcome to the Factorial Calculator!");

LoopExamples a = new LoopExamples();
boolean replay = true;
a.ifTrue(replay);

}
}




This is my code.
Even if I enter "n" or "N" when asked if I want to calculate another factorial, it doesn't break. Instead it goes back and asks me to enter a new integer. Help?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
while (replay = false) {

never do the above, and your non-working code is why. First, you probably meant to use '=='. but a better way would simply be:

while (!replay ) {
 
reply
    Bookmark Topic Watch Topic
  • New Topic