2013-04-01 71 views
0

我有这个代码的问题其对一所学校的任务,编译时它说变量hitOrStick可能尚未初始化

hitOrStick might not have been initialized 

我不知道是什么原因造成的,因为我敢肯定它已经初始化,如果你能发现任何其他错误,将不胜感激。

(我知道它不是真正的黑色插孔,它是非常基本的!)再次感谢。

import java.util.Scanner; 
import java.util.Random; 
import java.lang.*; 

class blackjack 

{ 
    public static void main(String[] args) 
    { 

Scanner scan = new Scanner(System.in); 

int total; 
boolean play; 
int computerTotal = 0; 
String hitOrStick; 

System.out.println("You are playing BLACK JACK."); 

    int card1 = (int) Math.ceil(Math.random() * 10); 
    int card2 = (int) Math.ceil(Math.random() * 10); 
    total = card1+card2; 

    System.out.println("Card 1 value: "+ card1); 
    System.out.println(" "); 
    System.out.println("Card 2 value: "+ card2); 
    System.out.println(" "); 
    System.out.println("You have: " + total); 
    System.out.println(" "); 

    if(total == 21) 
     { 
     System.out.println("Blackjack! Congratulations you have won!"); 
     } 
    while(total < 21) 
     { 
     System.out.println("Would you like to hit or stick? please type 'H' or 'S'"); 
     System.out.println(" "); 
     System.out.println("If you type anything other than the options it will count as a STICK."); 
     hitOrStick = scan.next(); 
     } 
     if(hitOrStick == "H") 
      { 
      int hitCard = (int) Math.ceil(Math.random() * 10); 
      System.out.println("New Card value: "+ hitCard); 
      total = total + hitCard; 
      System.out.println(" "); 
      System.out.println("You now have: " + total); 
       if(total > 21) 
       { 
       System.out.println("YOU ARE BUST!"); 
       } 
      } 
      else 
      { 
      System.out.println(" "); 
      System.out.println("You have chosen to stick."); 
      System.out.println(" "); 
      System.out.println("Your total is: " + total); 
      computerTotal = (int) Math.ceil(Math.random() * 20); 
      System.out.println(" "); 
      System.out.println("the computer has: " + computerTotal); 
      } 

      if (computerTotal > total) 
      { 
      System.out.println(" "); 
      System.out.println("COMPUTER WINS!"); 
      } 
      else 
      { 
      System.out.println(" "); 
      System.out.println("YOU WIN!"); 
      } 





} 
} 
+1

将显式空值置于您的var。例如String hitOrStick = null; – user1697575

回答

2

只需要声明的初始值:

String hitOrStick = ""; // or null 

编译器不解释你的代码,以确保该变量将被设置为一个值时,它的使用,它只是指出,它可能是可能的。

事实上,如果您使用的是Eclipse,您可以设置您的首选项,以便此编译器错误仅仅是警告或忽略的条件。

+0

或者您可以点击“继续”,观察程序是否崩溃。 – Justin

+0

嗨,感谢您的帮助,但我坚持了我应该结束while循环的原因,因为atm总是要求H/S选项 – user2233480

+1

@ user2233480您的while循环永远不会结束。看一下条件:'while(total <21)'但是在你的循环中,总数不会受到影响。你是否希望它延伸超过你的许多if语句?只需移动一个} – Justin

1

编译器抱怨你的字符串var没有初始值。将显式空值置于您的var。 例如 String hitOrStick=null;

1

在你的代码在这里:

int total; 
boolean play; 
int computerTotal = 0; 
String hitOrStick; 

几个变量已经被命名,但没有初始化。它有助于初始化它们全部:

int total = 0; 
boolean play = false; 
int computerTotal = 0; 
String hitOrStick = ""; 

通过这样做,您可以确保更少的错误发生。因为hitOrStick被命名,未初始化,然后在后面的代码发生


你的错误,你有if (hitOrStick == "H")(这不是一个好主意,更好地为if (hitOrStick.equals("H"))),而hitOrStick尚未初始化,它可以被调用。

+0

循环内的初始化**肯定会**持续循环。它与范围无关。但是,如果前两张牌产生** 22 **并且'while'循环从不会穿过它的正文,那么实际上会有一个未初始化变量的比较。 –