局部变量可能没有被初始化和一个循环

local Variable may not have been initialized and a loop

我有一个程序最初采用两个数字将值传递给一个方法并返回较大的数字。我明白了,所以我决定扩展到该程序并希望该程序向用户询问 2 个数字。我有 2 个我无法弄清楚的问题。第一个是它说我的变量 i 和 j 没有初始化。二是程序循环了3次。有人可以给我任何帮助吗?我来自 c# 谢谢。

package javabook;
import java.util.Scanner;

public class Chapter5 {

    public static void main(String[] args) 
    {
        int i;
        int j;
        int k = max(num1(i),num2(j));
        //Scanner input = new Scanner(System.in);       
        num1(i);
        num2(j);

        System.out.print("The maximum between " + num1(i) + " and " + num2(j) + " is " +k);
    }

    //Return the max between two numbers
    public static int max(int num1, int num2)
    {
        int result;

        if (num1>num2)
            result = num1;
        else
            result = num2;      
        return result;      
    }//End Max Method


    public static int num1(int i)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the first number: ");
        input.nextInt();
        input.close();
        return i;
    }//End num1 method
    public static int num2(int j)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter the second number: ");
        input.nextInt();
        input.close();
        return j;
    }//end num2 method


}

此处:

    int i;
    int j;   <---creates the var, but doesn't initialize it
    int k = max(num1(i),num2(j));
                             ^---using the var, without having initialized it

即使是简单的

j = 0;

会有帮助。

局部变量与全局声明变量的值不同,因此您必须分配一些值才能使用它们。默认情况下,全局变量 int 的值为 0。

    int i = 0;
    int j = 0;
or 

this.i = 0;
this.j = 0;