我想要一個ApplicationException異常,它只會在輸入不是數字時顯示消息。這是我現在所擁有的:只顯示來自ApplicationException的消息
static void getBookInfo(Book book)
{
bool isNumeric;
float number;
string numberInput;
Console.Write("Enter Book Title: ");
book.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ");
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Book Price: $");
numberInput = Console.ReadLine();
isNumeric = float.TryParse(numberInput, out number);
if (isNumeric)
book.Price = number;
else
{
throw new ApplicationException
(
"This is not a number!\n" +
"Please try again."
);
}
}
整個Program.cs後編輯工作。問題在於ApplicationException部分顯示了整個異常的打印輸出,現在不是這樣做,它只顯示消息部分。通常這很簡單。 :)
using System;
namespace Lab_6
{
class Program
{
static void Main(string[] args)
{
Address address = new Address();
address.StreetNumber = "800";
address.StreetName = "East 96th Street";
address.City = "Indianapolis";
address.State = "IN";
address.ZipCode = "46240";
Book book = new Book();
try
{
getBookInfo(book);
book.PublisherAddress = address;
book.PublisherName = "Sams Publishing";
Console.WriteLine("----Book----");
book.display();
}
catch (NegativeInputException ex)
{
Console.WriteLine(ex.Message);
return;
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.Message); // I had to change so I have only this,
// instead of whole printout.
return;
}
}
static void getBookInfo(Book book)
{
bool isNumeric;
float number;
string numberInput;
Console.Write("Enter Book Title: ");
book.Title = Console.ReadLine();
Console.Write("Enter Author's First Name: ")
book.AuthorFirstName = Console.ReadLine();
Console.Write("Enter Author's Last Name: ");
book.AuthorLastName = Console.ReadLine();
Console.Write("Enter Book Price: $");
numberInput = Console.ReadLine();
isNumeric = float.TryParse(numberInput, out number);
if (isNumeric)
book.Price = number;
else
{
throw new ApplicationException
(
"This is not a number!\n" +
"Please try again."
)
}
}
}
}
對於它什麼原因,什麼,你可以請修復格式。 –
這讓我很頭疼,但我已經糾正它。每次我粘貼代碼,然後選擇,然後按代碼格式化按鈕,它會進入雙倍間距......奇怪。 – HelpNeeder