Dari Ko

Greenhorn
+ Follow
since Oct 16, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dari Ko

// 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?
12 years ago