显示功能计算

时间:2015-01-18 15:44:29

标签: java loops

我正在编译一个正在编译的程序的问题。我必须在函数2上显示实际计算。主程序叫做display11,我在另一个类上调用函数。无法弄清楚什么是错误的,没有显示循环。非常感谢

import java.util.Scanner;
/**
 *
 * @author ec1302696
 */
public class Functions 
{
    private int n;

    public void string_function(String name)
    {

        int optionChosen;
        String fname;
        String sname;
        Scanner keyboard = new Scanner(System.in);
        Scanner sc = new Scanner(System.in);

       Functions fun = new Functions();



         System.out.println("Please enter full name");
         fname = sc.nextLine();   //accept first name entered by user
                            //****enter as a comment for now     fun.first(fname);

        fun.createname(fname);// this create name


        //display this is option 1,administrator name admin
        //ask for full name for generating username
        //call createname passing the username





    }

    public void number_function(String admin)
    {
        {
          int n, c, fact = 1;

      System.out.println("Enter a number to calculate it's factorial");
      Scanner in = new Scanner(System.in);

      n = in.nextInt();

      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;

         System.out.println("Factorial of "+n+" is = "+fact);
        }

       // return fact;
       //this is option 2 ,administrator name admin 
        //read the factorial 
        //calcualte the fatorial
        // print it
    }
    }

    public void createname(String username)
    {
          String fname = fname.substring(0,1);

          System.out.println("The usernameis  " + fname);

    //string calcualte the string function to find the first letter ,and second name.
        //concatenate and print it.

    }           
    }
import java.util.Scanner;

/**
 *
 * @author ec1302696
 */
public class Display11 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        Functions fun = new Functions();// this create teh functions

        int optionChosen;

        String admin;

        Scanner keyboard = new Scanner(System.in);
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter your name");
        admin = keyboard.nextLine();

        {
            System.out.println("Welcome please select option from menu below");
        }
        System.out.println("OPTION 1 – USERNAME");
        System.out.println("OPTION 2 - CALCULATE THE FACTORIAL");
        System.out.println("OPTION 3 - EXIT");
        optionChosen = keyboard.nextInt();


        if (optionChosen == 1) 
        {
            fun.string_function(admin);
        } 

        else if (optionChosen == 2) {

            {
                fun.number_function(admin);
            }

        }

        else if (optionChosen == 3)
        {
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果需要显示阶乘的计算,请将计算循环更改为:

StringBuilder result = new StringBuilder("Factorial of ").append(n).append(" is ");
for ( c = n ; c >0 ; c-- )
{
 result.append(c);
 if(c>1)
 {
  result.append(" * ");
 }
 fact = fact*c;
}

result.append(" = ").append(fact);
System.out.println(result);