0

我有这段代码,我花了太多时间,我想不通。它应该得到数字的平均值,然后输出答案。我输入名称,然后当我输入数字时,它只需要 1,之后我无法继续。我是小基础的初学者,很难理解这一点。我知道 Visual Basic 更高级,所以我在进入 Visual Basic 之前尝试理解简单的东西。如果我能得到一些建议,将不胜感激。

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name=TextWindow.Read()

While name<>""
TextWindow.Write(" Enter the grades :")
grades=textwindow.ReadNumber()
While grades<>""
total= total+grades
count=count+1
EndWhile
TextWindow.Write(name+ "average is" +total/grades)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name=textwindow.Read()
4

3 回答 3

1

我无法测试 atm,但我认为您在代码末尾缺少 EndWhile。(您使用 2 个循环,但仅结束 1 个)

  • 在 Visual basic 中创建这个程序实际上会更容易,因为关于 VB 的信息比关于小型基础的信息要多得多。
于 2014-07-14T08:14:59.053 回答
0

您确实需要一个 endwhile,但不要使用第二个 while - 这会导致无限循环。我已经为您修复了代码:

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name = TextWindow.Read()

While name<>""
  TextWindow.Write(" Enter the grades :")
  grades = textwindow.ReadNumber()
  If grades > 0 Then 
    total= total+grades
    count=count+1
  Else
    Goto end
  EndIf
EndWhile
end: 
TextWindow.WriteLine(name+ " average is " +total/count)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name = textwindow.Read()

希望这可以帮助。

于 2014-10-19T16:52:51.570 回答
0

我已经解决了你的问题!,而不是无限等待成绩小于 0 来显示平均值。我给它一个计数条件,如果计数等于 3(3 个学期(因为它是关于学校的))。然后它将停止显示该年学生的平均成绩(3 个学期)。

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name = TextWindow.Read()

While name<>""
  TextWindow.Write(" Enter the grades :")
  grades = textwindow.ReadNumber()
  If grades > 0 Then 
    total= total+grades
    count=count+1
  Else
    Goto end
  EndIf
  If count= 3 then
    Goto end
    EndIf
EndWhile
end: 
TextWindow.WriteLine(name+ " average is " +total/count)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name = textwindow.Read()
于 2015-05-10T13:46:13.267 回答