【发布时间】:2014-04-25 15:55:29
【问题描述】:
感谢所有答案,正如我之前所说,我是初学者,所以也许我会尝试从另一面展示我的问题。一开始我写了一个像下面这样的工作程序,但后来我意识到我的任务是使用那个密封的类,这对我来说太难了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Projekt_1__konsola
{
sealed class Element
{
int val;
public Element(int e)
{
val = e;
}
public int v
{
get
{
return val;
}
}
}
class Program
{
static void UstawienieStylu()
{
Console.Title = "Projekt";
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
}
static void PodajLiczbę(string komunikat, out int liczba)
{
while (true)
{
Console.Write(komunikat);
string str = Console.ReadLine();
try
{
liczba = int.Parse(str);
break;
}
catch (FormatException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wprowadzono liczbę w złym formacie");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (OverflowException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wartość jest za duża albo za mała");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (ArgumentNullException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Napotkano koniec strumienia");
Console.ForegroundColor = ConsoleColor.Black;
}
Console.WriteLine("Spróbuj jeszcze raz");
}
}
static void Sortuj(int[] tablica)
{
for (uint i = 1; i < tablica.Length; i++)
{
uint j = i;
int buf = tablica[j];
while ((j > 0) && (tablica[j - 1] > buf))
{
tablica[j] = tablica[j - 1];
j--;
}
tablica[j] = buf;
}
}
static void Main(string[] args)
{
UstawienieStylu();
int liczba;
PodajLiczbę("Podaj liczbę elementów do posortowania: ", out liczba);
int element, i;
int[] tablica = new int[liczba];
for (i = 0; i < liczba; i++)
{
PodajLiczbę("Podaj element [" + i + "]: ", out element);
tablica[i] = element;
}
Sortuj(tablica);
Console.WriteLine("Posortowane elementy: ");
for (i = 0; i < liczba; i++)
{
Console.WriteLine("Element [{0}] = {1}", i, tablica[i]);
}
}
}
}
我的任务是编写一个程序,该程序使用开头编写的确切类。没有它会很容易,但现在不是,因为我是编程初学者,尤其是对象。我做错了什么?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Projekt_1__konsola
{
sealed class Element
{
int val;
public Element(int e)
{
val = e;
}
public int v
{
get
{
return val;
}
}
}
class Program
{
static void UstawienieStylu()
{
Console.Title = "Projekt 1";
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Clear();
}
static Element[] WczytajDaneZKonsoli()
{
while (true)
{
string str = Console.ReadLine();
int element;
try
{
element = int.Parse(str);
break;
}
catch (FormatException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wprowadzono liczbę w złym formacie");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (OverflowException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Wartość jest za duża albo za mała, pamiętaj że możesz podać liczby z zakresu 1 do 4294967295");
Console.ForegroundColor = ConsoleColor.Black;
}
catch (ArgumentNullException)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Napotkano koniec strumienia");
Console.ForegroundColor = ConsoleColor.Black;
}
Console.WriteLine("Spróbuj jeszcze raz");
}
}
static void Sortowanie(Element[] tablica)
{
for (uint i = 1; i < tablica.Length; i++)
{
uint j = i;
int buf = tablica[j].v;
while ((j > 0) && (tablica[j - 1].v > buf))
{
tablica[j].e = tablica[j - 1].v;
j--;
}
tablica[j].e = buf;
}
}
static void WyświetlDane(Element[] elementy)
{
Console.WriteLine("Posortowane elementy: ");
for (int i = 0; i < elementy.Length; i++)
{
Console.WriteLine("Element [{0}] = {1}", i, elementy[i].v);
}
}
static void Main(string[] args)
{
UstawienieStylu();
Element[] elementy = WczytajDaneZKonsoli();
Sortowanie(elementy);
WyświetlDane(elementy);
}
}
}
【问题讨论】:
-
顺便说一句,
ArgumentNullException并不意味着您的错误消息所说的那样