通过标签循环

时间:2015-02-01 07:46:00

标签: loops label output goto basic

我无法通过我通过标签创建的循环。循环应该经过100次并打印出相应的消息,如果它可以被3整除,或者如果它可以被3和5整除。我是基本的新手并且正在使用桥接水解释器来编译程序。这是源代码。该程序一直运行到第二部分"打印消息,然后没有输出。我试图重写循环,但我仍然无法让它工作。我们应该只使用if-then和goto函数

print "Enter product cost."
input productCost
print "Enter in amount paid."
input amountPaid
if amountPaid < productCost then
   print "Insufficient amount paid. Program exiting."
stop
end if
if amountPaid >= productCost then
   change = amountPaid - productCost
   dollars = int(change / 1)
   quarters = int(change / .25)
   dimes = int(change /  .10)
   nickles = int(change / .05)
   pennies = int(change / .01)

   print "Change in dollars: ";  dollars
   print "Change in quarters: "; quarters
   print "Change in dimes: "; dimes
   print "Change in nickles: "; nickles
   print "Change in pennies: "; pennies
end if
print "Entering second part of the program."
totalIterations = 1
secondPart:
if totalIterations < 100 then  
   if totalIterations / 3 = 0 And totalIterations / 5 = 0 then
      print "The number: " + totalIterations + " is FizzBuzz"
      totalIterations = totalIterations + 1
   end if
   if totalIterations / 3 = 0 And totalIterations / 5 > 0 then
      print "The number: " + totalIterations + " is Fizz"
      totalIterations = totalIterations + 1
   end if
   if totalIterations / 3 > 0 And totalIterations / 5 = 0 then
      print "The number: " + totalIterations + " is Buzz"
      totalIterations = totalIterations + 1
   end if
end if
goto secondPart

2 个答案:

答案 0 :(得分:0)

您只在内部totalIterations内增加if - 从未输入。你应该把它移到“主”if,这样你就可以继续增加它,即使它不能被3或5整除:

totalIterations = 1
secondPart:
if totalIterations < 100 then  
   if totalIterations / 3 = 0 And totalIterations / 5 = 0 then
      print "The number: " + totalIterations + " is FizzBuzz"
   end if
   if totalIterations / 3 = 0 And totalIterations / 5 > 0 then
      print "The number: " + totalIterations + " is Fizz"
   end if
   if totalIterations / 3 > 0 And totalIterations / 5 = 0 then
      print "The number: " + totalIterations + " is Buzz"
   end if
   totalIterations = totalIterations + 1
end if
goto secondPart

答案 1 :(得分:0)

totalIterations = 1
secondPart:
if totalIterations < 100 then  
  ...
end if
goto secondPart

无论你在...上写什么,这都是一个无限循环。由于您声明&#34;我们应该只使用if-thengoto函数&#34;,通过编写解决循环问题:

totalIterations = 1
secondPart:
...
if totalIterations < 100 then goto secondPart

但等一下,使用这些数字,循环只会运行 99 次!此外,您只需在测试用例中递增 totalIterations 变量,这意味着如果没有,则变量根本不会前进。解决这两个问题会导致:

totalIterations = 0
secondPart:
totalIterations = totalIterations + 1
...
if totalIterations < 100 then goto secondPart
if totalIterations / 3 = 0 And totalIterations / 5 = 0 then
if totalIterations / 3 = 0 And totalIterations / 5 > 0 then
if totalIterations / 3 > 0 And totalIterations / 5 = 0 then

以上测试可分性

在没有任何类型后缀的情况下,BASIC会将您的 totalIterations 变量视为浮点值,然后执行常规除法运算,生成一个小数部分,将渲染所有这些and的错误。这可能是你的程序陷入无限循环的第二个原因。

解决方案在BASIC mod运算符中,它返回等效除法的余数。我不太可能知道您正在使用的BASIC变体,但其中许多变量将允许后缀来定义变量整数。这就是你需要的:

if (totalIterations% mod 15) = 0 then
  print "The number: ";totalIterations%;" is FizzBuzz"
end if
if (totalIterations% mod 3) = 0 then
  print "The number: ";totalIterations%;" is Fizz"
end if
if (totalIterations% mod 5) = 0 then
  print "The number: ";totalIterations%;" is Buzz"
end if
相关问题