Pascal初学者计划

时间:2013-01-28 02:28:14

标签: pascal freepascal

Program q2;


Var input: integer ;
Var repeatvar: char ;



Procedure display(input : integer) ;



Begin
If (input = 9) then 
  Write ('********************************************************') ;
  Write ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
  Write ('********************************************************') ;    
If (input = 8) then
  Write ('********************************************************') ;
  Write ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
  Write ('********************************************************') ;
If (input = 7) then
  Write ('********************************************************') ;
  Write ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
  Write ('********************************************************') ;
If (input = 6) then
  Write ('********************************************************') ;
  Write ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
  Write ('********************************************************') ;
If (input = 5) then
  Write ('********************************************************') ;
  Write ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
  Write ('********************************************************') ;
If (input = 4) then
  Write ('********************************************************') ;
  Write ('4! = 4 x 3 x 2 x 1 = 24 ');
  Write ('********************************************************') ;
If (input = 3) then
  Write ('********************************************************') ;
  Write ('3! = 3 x 2 x 1 = 6 ');
  Write ('********************************************************') ;
If (input = 2) then
  Write ('********************************************************') ;
  Write ('2! = 2 x 1 = 2 ');
  Write ('********************************************************') ;
If (input = 1) then
  Write ('********************************************************') ;
  Write ('1! = 1 = 1 ');
  Write ('********************************************************') ;
If (input = 0) then
  Write ('********************************************************') ;
  Write ('0! = 1 = 1 ');
  Write ('********************************************************') ;

End ;


Begin

While (repeatvar = 'y') do
Begin
Write('Enter an integer between 0 and 9 or -1 to quit');
readln(input);

If (input = -1) then
  begin
  Write('Sentinel value - Exiting program !'); 
  Readln;
  end
Else
  display(input);

End 


End.

这是我编译后得到的警告。

Free Pascal Compiler version 2.4.0 [2010/05/18] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling q2.pas
q2.pas(61,8) Warning: Variable "repeatvar" does not seem to be initialized
Linking q2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
78 lines compiled, 0.2 sec
1 warning(s) issued

我在q2中键入但看不到任何输出,它没有做任何事情。我尝试了很多东西,我也是Pascal和fpc的新手。谁能告诉我这是什么问题?

在repeatvar的部分,我想实现这个:

我想实现这个c ++代码:

do
{
    cout << "Enter an integer between 0 and 9 or -1 to quit => " ;
    cin >> input ; // get number 
    if(input == -1)
    {
        cout << "Sentinel value - Exiting program !" << endl;
        exit ;
        system("pause");
        return 0;
    }
    else
    {

    // display function
    display(input) ;
    }

    cout << endl;
    cout << " Press y to repeat , any other key and enter to terminate: " ;
    cin >> repeat ;
    if(repeat != 'y')
        exit;

}while( repeat == 'y');

2 个答案:

答案 0 :(得分:2)

因为repeatvar永远不会被初始化,所以你的程序永远不会进入while循环,因为repeatvar没有办法从'y'开始。

答案 1 :(得分:0)

您忘记初始化变量

repeatvar := 'y'

在行

之前插入上一行
While (repeatvar = 'y') do 
Begin

...

End