【问题标题】:Pascal beginner program帕斯卡初学者程序
【发布时间】:2013-01-28 02:28:14
【问题描述】:
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');

【问题讨论】:

  • 问你自己这个:当你第一次到达这条线时,repeatvar 的值是多少:While (repeatvar = 'y') do。问问自己测试正在检查什么以及您要提供什么。即这行第一次执行是什么时候,你什么时候要求输入!
  • 我想实现这个c++代码!! ,我编辑的那个。请看看@PreetSangha。我对编程比较陌生,所以逻辑有点任性。
  • c++ to pascal conversion of a beginner program 的可能副本。当您的问题结束时,请勿再次发布相同的问题作为新问题。相反,改进已关闭的问题并要求重新打开它。将其作为副本重新发布违反了本网站的准则。
  • 好的,请记住这一点。对此我很抱歉。我是这个网站的新手。谢谢! @KenWhite - 我应该如何要求重新打开我之前的问题?
  • 请不要这样编辑您的问题,因为这会使现有的 cmets/answers/suggestions 变得毫无意义。如果您还有其他问题,请将其作为问题发布。

标签: pascal freepascal


【解决方案1】:

因为repeatvar 从未初始化,所以您的程序永远不会进入while 循环,因为repeatvar 无法以'y' 开头。

【讨论】:

    【解决方案2】:

    你忘了初始化变量

    repeatvar := 'y'
    

    在行前插入上述行

    While (repeatvar = 'y') do 
    Begin
    
    ...
    
    End
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      相关资源
      最近更新 更多