【问题标题】:Uncaught syntax error in fizz buzz code in RubyRuby 中的 Fizz Buzz 代码中未捕获的语法错误
【发布时间】:2016-02-04 20:03:31
【问题描述】:

无法弄清楚我的语法错误。从我的阅读来看,它需要一个 end 关键字?

除了错误语句和代码之外,我还包含了正在测试我的代码的 rspec 文件。感谢所有帮助的人!

def fizzbuzz(int)
  if int % 3 == 0;
    puts "Fizz";
    if int % 5 == 0;
      puts "Buzz";
     if int % 3 && 5 == 0;
       puts "FizzBuzz"; 
end

RSPEC 文件:

require_relative './spec_helper.rb'

describe "fizzbuzz" do
  it 'returns "Fizz" when the number is divisible by 3' do
    fizz_3 = fizzbuzz(3)

    expect(fizz_3).to eq("Fizz")
  end
  it 'returns "Buzz" when the number is divisible by 5' do
    fizz_5 = fizzbuzz(5)

    expect(fizz_5).to eq("Buzz")
  end
  it 'returns "FizzBuzz" when the number is divisible by 3 and 5' do
    fizz_15 = fizzbuzz(15)

    expect(fizz_15).to eq("FizzBuzz")
  end
  it 'returns nil when the number is not divisible by 3 or 5' do
    fizz_4 = fizzbuzz(4)

    expect(fizz_4).to eq(nil)
  end
end

错误:

/Users/user/Development/code/rspec-fizzbuzz-001-prework-web/spec/spec_helper.rb:8:in `require_relative': /Users/user/Development/code/rspec-fizzbuzz-001-prework-web/fizzbuzz.rb:8: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
    from /Users/user/Development/code/rspec-fizzbuzz-001-prework-web/spec/spec_helper.rb:8:in `<top (required)>'
    from /Users/user/Development/code/rspec-fizzbuzz-001-prework-web/spec/fizzbuzz_spec.rb:1:in `require_relative'
    from /Users/user/Development/code/rspec-fizzbuzz-001-prework-web/spec/fizzbuzz_spec.rb:1:in `<top (required)>'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `load'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `each'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:102:in `setup'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:88:in `run'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in `run'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in `invoke'
    from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.4.1/exe/rspec:4:in `<top (required)>'
    from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
    from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
    from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
    from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

【问题讨论】:

  • 这不是 python :p
  • 你必须在 Ruby 中用 end 终止 if 语句。
  • 刚刚想通了,谢谢 Marek。现在我解决了这个问题,但由于某种原因,代码在代码的“FizzBu​​zz”部分返回“Fizz”。
  • if int % 3 &amp;&amp; 5 == 0; 你需要说if int % 3 == 0 &amp;&amp; int %5 == 0
  • 并为这些场景检查 if/elsif/end 和/或 case 语句。

标签: ruby rspec


【解决方案1】:

你需要终止你的 if 语句,这应该可以解决问题:

def fizzbuzz(int)
    if int % 3 == 0
        puts "Fizz"
    end
    if int % 5 == 0
        puts "Buzz"
    end
    if (int % 3 == 0) && (int % 5 == 0)
        puts "FizzBuzz"
    end
end

【讨论】:

  • 我认为最好将所有if 语句组合在一起,并以某种方式对它们进行排序,以便它们返回您想要的结果。例如,int % 3 == 0int % 3 == 0 &amp;&amp; int % 5 == 0。如果您按当前顺序订购了if 语句,而int = 15,您将是puts "Fizz",然后是puts "Buzz",最后是puts "FizzBuzz"
  • 这是正确答案。任何关于风格、正确性或编码质量的评论都与此问题无关。
  • 如果我添加 puts 所有三个都失败。当我保留返回时,只有 FIzzBuzz 部分失败。
【解决方案2】:

你想要这样的东西:

def fizzbuzz(int)
  if int % 3 == 0 && int % 5 == 0
    puts "FizzBuzz"
  elsif int % 3 == 0
    puts "Fizz"
  elsif int % 5 == 0
    puts "Buzz"
  end
end

这将仅输出“FizzBu​​zz”,如果它是两者的倍数,“Fizz”仅用于 3 的倍数,“Buzz”仅用于 5 的倍数。

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多