通过标签循环



我在通过标签创建的循环时遇到问题。循环应该经过 100 次,如果它能被 3、5 整除,或者如果它能被 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

你只在内部if内递增totalIterations - 永远不会输入。您应该将其移动到"主"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
totalIterations = 1
secondPart:
if totalIterations < 100 then  
  ...
end if
goto secondPart

不管你在...上写什么,这是一个无限循环。既然你说"我们应该只使用if-thengoto函数",那就通过编写来解决循环问题:

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的 FALSE。这将是您的程序陷入无限循环的第二个原因。

解决方案在 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

最新更新