我对以下作业有疑问:
创建一个程序,提示用户输入两个值:电影评级和他或她的年龄。使用决策结构,根据输入的评级和年龄确定是否允许用户在影院观看电影。最后,将这个决定的结果显示给用户。
这是我的代码,在发布此代码之前,我在 NetBeans 中执行了 Alt+Shift+F:
/*
* 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");
}
}
}
如果我将 Age 的结束括号移动到if
语句开始之前。我可以让显示器询问用户的年龄和评级,然后程序以没有结果结束。如果我把括号留在那里,程序就会完全出错。我是 Java 新手,很困惑,我一直在研究这本书和网站,但我开始感到困惑。此外,user_age
andmrating
得到一个错误,指出未使用变量。