我正在嘗試修復此代碼的時間最爲困難。任何和所有的幫助,不勝感激。在我開始調試之前,沒有紅色的下劃線或任何東西。但是,當我打的調試,這行代碼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);
}
}
}
}
將它包裝在'string.IsNullOrEmpty'中。 – Arran
[爲什麼在使用read()之後從c#中的控制檯爲readLine()獲取null值的可能重複(http://stackoverflow.com/questions/17781109/why-getting-null-value-from-console-in- c-sharp-for-readline-after-using-read) – wudzik
@wudzik:與鏈接的問題無關; OP不調用'Read'。 –