2015-02-10 45 views
0
import java.util.Scanner; 
//This program computes tax and tip on a restaurant bill. 

public class RestaurantBill 
{ 
    public static void main(String[] args) { 


    double charge; 
    double tax = 0.0675; 
    double tipRate = 0.15; 
    double totalWithTax; 
    double taxAmount; 
    double tipAmount; 
    double grandTotal; 


    Scanner keyboard = new Scanner(System.in); 
    System.out.print("What is your name? "); 
    name = keyboard.nextLine(); 
    System.out.print("Please enter the cost of your meal "); 
    charge = keyboard.nextDouble(); 

    System.out.println("Hi there " + name "\n Your tax          is "       +      taxAmount "  \n  Your tip will be $" + tipAmount " /n and your total cost will be $" + grandTotal); 
    } 
} 

,我得到這個錯誤一遍又一遍......與餐廳的計費程序連續錯誤

RestaurantBill2.java:24: error: ')' expected 
    System.out.println("Hi there " + name "\n Your tax is " + taxAmount " \n Your tip will be $" + tipAmount " /n and your total cost will be $" + grandTotal); 

回答

0

語法錯誤在字符串連接:name後添加+taxAmount後和tipAmount的線表示在錯誤信息中。

或分割線分成幾個獨立的打印:

System.out.println("Hi there " + name); 
System.out.println("\n Your tax is " + taxAmount); 
System.out.println("\n Your tip will be $" + tipAmount); 
System.out.println("/n and your total cost will be $" + grandTotal); 

檢查,如果你真的需要打印\n使用功能時...... LN後綴。

您可能還想用\n替換/n並清理一些空格。
你當然需要添加一些計算,太...

+0

耶多餘的空格添加自己,當我到這個網站傳輸的代碼......不知道爲什麼會發生...但我重新寫了整個事情...現在我得到一個「變量尚未初始化」的錯誤... – none 2015-02-10 21:34:01

+0

這似乎應該得到一個單獨的問題。 – CiaPan 2015-02-11 06:20:34