修改Java程序修正数学输出。需要帮助。

时间:2015-03-23 03:18:56

标签: java methods factorial

您好我已经构建了此代码,但是当您运行选项2时,我需要它输出答案,以便以此形式打印阶乘 - 阶乘5为5 * 4 * 3 * 2 * 1 = 120(例如)。如果有人可以修改我的代码,我会非常感激。

import java.io.*;
import java.util.Scanner;
public class LO1v5 {



    public static void main(String[] args) {

        System.out.print("Enter your full name: ");

        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String me = null;




        try {
            me = br.readLine();
        } 

        catch (final IOException ioe) {
            System.out.println("Error trying to read your name");
            System.exit(1);

        }

        menu(me);

    }


    public static void menu(String me) {

        System.out.println();
        System.out.println("Thank you, " + me +".");
        System.out.println();
        System.out.println ("Please choose from one of the following options:");
        System.out.println();
        System.out.println("1 = create new user");
        System.out.println("2 = do maths");
        System.out.println("3 = exit");

        int choice = 0;

        final Scanner scanchoice = new Scanner(System.in);

        while (choice != 3) {

            System.out.println();
            System.out.println("Enter \"1\", \"2\" or \"3\":");

            choice = scanchoice.nextInt();

            switch (choice) {
            case 1:  string_function(me);
            break;
            case 2:  number_function();
            break;
            case 3:  System.out.println("Thanks for using this program");
            System.exit(0);

            break;

            default: System.out.println("I am sorry, please enter 1, 2 or 3");

            break;

            }

        }

    }

    public static void string_function(String me) {

        System.out.println("Thank you " + me + ". Enter name to recieve new username: ");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String user = null; 

        try {  
            user = br.readLine();
        } 
        catch (IOException ioe) {  
            System.out.println("Error trying to read your name");
            System.exit(1);
        }

        create_username(user);  

    }



    private static void number_function() {


        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number whose factorial is to be found: ");
        int n = scanner.nextInt();
        int result = 1;
        for (int i = 1; i <= n; i++) {
            result = result * i;
        }


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


    }



    private static void create_username(String user) {

        String fullName = null;

        int spacePos = user.indexOf(" ");
        int length = user.length();

        String firstName = user.substring(0, 1);
        String secondInitial = user.substring(spacePos+1, length);
        fullName = firstName.concat(secondInitial);

        System.out.println("Hello.  Your username is " + fullName);





    }
}

3 个答案:

答案 0 :(得分:0)

private static void number_function() {
       ...       
       String label = "";
       String prefix = "";
       for (int i = 1; i <= n; i++) {
           result = result * i;
           label += prefix + (n - i + 1);
           prefix = " * ";
       }

       System.out.println("The factorial of " + n + " is " + label + " = " + result);
}

答案 1 :(得分:0)

 private static void number_function() {


    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the number whose factorial is to be found: ");
    int n = scanner.nextInt();
    int result = 1;
    String s ="";
    for (int i = 1; i <= n; i++) {
        result = result * i;
        if (i ==1)
            s += n + "*";
        else if(i < n)
            s += n-(i-1) + "*";
        else if(i == n)
            s += n-(i-1);
    }   

     // changed " is " to    " = "
    System.out.println("The factorial of " + s + " = " + result);
    // Or if you dont want the words "The factorial of " then use line below
    System.out.println(s + " = " + result);


}

答案 2 :(得分:-1)

 int n = 5; 
 int result = 1;
 for ( int i = n ; i >= 1 ; i--){
   result = result * i;
   if ( i == 1) {
     cout << i << " = " ;
     break;
   }else{
      cout << i << " * " ;
   }
 } 
 cout << result << endl;