在 java 中声明和初始化变量
declare and initialize variables in java
此代码用于掷骰子游戏(详细说明 java 如何编程)
为什么myPoint变量需要初始化,否则会出现编译错误???
我的意思是,如果第一卷有赢或输,则无需初始化 mypoint(因为它不再被使用)
如果第一次掷骰没有赢或输,myPoint 将在开关默认标签中获取值
import java.util.Random;
public class Craps
{
// create random number generator for use in method rollDice
private static final Random randomNumbers = new Random();
// enumeration with constants that represent the game status
private enum Status { CONTINUE, WON, LOST };
// constants that represent common rolls of the dice
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_LEVEN = 11;
private static final int BOX_CARS = 12;
// plays one game of craps
public static void main( String[] args )
{
int myPoint = 0; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
int sumOfDice = rollDice(); // first roll of the dice
// determine game status and point based on first roll
switch ( sumOfDice )
{
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
break;
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus = Status.LOST;
break;
default: // did not win or lose, so remember point
gameStatus = Status.CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
System.out.printf( "Point is %d\n", myPoint );
break; // optional at end of switch
} // end switch
// while game is not complete
while ( gameStatus == Status.CONTINUE ) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if ( sumOfDice == myPoint ) // win by making point
gameStatus = Status.WON;
else
if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
gameStatus = Status.LOST;
} // end while
// display won or lost message
if ( gameStatus == Status.WON )
System.out.println( "Player wins" );
else
System.out.println( "Player loses" );
} // end main
// roll dice, calculate sum and display results
public static int rollDice()
{
// pick random die values
int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf( "Player rolled %d + %d = %d\n",
die1, die2, sum );
return sum; // return sum of dice
} // end method rollDice
} // end class Craps
您需要初始化 myPoint
,因为并非通过代码的所有路径都会导致变量的首次使用(即 if
中的 sumOfDice == myPoint
)赋值给myPoint
.
具体来说,如果 sumOfDice
等于 超出 switch
语句的 default
情况的任何值,就会发生这种情况。
与默认初始化的成员变量不同,局部变量需要显式初始化。如果编译器发现使用未分配变量的路径,则会产生错误。
Java 中的局部变量是在堆栈上创建的。在运行时,它不知道那个内存位置会是什么,所以这被认为是 "garbage"。编译器/运行时 不初始化 局部变量,只有实例(和静态)变量。
这就是为什么您总是需要在Java中初始化您的局部变量。如果不是,编译器会报错
此代码用于掷骰子游戏(详细说明 java 如何编程)
为什么myPoint变量需要初始化,否则会出现编译错误???
我的意思是,如果第一卷有赢或输,则无需初始化 mypoint(因为它不再被使用)
如果第一次掷骰没有赢或输,myPoint 将在开关默认标签中获取值
import java.util.Random;
public class Craps
{
// create random number generator for use in method rollDice
private static final Random randomNumbers = new Random();
// enumeration with constants that represent the game status
private enum Status { CONTINUE, WON, LOST };
// constants that represent common rolls of the dice
private static final int SNAKE_EYES = 2;
private static final int TREY = 3;
private static final int SEVEN = 7;
private static final int YO_LEVEN = 11;
private static final int BOX_CARS = 12;
// plays one game of craps
public static void main( String[] args )
{
int myPoint = 0; // point if no win or loss on first roll
Status gameStatus; // can contain CONTINUE, WON or LOST
int sumOfDice = rollDice(); // first roll of the dice
// determine game status and point based on first roll
switch ( sumOfDice )
{
case SEVEN: // win with 7 on first roll
case YO_LEVEN: // win with 11 on first roll
gameStatus = Status.WON;
break;
case SNAKE_EYES: // lose with 2 on first roll
case TREY: // lose with 3 on first roll
case BOX_CARS: // lose with 12 on first roll
gameStatus = Status.LOST;
break;
default: // did not win or lose, so remember point
gameStatus = Status.CONTINUE; // game is not over
myPoint = sumOfDice; // remember the point
System.out.printf( "Point is %d\n", myPoint );
break; // optional at end of switch
} // end switch
// while game is not complete
while ( gameStatus == Status.CONTINUE ) // not WON or LOST
{
sumOfDice = rollDice(); // roll dice again
// determine game status
if ( sumOfDice == myPoint ) // win by making point
gameStatus = Status.WON;
else
if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
gameStatus = Status.LOST;
} // end while
// display won or lost message
if ( gameStatus == Status.WON )
System.out.println( "Player wins" );
else
System.out.println( "Player loses" );
} // end main
// roll dice, calculate sum and display results
public static int rollDice()
{
// pick random die values
int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
int sum = die1 + die2; // sum of die values
// display results of this roll
System.out.printf( "Player rolled %d + %d = %d\n",
die1, die2, sum );
return sum; // return sum of dice
} // end method rollDice
} // end class Craps
您需要初始化 myPoint
,因为并非通过代码的所有路径都会导致变量的首次使用(即 if
中的 sumOfDice == myPoint
)赋值给myPoint
.
具体来说,如果 sumOfDice
等于 超出 switch
语句的 default
情况的任何值,就会发生这种情况。
与默认初始化的成员变量不同,局部变量需要显式初始化。如果编译器发现使用未分配变量的路径,则会产生错误。
Java 中的局部变量是在堆栈上创建的。在运行时,它不知道那个内存位置会是什么,所以这被认为是 "garbage"。编译器/运行时 不初始化 局部变量,只有实例(和静态)变量。
这就是为什么您总是需要在Java中初始化您的局部变量。如果不是,编译器会报错