Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Java代码是关于factorial的for循环,但它不能正常工作_Java_Loops_For Loop_Factorial - Fatal编程技术网

我的Java代码是关于factorial的for循环,但它不能正常工作

我的Java代码是关于factorial的for循环,但它不能正常工作,java,loops,for-loop,factorial,Java,Loops,For Loop,Factorial,我的Java代码应该允许用户输入一个数字,然后计算该数字的阶乘,当我输入数字5时,我需要使用“for循环”,当它应该是120时,它告诉我阶乘是6。我曾尝试观看过带分解循环的教程,但它们不起作用,我认为这是因为我有“do”命令,可以从调用中获取值 代码如下: static Scanner kboard = new Scanner(System.in); //variable to read in values public static void main(String[] args) {

我的Java代码应该允许用户输入一个数字,然后计算该数字的阶乘,当我输入数字5时,我需要使用“for循环”,当它应该是120时,它告诉我阶乘是6。我曾尝试观看过带分解循环的教程,但它们不起作用,我认为这是因为我有“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");

  }

}
static Scanner kboard=新的扫描仪(System.in)//要读入值的变量
公共静态void main(字符串[]args){
int-choice=0;
字符串dummy=“”;
字符串名=”;
字符串姓氏=”;
整数=0;
做{
System.out.println(“1.显示用户名,2.计算阶乘,3.退出”);
choice=kboard.nextInt();
dummy=kboard.nextLine();//去掉返回值
如果(选项==1){
forename=getforename();
姓氏=GetLastname();
显示详细信息(姓名、姓氏);
}
如果(选项==2){
number=getnumber();
计算工厂(编号);
}
如果(选项==3){
System.out.println(“再见”);
}
}while(选项!=3);
}
公共静态字符串getforename(){
字符串newforename=“”;
System.out.println(“请输入您的名字?”);
newforename=kboard.next();
返回(newforename);
}//结束getforename
公共静态字符串getNames(){
/*
提示用户输入姓氏的代码
*/
字符串newsurname=“”;
System.out.println(“请输入您的姓氏?”);
newsurname=kboard.next();
返回(newsurname);
}//结束GetLastname
公共静态void displaydetails(字符串displayforename、字符串displayName){
/*
代码将执行规定的更改并显示结果
*/
字符显示首字母;
字符串displayusername=“”;
displaylusname=displaylusname.toUpperCase();
displayinitial=DisplayForName.charAt(0);
displayusername=displayinitial+DisplayName;
System.out.println(“您的用户名是”+displayusername);
}
公共静态int getnumber(){
System.out.println(“您想知道什么样的阶乘数?”);
int newnumber=kboard.nextInt();
返回newnumber;
}
公共静态无效CalcFactory(int newnumber){
整数计数=0;
整数阶乘=1;
如果(新编号>0){

对于(count=1;count如果您使用了调试器,那么您可以看出它只在
calcfactorial
方法中执行一次乘法,此时
count
已经是
6
。原因:

首先,删除
for
条件循环末尾的分号。它充当
for
循环的主体。这使
计数等于
新编号+1
,或
6


其次,将打印语句移动到
for
循环结束后,但仍在
if
块内。否则,您将获得
新的打印输出行。

我不会完全给出答案……用户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");

    }

}
publicstaticvoidcalcfactorial(int newnumber)
{
整数计数=0;
整数阶乘=1;
如果(新编号>0)
{

对于(count=1;count)这将是有用的:另一个技巧:使用编辑器,它可以为您缩进代码。这样您可以注意到,在不需要它们的地方,缺少
{
和额外的
(如在
if(条件);
或lops
while(conidtion);{…}
for(…);{…
).Related:(也适用于循环)。你的for循环代码中有一个错误。一个像样的IDE会向你展示这一点。哈-我从原始IDE复制而来,从未将其插入IDE。猜测OP遗漏了一个大括号。不过感谢snide的评论。非常感谢!我还是java和eclipse新手,所以如果我做了一些愚蠢的事情,我很抱歉……我不知道为什么我的问题被否决了:(但是非常感谢你,伙计!