2016-02-04 34 views
-1

找不到我的语法错误。从我的阅读中期待结束关键字?Ruby中的嘶嘶响buzz代码中的未捕获语法错误

我已经包含了除了错误语句和代码之外还在测试我的代码的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 FILE:

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>' 
+4

这不是_python_:p –

+2

您必须在Ruby中用'end'终止'if'语句。 –

+0

刚刚计算出来感谢马立克。现在我解决了这个问题,但由于某些原因代码在代码的“FizzBu​​zz”部分返回“Fizz”。 –

回答

0

你需要终止你的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 
+0

我认为最好将所有if语句组合在一起,并以某种方式排列它们,以便它们返回所需的结果。例如,“int%3 == 0”和“int%3 == 0 && int%5 == 0”。如果你按照当前顺序和'int = 15'命令'if'语句,你将'放入'Fizz'',然后'放入'Buzz'',最后你将'放入'FizzBu​​zz''。 –

+0

这是正确的答案。任何对风格,正确性或编码质量的评论都与这个问题无关。 –

+0

如果我添加所有三个失败。当我保持返回时只有FIzzBuzz部分失败。 –

0

你想要的东西像这样:

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的倍数。

相关问题