我无法让代码工作。目的是为创建的方法反转已输入的整数。在底部是测试方法的测试代码。
import java.util.Scanner;
public class ReverseDigit
{
public void reverseDigits()
{
Scanner input = new Scanner(system.in);
{
System.out.print( "Please enter an integer number (other than -999 unless you wish to exit):");
number = input.nextInt();
while ( number != -999 )
{
System.out.printf( "%d reversed is %d\n", number, reverseDigits( number ) );
System.out.print( "Enter an integer (-1 to exit): " );
number = input.nextInt();
}
}
}
public int reverseDigits ( int number);
{
int reverseNumber = 0;
int placeValue;
while ( number > 0 )
{
placeValue = number % 10;
number = number / 10;
reversedNumbers = reversedNumbers * 10 + placeValue;
}
return reversedNumbers;
}
}
public class ReverseDigitsActive
{
public static void main( String args[] )
{
Reverse application = new Reverse();
application.reverseDigit();
}
}
答案 0 :(得分:2)
您需要将课程更改为此类
import java.util.Scanner;
public class ReverseDigit
{
public void reverseDigits()
{
Scanner input = new Scanner(System.in);
System.out.print( "Please enter an integer number (other than -999 unless you wish to exit):");
int number = input.nextInt();
do
{
System.out.printf( "%d reversed is %d\n",
number, toReverseDigits( number ) );
System.out.print( "Enter an integer (-1 to exit): " );
number = input.nextInt();
}while(number>-1)
}
public int toReverseDigits ( int number)
{
int reverseNumber = 0;
int placeValue;
while ( number > 0 )
{
placeValue = number % 10;
number = number / 10;
reverseNumber = reverseNumber * 10 + placeValue;
}
return reverseNumber;
}
}
public class ReverseDigitsActive
{
public static void main( String args[] )
{
ReverseDigit application = new ReverseDigit();
application.reverseDigits();
}
}
希望这会有所帮助:)
答案 1 :(得分:1)
如果您使用任何IDE来运行此应用程序,则此代码中应该会出现许多错误。
System
(不是“system”)。number
变量而未在第6行中声明它。reversedNumbers
没有任何声明。
Reverse
。
reverseDigit()
方法,该方法也不存在。Aradhna给出的代码供您参考,如果您想在用户输入为-1时退出程序,则代码如下:
System.out.print( "Enter an integer (-1 to exit): " );
number = input.nextInt();
if (number == -1)
break;