When I try to use a while loop it won't follow the conditions I set in the while. Like if I put while s1 != "n")
it will continue even if s1 = n. It does the opposite when I try to use while s1 == "y")
and it very irritating. I'm trying to make a factorial calculator that prompts the user if they would like to keep going or stop.
Here's the full code:
package factorial;
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
int factorial = 1;
int number = 6;
int i = 1;
String s1 = "y";
Scanner keyboard = new Scanner(System.in);
while(s1 != "n") {
System.out.println("Enter an N:");
number = keyboard.nextInt();
while(i <= number) {
factorial *= i;
i++;
}
System.out.println("Factorial = "+factorial);
System.out.println("Would you like to continue? (y/n)");
s1 = keyboard.next();
}
keyboard.close();
System.out.println("Have a nice day!");
}
}