局部变量不会初始化(Java)

use*_*098 2 java local-variables do-while

我正在尝试编写一个简单的骰子滚动游戏,如果骰子匹配,那就是胜利,如果它们是一个数字,它是一个平局(junker),如果它们既不满足这些条件,那也是一种损失.

我正在使用do while循环,并且似乎无法获取局部变量来初始化:

import java.util.Scanner;

public class Program06 
{
public static void main(String[] args)
{
    Scanner stdIn = new Scanner(System.in);
    String response = "k";

    int d1 = 1;
    int d2;
    int win;
    int lose;
    int junker;

    System.out.println("Welcome to Computer Dice");
    System.out.println("---------------------------");
    System.out.println("\nYou will first roll your dice");

    System.out.println("Next the outcome of your roll will be determined:");

    System.out.println("Any pair and you Win");
    System.out.println("Anything else and you Lose");
    System.out.println("\n----------------------------");

    System.out.println();

    do
    {

    System.out.println("Player");
    System.out.println("----------");

    d1 = (int)(Math.random() * 6) + 1;
    d2 = (int)(Math.random() * 6) + 1;

    if (d1 == d2)
        ++win;
    else if
        (d1 == d2 +1 || d1 == d2 -1)
        ++junker;
    else
        ++lose;

    System.out.print("Do you wish to play again? [y, n]: ");
    response = stdIn.next();

    } while (d1 == -1);
    stdIn.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用if else语句插入括号,但这没有帮助.

Mak*_*oto 6

您没有为win,, lose或者设置初始值junker,但是您尝试递增它们.

您必须将它们设置0为开头.