2013-03-08 53 views
-1

我有以下的分配問題:決策組織結構的Java

創建一個程序,提示用戶輸入兩個值:電影分級和他或她的年齡。使用決策結構,根據輸入的評分和年齡確定是否允許用戶在影院中觀看電影。最後,將這個決定的結果顯示給用戶。

這裏是我的代碼,我做了鍵Alt + Shift + F在NetBeans我張貼在此之前:

/* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package age; 

    /** 
    * 
    * @author Jason 
    */ 
    import java.util.Scanner; 

    public class Age { 

     public static void main(String[] args) { 

      Scanner user_input = new Scanner(System.in); 


      String user_age; 
      System.out.print("Enter your age: "); 
      user_age = user_input.next(); 

      String mrating; 
      System.out.print("Enter the movie rating: "); 
      mrating = user_input.next(); 

     } 

    { 
     if (user_age < 13 + mrating = G);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 13 + mrating = PG13);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 17 + mrating = R);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

    } 
} 

如果我提出我的尾架的年齡到if語句開始之前。我可以讓顯示器詢問用戶的年齡和評分,然後程序結束,沒有結果。如果我將支架留在那裏,程序完全失敗。我是Java新手,感到非常困惑,我一直在爲這本書和網站上的小時工作,但我開始感到困惑。此外,user_agemrating出現錯誤,說不使用變量。

+0

您的類中有一段代碼沒有引用尚未聲明的變量。在這個模塊中if語句的多個部分有懸掛分號 - 例如('if(user_age <13 + mrating = G); {')。這是你的真實代碼嗎? – Perception 2013-03-08 00:52:22

+0

請注意,第一個右大括號是縮進以與您的'main()'方法對齊。這意味着'if'語句不在該方法中。刪除該大括號和下面的大括號,然後再次執行Alt-Ctrl-F。 – 2013-03-08 00:53:44

+0

@Perception「* not *引用具有* not *聲明的變量」沒有問題。 – 2013-03-08 01:00:16

回答

-1

由於這是任務,我也不會完全的幫助....你必須確保現在你的程序的邏輯正確性...

我已經解決了所有編譯錯誤......很多語法問題..請通過一些初學java的書。

下面

將讓你開始...

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package age; 

/** 
* 
* @author Jason 
*/ 
import java.util.Scanner; 

public class Age 
{ 

    public static void main(String[] args) 
    { 

     Scanner user_input = new Scanner(System.in); 

     System.out.print("Enter your age: "); 
     int user_age = Integer.parseInt(user_input.next()); 

     String mrating; 
     System.out.print("Enter the movie rating: "); 
     mrating = user_input.next(); 

     if (user_age < 13 && mrating == "G") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 13 && mrating == "PG13") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 17 && mrating == "R") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

    } 
} 
+1

解釋OP可以做的事情比僅僅發佈代碼更有價值解釋,特別是當他不明白髮生了什麼事情時。 – adchilds 2013-03-08 00:54:35

+0

我同意你@adchilds ...只是說,有太多的語法錯誤..在語言中指出很多努力....可能他會理解一些,而不是一些...所以最好的方式,我雖然會提供語法正確的代碼,以便他可以比較並知道錯在什麼地方...... – Amit 2013-03-08 00:56:44

1

首先,你不能比較一個String(例如user_age)爲整數。在與另一個整數比較之前,使用Integer.parseInt(String)轉換爲int

其次,您不應該使用==來比較兩個字符串值。使用equals方法來比較兩個字符串值。

第三,使用&&布爾運算符來表示「and」而不是+

第四,刪除緊跟在每條if語句的條件之後的分號。否則,如果條件爲真,編譯器會認爲;是要執行的塊。

可能存在其他錯誤;這是我發現的第4個。

0

您需要確保您的所有代碼都位於main()方法內。這是使用Alt-Ctrl-F格式化代碼的原因之一。如果您在代碼中檢查大括號,則會看到main()if語句之前結束。您可以通過在您的if聲明之前刪除關閉和開啓括號來解決此問題。

您的代碼中可能還存在其他問題。如果您需要修復它們的幫助,請發佈完整的編譯器錯誤,以便我們可以爲您提供幫助。