简体   繁体   中英

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. 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. 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().
  • Note 2: and as per Dukeling -- you need to give all of your methods a similar fix since they all have the same problem.

Your Input() function calls itself at the end:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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