我究竟做错了什么?尝试做一个 table(行 = 每季度销售额,列 = 销售人员)
What am I doing wrong? Trying to make a table (rows = sales per quarter, columns = salesperson)
在此程序中,您输入与每次销售相关的三项信息:销售人员的销售编号、销售季度和销售金额。如果同一个销售人员有不止一笔销售,我的程序会创建一个额外的列,而不是将销售金额添加到该销售人员的现有列中(抱歉,我对解释这个和学习如何编程一窍不通,所以如果我没有很好地解释某些事情,请告诉我。)最后,我想制作一个 table,每个销售人员只有一列,而不是每次销售(这就是我似乎现在有)。
例如,输入如下:
- sNr: 100, q: 1, amt: 50
- sNr: 200;问:2,数量:50
- sNr: 300, q: 3, amt: 50
- sNr: 100, q: 1, amt: 25
我想看到三列,每个销售人员的编号(100、200、300),每个销售人员编号下每个季度的合并销售额(第一季度是销售人员编号下的行,第二季度是下那个等等)。对于没有销售额的季度,会显示 0。
此刻,我为四个(或更多)条目中的每一个得到单独的一行,即使每个销售人员有多个条目。
这是我 运行 程序时出现的内容。
Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
100 200 300 100
50 0 0 25
0 50 0 0
0 0 50 0
0 0 0 0
Press < ENTER > to stop the program.
我想要这个输出:
Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
100 200 300
75 0 0
0 50 0
0 0 50
0 0 0
Press < ENTER > to stop the program.
下面是我的代码。有人能帮我吗?
program Sales;
var arrayN: array of integer;
var arrayS: array of array of integer;
var max, row, column, sColumn, counter, nTotal, sNr, q, amt: integer;
begin
max := 10;
sColumn := 0;
nTotal := 1;
SETLENGTH( arrayN, max );
SETLENGTH( arrayS, 5, max );
writeln('Input sales number from sales person.');
readln(sNr);
arrayN[sColumn] := sNr;
arrayS[0, sColumn] := sNr;
while (sNr <> 0) do
begin
if nTotal = LENGTH( arrayN ) then
begin
SETLENGTH( arrayN, LENGTH( arrayN ) + 5 );
end;
writeln('Give sales quarter ( 1 2 3 or 4 ) .');
readln(q);
writeln('Give amount of sale.');
readln(amt);
arrayS[q, sColumn] := arrayS[q, sColumn] + amt;
writeln('Input sales number from sales person. Input 0 to quit.');
readln(sNr);
if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
begin
sColumn := nTotal;
nTotal := nTotal + 1;
end
else
begin
for counter := 0 to HIGH( arrayN ) do
begin
if sNr = arrayN[counter] then
begin
sColumn := counter;
end;
end;
end;
arrayN[sColumn] := sNr;
arrayS[0, sColumn] := sNr;
end;
for row := 0 to 4 do
begin
for column := 0 to nTotal - 1 do
begin
write(arrayS[row, column] :5);
end;
writeln;
end;
writeln;
writeln('Press < ENTER > to stop the program.');
readln;
end.
您获得的销售人员列多于输入不同销售人员编号的原因在于条件检查和紧随其后的 begin - end
块
if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
begin
sColumn := nTotal;
nTotal := nTotal + 1;
end
因为你的销售人员编号与ArrayN的索引无关,而且你使用了100、200、300,所以条件总是成立的。您实际上可以跳过上面的块(见下文)。
输入sNr
后你应该做的是检查该号码是否已经在'ArrayN'中。您实际上是在上述条件检查的 else
部分执行此操作:
else
begin
for counter := 0 to HIGH( arrayN ) do
begin
if sNr = arrayN[counter] then
begin
sColumn := counter;
end;
end;
end;
删除 else
和外部 begin - end
块,只留下 for
循环。
您需要修改此检查,以便在 sNr
未找到 时在循环后增加 nTotal
。在循环之前增加 nTotal
并在找到匹配项时减少它更容易,例如在分配 sColumn
.
的同一 begin - end
块中
在此程序中,您输入与每次销售相关的三项信息:销售人员的销售编号、销售季度和销售金额。如果同一个销售人员有不止一笔销售,我的程序会创建一个额外的列,而不是将销售金额添加到该销售人员的现有列中(抱歉,我对解释这个和学习如何编程一窍不通,所以如果我没有很好地解释某些事情,请告诉我。)最后,我想制作一个 table,每个销售人员只有一列,而不是每次销售(这就是我似乎现在有)。
例如,输入如下:
- sNr: 100, q: 1, amt: 50
- sNr: 200;问:2,数量:50
- sNr: 300, q: 3, amt: 50
- sNr: 100, q: 1, amt: 25
我想看到三列,每个销售人员的编号(100、200、300),每个销售人员编号下每个季度的合并销售额(第一季度是销售人员编号下的行,第二季度是下那个等等)。对于没有销售额的季度,会显示 0。
此刻,我为四个(或更多)条目中的每一个得到单独的一行,即使每个销售人员有多个条目。
这是我 运行 程序时出现的内容。
Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
100 200 300 100
50 0 0 25
0 50 0 0
0 0 50 0
0 0 0 0
Press < ENTER > to stop the program.
我想要这个输出:
Input sales number from sales person.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
200
Give sales quarter ( 1 2 3 or 4 ) .
2
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
300
Give sales quarter ( 1 2 3 or 4 ) .
3
Give amount of sale.
50
Input sales number from sales person. Input 0 to quit.
100
Give sales quarter ( 1 2 3 or 4 ) .
1
Give amount of sale.
25
Input sales number from sales person. Input 0 to quit.
0
100 200 300
75 0 0
0 50 0
0 0 50
0 0 0
Press < ENTER > to stop the program.
下面是我的代码。有人能帮我吗?
program Sales;
var arrayN: array of integer;
var arrayS: array of array of integer;
var max, row, column, sColumn, counter, nTotal, sNr, q, amt: integer;
begin
max := 10;
sColumn := 0;
nTotal := 1;
SETLENGTH( arrayN, max );
SETLENGTH( arrayS, 5, max );
writeln('Input sales number from sales person.');
readln(sNr);
arrayN[sColumn] := sNr;
arrayS[0, sColumn] := sNr;
while (sNr <> 0) do
begin
if nTotal = LENGTH( arrayN ) then
begin
SETLENGTH( arrayN, LENGTH( arrayN ) + 5 );
end;
writeln('Give sales quarter ( 1 2 3 or 4 ) .');
readln(q);
writeln('Give amount of sale.');
readln(amt);
arrayS[q, sColumn] := arrayS[q, sColumn] + amt;
writeln('Input sales number from sales person. Input 0 to quit.');
readln(sNr);
if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
begin
sColumn := nTotal;
nTotal := nTotal + 1;
end
else
begin
for counter := 0 to HIGH( arrayN ) do
begin
if sNr = arrayN[counter] then
begin
sColumn := counter;
end;
end;
end;
arrayN[sColumn] := sNr;
arrayS[0, sColumn] := sNr;
end;
for row := 0 to 4 do
begin
for column := 0 to nTotal - 1 do
begin
write(arrayS[row, column] :5);
end;
writeln;
end;
writeln;
writeln('Press < ENTER > to stop the program.');
readln;
end.
您获得的销售人员列多于输入不同销售人员编号的原因在于条件检查和紧随其后的 begin - end
块
if ( sNr < LOW( arrayN ) ) or ( sNr > HIGH( arrayN ) ) then
begin
sColumn := nTotal;
nTotal := nTotal + 1;
end
因为你的销售人员编号与ArrayN的索引无关,而且你使用了100、200、300,所以条件总是成立的。您实际上可以跳过上面的块(见下文)。
输入sNr
后你应该做的是检查该号码是否已经在'ArrayN'中。您实际上是在上述条件检查的 else
部分执行此操作:
else
begin
for counter := 0 to HIGH( arrayN ) do
begin
if sNr = arrayN[counter] then
begin
sColumn := counter;
end;
end;
end;
删除 else
和外部 begin - end
块,只留下 for
循环。
您需要修改此检查,以便在 sNr
未找到 时在循环后增加 nTotal
。在循环之前增加 nTotal
并在找到匹配项时减少它更容易,例如在分配 sColumn
.
begin - end
块中