简体   繁体   English

程序不会继续使用下一个方法

[英]Program won't move on to next methods

I am trying to write a program that will have a user input a number and then output the first digit, the last digit, and the number of digits in that number. 我正在尝试编写一个程序,让用户输入一个数字,然后输出第一个数字,最后一个数字以及该数字中的数字。 My problem is that the program will ask for the input in the Input method, but it just keeps asking for an input and never moves on to the next methods. 我的问题是程序将在Input方法中要求输入,但它一直在要求输入,而从未继续进行下一个方法。 Any help would be much appreciated. 任何帮助将非常感激。

private static String number;
private static String x;
private static int argument;
public static int Input()//Takes the user's input
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return Input();
}
public static int firstDigit()//Returns the first digit
{
    System.out.println(number.substring(0,1));
    return firstDigit();
}
public static int lastDigit ()//Returns the last digit
{
    int a=number.length();
    System.out.println(number.substring(a-1,a));
    returnn lastDigit();
}
public static  int Digits ()//Returns the number of digits
{
    int a=number.length();
    System.out.println(a);
    return Digits();
}
public static void main(String[]args)
{       
   Input();
   firstDigit();
   lastDigit();
   Digits();
}

} }

Your Input() method keeps calling itself recursively at the end. 您的Input()方法始终在末尾递归调用自身。 Don't do that, don't recall the method inside of itself. 不要那样做,不要在内部调用方法。

public static int input()//Takes the user's input
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return Input();  // ***** recursion here *****
}

Instead return the number, argument. 而是返回数字,参数。

public static int input()//Takes the user's input
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return argument;
}

or better yet, make it void and have it return nothing , since you don't appear to be doing anything with the return values of any method. 或更妙的是,使其无效并使其不返回任何内容 ,因为您似乎对任何方法的返回值都没有做任何事情。

public static void input()//Takes the user's input
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
}
  • Note: all method names should begin with a lower-case letter, and so input()... not Input(). 注意:所有方法名称都应以小写字母开头,因此input()...而不是Input()。
  • Note 2: and as per Dukeling -- you need to give all of your methods a similar fix since they all have the same problem. 注意2:按照Dukeling的方法,由于所有方法都存在相同的问题,因此需要对所有方法进行类似的修复。

Your Input() function calls itself at the end: 您的Input()函数在最后调用自己:

return Input();

that is recursive, and since it has no other "return" but itself, it will keep calling itself. 那是递归的,并且由于它本身没有其他“返回”,它将继续调用自身。

public static int Input()//Takes the user's input  {    <----\
   Scanner in=new Scanner(System.in);                         |
   System.out.println("Please input a number");               |
                                                              |
   ...                                                        |
                                                              |
   return Input();                                 ----------/
}

Forever and ever. 永永远远。 Something has to happen to change that. 必须发生某些改变。 Consider something like 考虑类似

public static int iNumbersEntered = 0;

public static int Input()//Takes the user's input  {    
   Scanner in=new Scanner(System.in);                        
   System.out.println("Please input a number");
   iNumbersEntered++;

   ...                                                        

   if(iNumbersEntered < 3)  {
      return Input();
   }  else  {
      getTheInformation...();
   }
}

在Input()方法的最后,执行“ return Input();”,这将导致无限循环。

look at your code 看你的代码

public static int Input()//<=== you are entering the method here
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return Input(); <=== you are entering the same method again!
}

what you are actually doing is called recursion, and you are calling the exact same method. 您实际上在做的事情称为递归,并且您在调用完全相同的方法。

this is the correct code: 这是正确的代码:

public static int Input()
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return number; 
}

As other posters have suggested, you return by calling the method again, which causes an infinite loop. 正如其他张贴者所建议的那样,您可以通过再次调用该方法来返回,这将导致无限循环。

This is the correct method: 这是正确的方法:

public static int input()
{
   Scanner in=new Scanner(System.in);
   System.out.println("Please input a number");
   argument=in.nextInt();
   number=Integer.toString(argument);
   x=number.substring(0);
   return x; 
}

You should then use the return value as parameters in the other methods: 然后,您应该在其他方法中将返回值用作参数:

public static void main(String[]args)
{       
   int input = input();
   firstDigit(input);
   lastDigit(input);
   digits(input);
}

This of course implies that you also change the other methods to accept an int as a parameter. 当然,这意味着您还可以更改其他方法以接受int作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM