我在这里有一个非常简单的代码,在运行控制台时会显示'你的卡是(随机数)(作为选择输入的数字)。当我瞄准控制台显示卡的套装时而不是用户输入的数字,我认为赋值语句将允许。
type
Suit = array[1..4] of string;
var
SuitChoice : Suit;
I : integer;
SuitName : string;
Value : Integer;
Index : integer;
Choice : string;
begin
randomize;
Writeln('What is your suit?');
readln(SuitChoice[i]);
SuitChoice[1]:= 'Clubs';
SuitChoice[2]:= 'Diamonds';
SuitChoice[3]:= 'Hearts';
SuitChoice[4]:= 'Spades';
For index:= 1 to 13
do
Value:=Random(index);
writeln('Your card is the ', Value, ' of ', SuitChoice[i]);
readln;
end.
答案 0 :(得分:1)
您发布的代码存在一些问题。让我们一次一个地看看它们:
type
Suit = array[1..4] of string;
var
SuitChoice : Suit;
I : integer;
SuitName : string;
Value : Integer;
Index : integer;
Choice : string; { You declare but never use this variable, but
use i instead. Remove one of them. }
begin
randomize;
Writeln('What is your suit?');
{ You use an uninitialized variable to access the SuitChoice[] array }
readln(SuitChoice[i]);
{
You immediately overwrite anything you might have written (if the
uninitialized value of i happened to be 1-4) with new values
}
SuitChoice[1]:= 'Clubs';
SuitChoice[2]:= 'Diamonds';
SuitChoice[3]:= 'Hearts';
SuitChoice[4]:= 'Spades';
{
The next lines produce no output except for the final
Value := Random(13). There should be a begin..end block
here, so that both the assignment to Value and the
WriteLn execute each pass through the loop.
}
For index:= 1 to 13 do
Value:=Random(index);
writeln('Your card is the ', Value, ' of ', SuitChoice[i]);
readln;
end.
将所有这些放在一起应该会给你更多这样的东西:
program Cards;
type
Suit = array[1..4] of string;
var
SuitChoice : Suit;
SuitName : string;
Value : Integer;
Index : integer;
Choice : string;
begin
randomize;
SuitChoice[1]:= 'Clubs';
SuitChoice[2]:= 'Diamonds';
SuitChoice[3]:= 'Hearts';
SuitChoice[4]:= 'Spades';
{ Write prompt, and read value into i.
Writeln('What is your suit?');
readln(Choice);
{
Make sure we execute both the value assignment and the
WriteLn on each pass through the loop
}
for index:= 1 to 13 do
begin
Value := Random(index);
WriteLn('Your card is the ', Value, ' of ', SuitChoice[Choice]);
end;
ReadLn;
end.
由于您使用它的方式,您可以通过将SuitType
更改为常量声明来稍微改善这一点。它可以在一行中完成,并保存初始化每个元素的代码,因为它是在编译时而不是运行时完成的。
const
SuitChoice: array[1..4] of string = ('Clubs', 'Diamonds', 'Hearts', 'Spades');
然后,您可以完全删除SuitType
类型,以及初始化它的4行,并将WriteLn
语句更改为
WriteLn('Your card is the ', Value, ' of ', SuiteChoice[Choice]);
或者,IMO更简单地作为
WriteLn(Format('Your card is the %s of %s', [Value, SuitChoice[Choice]]));