我一直收到一个错误,即变量temp
可能尚未初始化。我没有得到它 - 我初始化了,我做错了什么?
import java.util.Scanner;
public class ReverseDigit
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num; //variable to hold the current number
int temp; //variable to hold the temporary number
System.out.print("Enter a positive interger: ");
do
{
num = console.nextInt();
temp = reverseDigit(temp, num);
}
while (num > 0);
System.out.print(" " + temp + " ");
}
public static int reverseDigit(int temp, int num)
{
temp = num % 10;
num = num / 10;
return num;
}
}
答案 0 :(得分:1)
只需在方法开头初始化temp:
int temp = 0;
这样您就无法在初始化之前访问它。
答案 1 :(得分:0)
试试这个:
int temp = 0;
do
{
num = console.nextInt();
temp = reverseDigit(temp, num);
}
while (num > 0);
System.out.print(" " + temp + " ");