mercredi 13 février 2019

Getting a Java factorial program with a while loop to keep repeating if the user enters with "y" and has the factorial part working as well

I am new to programming and I am trying to figure out how to get a java program to correctly run a factorial and ask the user if they would like to continue and input another number to use and display the factorial for that one. When the user inputs "y", the program is supposed to then ask for another number. If they select "n", the program should terminate. I've been working on this code for a day and still haven't figured out where I am going wrong in the code to make it solve factorials correctly while looping. Can someone please help me?

        int i = 0;
        int factorial = 1;
        int input;
        char ind = 'y';

             
 while (ind == 'y') {
        System.out.println("Please enter in a number to determine it's factorial: ");
        input = scan.nextInt();
                scan.nextLine();
if  ( i<= input) {
        i=input;
                        i++;
         factorial = factorial * i;
         System.out.println(" the factorial of: " + input + " is " + factorial);
}System.out.println(" Do you want to continue? (y/n) :");
        ind = scan.nextLine().charAt(0);        
}
System.out.println("goodbye.");
        } }

Aucun commentaire:

Enregistrer un commentaire