我的与阶乘for循环有关的Java代码无法正常运行

时间:2018-12-03 20:50:07

标签: java loops for-loop factorial

我的Java代码应让用户输入一个数字,然后计算该数字的阶乘,因此我需要使用“ for循环”。当我输入数字5时,它告诉我阶乘为6,应为120。尝试观看带分解循环的教程,但它们无法正常工作,我认为这是因为我有“ do”命令,该命令从调用中获取值

代码如下:

static Scanner kboard = new Scanner(System.in); //variable to read in values

public static void main(String[] args) {
  int choice = 0;
  String dummy = "";
  String forename = "";
  String surname = "";
  int number = 0;



  do {

    System.out.println("1. display the user name, 2. calculate factorial, 3. exit");
    choice = kboard.nextInt();
    dummy = kboard.nextLine(); //strips out the return 

    if (choice == 1) {
      forename = getforename();
      surname = getsurname();
      displaydetails(forename, surname);
    }

    if (choice == 2) {
      number = getnumber();
      calcfactorial(number);
    }

    if (choice == 3) {
      System.out.println("bye");
    }

  } while (choice != 3);
}


public static String getforename() {

  String newforename = "";

  System.out.println("Please enter your forename ?");
  newforename = kboard.next();
  return (newforename);
} // end getforename

public static String getsurname() {
  /*
  Code to prompt the user to enter a surname
  */
  String newsurname = "";

  System.out.println("Please enter your surname ?");
  newsurname = kboard.next();
  return (newsurname);
} // end getsurname


public static void displaydetails(String displayforename, String displaysurname) {
  /*
  Code will carry out prescribed changes and display the result
  */
  char displayinitial;
  String displayusername = "";

  displaysurname = displaysurname.toUpperCase();
  displayinitial = displayforename.charAt(0);
  displayusername = displayinitial + displaysurname;
  System.out.println("Your username is " + displayusername);

}



public static int getnumber() {


  System.out.println("What numbers factorial do you want to know?");
  int newnumber = kboard.nextInt();
  return newnumber;
}

public static void calcfactorial(int newnumber) {
  int count = 0;
  int factorial = 1;

  if (newnumber > 0) {
    for (count = 1; count <= newnumber; count++); {

      factorial = factorial * count;
      System.out.println("Factorial of " + newnumber + " is: " + factorial);

    }

  } else {
    System.out.println("Number must be positive");

  }

}

2 个答案:

答案 0 :(得分:6)

如果您使用过调试器,那么您可以说它只在calcfactorial方法中执行乘法,并且此时count已经是6。原因:

首先,在for条件循环的末尾删除分号。它充当for循环的主体。这使count等于newnumber + 16

第二,将打印语句移动到for循环结束之后,但仍在if块内。否则,您将获得newnumber行打印输出。

答案 1 :(得分:-1)

我不会完全放弃答案...用户azurefrog发布了有关调试代码的有用链接。

但是,您的for循环正在执行您不想要的事情:

public static void  calcfactorial(int newnumber)
{
    int count = 0;
    int factorial = 1;

    if (newnumber > 0)
    {
        for (count=1;count<=newnumber;count++);
        {

            factorial = factorial * count; System.out.println("Factorial of "+newnumber+" is: "+factorial);

        }

    }

    else
    {
        System.out.println("Number must be positive");

    }

}

通读此行,factorial = factorial * count;行将针对输入的任何要进行阶乘计算的内容执行1 * 1、1 * 2、1 * 3、1 * 4、1 * 5等。 。这不是正确的逻辑。

我建议仔细考虑for循环的逻辑。您应该在该循环中的某处使用输入的数字(例如,给出的示例为5)。