2013-12-16 224 views
1

我正在嘗試修復此代碼的時間最爲困難。任何和所有的幫助,不勝感激。在我開始調試之前,沒有紅色的下劃線或任何東西。但是,當我打的調試,這行代碼hitOrStay = Console.ReadLine().ToLower().;得到以下錯誤:「對象引用不設置到對象的實例」未將對象引用設置爲對象的實例console.readline.tolower

這裏是我的源代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace finalBlackjack 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    static string[] playerCards = new string[12]; 
    static string hitOrStay = ""; 
    static int total = 0, count = 1, dealerTotal = 0; 
    static Random cardRandomizer = new Random(); 

    static void Start() 
    { 
     dealerTotal = cardRandomizer.Next(15, 22); 
     playerCards[0] = Deal(); 
     playerCards[1] = Deal(); 
     do 
     { 
      Console.WriteLine("Welcome to Blackjack! You were dealed " + playerCards[0] + " and " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay?"); 
      hitOrStay = Console.ReadLine().ToLower(); 

     } while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay")); 
     Game(); 
    } 

    static void Game() 
    { 
     if (hitOrStay.Equals("hit")) 
     { 
      Hit(); 
     } 
     else if (hitOrStay.Equals("stay")) 
     { 
      if (total > dealerTotal && total <= 21) 
      { 
       Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
       PlayAgain(); 
      } 
      else if (total < dealerTotal) 
      { 
       Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
       PlayAgain(); 
      } 

     } 
     Console.ReadLine(); 
    } 

    static string Deal() 
    { 
     string Card = ""; 
     int cards = cardRandomizer.Next(1, 14); 
     switch (cards) 
     { 
      case 1: Card = "Two"; total += 2; 
       break; 
      case 2: Card = "Three"; total += 3; 
       break; 
      case 3: Card = "Four"; total += 4; 
       break; 
      case 4: Card = "Five"; total += 5; 
       break; 
      case 5: Card = "Six"; total += 6; 
       break; 
      case 6: Card = "Seven"; total += 7; 
       break; 
      case 7: Card = "Eight"; total += 8; 
       break; 
      case 8: Card = "Nine"; total += 9; 
       break; 
      case 9: Card = "Ten"; total += 10; 
       break; 
      case 10: Card = "Jack"; total += 10; 
       break; 
      case 11: Card = "Queen"; total += 10; 
       break; 
      case 12: Card = "King"; total += 10; 
       break; 
      case 13: Card = "Ace"; total += 11; 
       break; 
      default: Card = "2"; total += 2; 
       break; 
     } 
     return Card; 
    } 

    static void Main() 
    { 
     Start(); 
    } 

    static void Hit() 
    { 
     count += 1; 
     playerCards[count] = Deal(); 
     Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + "."); 
     if (total.Equals(21)) 
     { 
      Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealerTotal + ".\nWould you like to play again?"); 
      PlayAgain(); 
     } 
     else if (total > 21) 
     { 
      Console.WriteLine("\nYou busted, therefore you lost. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
      PlayAgain(); 
     } 
     else if (total < 21) 
     { 
      do 
      { 
       Console.WriteLine("\nWould you like to hit or stay?"); 
       hitOrStay = Console.ReadLine().ToLower(); 
      } while (!hitOrStay.Equals("hit") && !hitOrStay.Equals("stay")); 
      Game(); 
     } 
    } 

    static void PlayAgain() 
    { 
     string playAgain = ""; 
     do 
     { 
      playAgain = Console.ReadLine().ToLower(); 
     } while (!playAgain.Equals("y") && !playAgain.Equals("n")); 
     if (playAgain.Equals("y")) 
     { 
      Console.WriteLine("\nPress enter to restart the game!"); 
      Console.ReadLine(); 
      Console.Clear(); 
      dealerTotal = 0; 
      count = 1; 
      total = 0; 
      Start(); 
     } 
     else if (playAgain.Equals("n")) 
     { 
      Console.WriteLine("\nPress enter to close Blackjack."); 
      Console.ReadLine(); 
      Environment.Exit(0); 
     } 

    } 
} 
} 
+4

將它包裝在'string.IsNullOrEmpty'中。 – Arran

+2

[爲什麼在使用read()之後從c#中的控制檯爲readLine()獲取null值的可能重複(http://stackoverflow.com/questions/17781109/why-getting-null-value-from-console-in- c-sharp-for-readline-after-using-read) – wudzik

+0

@wudzik:與鏈接的問題無關; OP不調用'Read'。 –

回答

2

你可能喜歡打破此行分爲兩部分:

hitOrStay = Console.ReadLine(); 
if(!string.IsNullOrEmpty(hitOrStay)) 
    hitOrStay = hitOrStay.ToLower(); 
+2

雖然您將修復此錯誤,但您已經錯過了OP所做的基本問題:'Console.ReadLine()'不能從Windows窗體運行。 –

+1

@DanPuzey,你提到的是絕對正確的!我真的錯過了這是一個Windows窗體應用程序的事實。感謝您承認這一事實並讓我知道......愚蠢的我! :) –

8

您在Windows窗體應用程序中使用Console.ReadLine()。沒有控制檯! Console.ReadLine()將永遠返回null(立即,無需等待輸入)。這就是你看到這個問題的原因。

我懷疑您需要創建一個新項目,確保您選擇「控制檯應用程序」而不是「Windows窗體應用程序」,並將代碼添加到此應用程序中。

+0

'Console.ReadLine'作用於進程的輸入流。 「控制檯」有點不恰當 - 例如,如果另一個進程管理數據,您將能夠在WinForms中讀取它。這一區別很重要,儘管您在這方面有所發現。 – Gusdor

+1

是的,@Gusdor,但在WinForms應用程序中,沒有任何東西連接到控制檯輸入。 ['Console.ReadLine'](http://msdn.microsoft.com/en-us/library/system.console.readline(v = vs.110).aspx)返回'null'「,如果沒有更多行可用「。在WinForms應用程序中,默認情況下總是這樣。鑑於發佈的代碼,我懷疑OP是否重新接線標準輸入/輸出。 –

+0

我同意。我並不矛盾你,只是增加了味道:) – Gusdor

相關問題