2015-02-07 84 views
-2
Getting this when running program 

customerbill.rb:28:语法错误,意外的tIVAR,预计输入结束。我试图计算一个红宝石餐厅检查。Ruby语法错误,意外的tIVAR,预计输入结束

class CustomerBill 


class Bill < CustomerBill 
    def initalize (burgers, drinks, subtotal) 
    @burgers = 6.95 * 5 
    @drinks = 1.75 * 4 
    @meal = @burgers + @drinks 
    @totalBill = @meal + @taxAmount + @tipAmount 
end 

class CustomerTax < CustomerBill 
    def initalize (tax, taxAmount, totalWithTax) 
    @tax   = 0.0825 
    @taxAmount = @meal * @tax 
    @totalWithTax = @meal + @tax 
end 

class CustomerTip 
def initalize (tipRate, tipAmount) 
@tipRate = 0.15 
@tipAmount = @totalWithTax * @tipRate 
end 

puts "Total meal charge #{@meal}" 
puts "Tax amount #{@taxAmount}" 
puts "Tip amount #{@tipAmount}" 
puts "Total bill #{@totalBill}" 
+1

这将是超级,如果你可以重新格式化,以便适当的东西被标记为代码。 :)另外,如果这是实际的代码,那里有非常多的结束。 :) – 2015-02-07 19:36:31

+0

真的不清楚你想要做什么。您在结束语中肯定会遇到一些问题,但不止于此。例如,您是否真的希望在CustomerBill内部定义账单类? – nPn 2015-02-07 19:37:44

+1

@DavidHoelzer从格式化很难判断,但实际上并没有足够的_ends_。 – 2015-02-07 20:57:09

回答

1

正如heading_to_tahiti的回答指出,你有一个缺少结束声明,但除了你完全误解在Ruby中使用类。你想要做的只是这个:

burgers = 6.95 * 5 
drinks = 1.75 * 4 
meal = burgers + drinks 
tax   = 0.0825 
taxAmount = meal * tax 
totalWithTax = meal + taxAmount 
tipRate = 0.15 
tipAmount = totalWithTax * tipRate 
totalBill = meal + taxAmount + tipAmount 



puts "Total meal charge #{meal}" 
puts "Tax amount #{taxAmount}" 
puts "Tip amount #{tipAmount}" 
puts "Total bill #{totalBill}" 
+0

那正是我需要做的。我在这个假设下必须上课。感谢您的解决方案为我解决了这个问题。 – user3905353 2015-02-07 20:40:03

+0

@ user3905353。你刚刚从我的答案中删除了正确的答案。由于nPn解决方案确实会产生结果,因此它不会直接解决您的问题根源中的语法错误,这是“输入输入结束时出现错误”的语法错误。我立即为你提供了正确的答案。在解决了你的问题后,你用一个单独的问题评论了我的答案,其中一个变量没有被初始化,我也解决了这个问题。 – 2015-02-07 20:52:24

+0

@ user3905353 - 只是想为我以前的评论添加更多的上下文。 SO旨在帮助解决编程问题并建立一个解决方案库。在这个例子中,你发布了如何解决“语法错误,意外tIVAR,期待输入结束”的问题,通过添加缺少的“结束”语句来解决。其他搜索SO类似问题的新手程序员无法通过简单地删除类和方法定义来解决此问题。这根本不是你发布的问题的真实世界的解决方案。 – 2015-02-08 02:37:12

1

你缺少你结束语句的其关闭的定义,这是我为什么错误指出“期待输入结束”。与最终报表解决关闭所有的定义,方法,类等等,即

class Bill < CustomerBill 

    def initalize (burgers, drinks, subtotal) 
     @burgers = 6.95 * 5 
     @drinks = 1.75 * 4 
     @meal = @burgers + @drinks 
     @totalBill = @meal + @taxAmount + @tipAmount 
    end 

end 
+0

谢谢大家。我通过修复结束语来清除错误。现在我的字符串正在打印,但实例变量未显示。放入“总餐费#{@餐}” 放入“税额#@ taxAmount” 放入“Tip amount#@ tipAmount” puts“Total bill#{@ totalBill}” – user3905353 2015-02-07 19:59:57

+0

这是另一个问题和单独的问题。请将我的答案标记为关于您的语法错误的正确答案。 – 2015-02-07 20:11:22

+0

要解决您的第二个问题,您有一些与代码有关的重大结构问题,您应该真正解决。但简单地说,你的变量(@tipAmount)不打印,因为它们是零,这意味着它们在你的代码中调用它们之前没有被初始化。 – 2015-02-07 20:13:05

相关问题