2013-10-24 16 views
2

我試着編譯這一點,得到了一個錯誤說:$ javac的Question2.java 2> Question2.java:55:錯誤:文件的最終達成在分析 } ^ 1錯誤類型不能解決變量上的Java程序,具有字符串和編譯錯誤

我很確定代碼是正確的,但有一點我缺少的東西。該計劃旨在檢查票證類型(標準/貴賓/限制)和折扣(無/學生/領取養老金),如果選擇VIP,沒有折扣,並且學生和退休人員有5%和10%的折扣分別,但我無法理清輸入方法或輸入聲明。

任何形式的幫助表示讚賞。 PS,我是一名正在學習Java的學生,似乎無法找到解決此問題的確切解決方案,因此我在此處發佈了一個問題。

import java.util.Scanner; 



public class Question2 { 

public static void main(String args[]){ 

    Scanner userinput = new Scanner(System.in); 

    String ticket ; 
    System.out.print(" Type of Ticket: "); 
    ticket = userinput.next(); 

    String discount; 
    System.out.print("What sort of discount do you have?"); 
    discount = userinput.next(); 

    int standard = 40; 
    int restricted = 20; 
    int VIP = 60; 



    if ((ticket == "standard") && (type == "student")) { 
     double standard_student = standard * 0.95; 
     } 

    if ((ticket == "standard") && (type == "pensioners")) { 
     double standard_pensioners = standard * 0.90; 
     } 


    if ((ticket == "restricted") && (type == "student")) { 
     double restricted_students = restricted * 0.95; 
    } 


    if ((ticket == "restricted") && (type == "pensioner")) { 
     double restricted_pensioner = restricted * 0.90; 
    } 

    if (ticket=="standard") 
     System.out.print("Your ticket costs $."); 

    if (ticket == "restricted") 
     System.out.print("Your ticket costs $."); 


    if (ticket== "VIP") 
    System.out.print("Your discount type cannot be used with your requested ticket type."); 


    System.out.println ("Thank you!"); 
    } 
+1

首先,你忘了關閉你的類的'}'。其次,比較字符串值與'String'的'equals'方法,而不是'=='。 – rgettman

+1

關於rgettman評論的第二部分,請參見[如何比較Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java?rq=1 ) –

回答

2

你錯過了結束花括號爲您Question2類:

import java.util.Scanner; 

public class Question2 { 
    public static void main(String args[]) { 
    Scanner userinput = new Scanner(System.in); 

    String ticket ; 
    System.out.print(" Type of Ticket: "); 
    ticket = userinput.next(); 

    String discount; 
    System.out.print("What sort of discount do you have?"); 
    discount = userinput.next(); 

    int standard = 40; 
    int restricted = 20; 
    int VIP = 60; 

    if ((ticket.equals("standard")) && (type.equals("student"))) { 
     double standard_student = standard * 0.95; 
    } 

    if ((ticket.equals("standard")) && (type.equals("pensioners"))) { 
     double standard_pensioners = standard * 0.90; 
    } 

    if ((ticket.equals("restricted")) && (type.equals("student"))) { 
     double restricted_students = restricted * 0.95; 
    } 


    if ((ticket.equals("restricted")) && (type.equals("pensioner"))) { 
     double restricted_pensioner = restricted * 0.90; 
    } 

    if (ticket.equals("standard")) 
     System.out.print("Your ticket costs $."); 

    if (ticket.equals("restricted")) 
     System.out.print("Your ticket costs $."); 

    if (ticket.equals("VIP")) 
     System.out.print("Your discount type cannot be used with your requested ticket type."); 

    System.out.println ("Thank you!"); 
    } 
} // <--- Add this 

這種類型的錯誤可以很容易地通過使用合適的縮進避免。事實上,大多數IDE可以自動爲您設置代碼格式。

另請參見:注意,如果要比較字符串值,則需要使用.equals()。使用==運算符只會比較引用。我已更新我的示例。

0

比較字符串的值時,請始終使用equals方法,而不是==。

例如:(type.equals(「student」))