我有以下的分配問題:決策組織結構的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_age
和mrating
出現錯誤,說不使用變量。
您的類中有一段代碼沒有引用尚未聲明的變量。在這個模塊中if語句的多個部分有懸掛分號 - 例如('if(user_age <13 + mrating = G); {')。這是你的真實代碼嗎? – Perception 2013-03-08 00:52:22
請注意,第一個右大括號是縮進以與您的'main()'方法對齊。這意味着'if'語句不在該方法中。刪除該大括號和下面的大括號,然後再次執行Alt-Ctrl-F。 – 2013-03-08 00:53:44
@Perception「* not *引用具有* not *聲明的變量」沒有問題。 – 2013-03-08 01:00:16