为什么我的Ada程序无法正确输出?

时间:2018-04-11 10:39:55

标签: ada

首先我要说这是我创建的第一个ada程序。我不知道它是如何工作的,我的任务非常简单。但是,输出无法正常工作。它适用于第一个变量,但不适用于接下来的两个变量。它还打印出第一个奇怪的变量。这是我的代码:

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
    Author, Title, Pages: String := " ";
begin

    Put("Enter Author: ");
    Get(Author);
    Put("Enter Title: ");
    Get(Title);
    Put("Enter number of pages: ");
    Get(Pages);
    Put("Author: ");
    Put(Author);
    New_Line;
    Put("Title: ");
    Put(Title);
    New_Line;
    Put("Number of pages: ");
    Put(Pages);

end Main;

目标只是输入有关书籍的信息,程序会将其读出给您。这是输出:

Enter Author: john
Enter Title: Enter number of pages: Author: j
Title: o
Number of pages: h

旁注,我无法将页码作为整数运行。 getput方法只是出错了。这并不重要,但如果有人能帮助我做一个整数,我会很感激。

1 个答案:

答案 0 :(得分:5)

这是一个可能的解决方案:

with Ada.Text_IO;

procedure Text_Input is

   type Page_Count is range 1 .. 10_000;

   package Page_Count_Text_IO is new Ada.Text_IO.Integer_IO (Page_Count);

   function Get_Line (Message : in String) return String;
   function Get_Line (Message : in String) return Page_Count;

   function Get_Line (Message : in String) return String is
   begin
      Ada.Text_IO.Put (Message);
      return Ada.Text_IO.Get_Line;
   end Get_Line;

   function Get_Line (Message : in String) return Page_Count is
   begin
      return Result : Page_Count do
         Ada.Text_IO.Put (Message);
         Page_Count_Text_IO.Get (Result);
         if Ada.Text_IO.Get_Line /= "" then
            raise Constraint_Error
              with "Page count followed by extra characters.";
         end if;
      end return;
   end Get_Line;

   Author : constant String     := Get_Line ("Enter author: ");
   Title  : constant String     := Get_Line ("Enter title: ");
   Pages  : constant Page_Count := Get_Line ("Enter number of pages: ");
begin
   Ada.Text_IO.Put_Line ("Author:          " & Author);
   Ada.Text_IO.Put_Line ("Title:           " & Title);
   Ada.Text_IO.Put_Line ("Number of pages:" & Page_Count'Image (Pages));
end Text_Input;

请注意,我已Get_Line Page_Count class Faqs extends Component { constructor(props){ super(props); this.state = { isHidden: true } } toggleHidden () { this.setState({ isHidden: !this.state.isHidden }) } renderAnswer() { if (this.state.isHidden) { return; } return ( <p>Answer</p> ); } render() { return ( <div> <span onClick={() => this.toggleHidden()}><strong>This is the question</strong></span> { this.renderAnswer() } <br/> <span onClick={() => this.toggleHidden()}><strong>Question2</strong></span> { this.renderAnswer() } <br/> <hr></hr> </div > ) } } 函数检查您是否在该行上没有任何尾随垃圾,您输入的页数。

我希望你不同意我的估计,约翰永远不会写一本超过10页的书。 : - )