我有以下的分配问题:决策组织结构的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