Java的决策结构

Java的决策结构,java,Java,我对以下作业有问题: 创建一个程序,提示用户输入两个值:电影分级和他或她的年龄。使用决策结构,根据输入的评级和年龄确定是否允许用户在影院观看电影。最后,向用户显示此决策的结果 这是我的代码,在发布以下内容之前,我在NetBeans中执行了Alt+Shift+F: /* * To change this template, choose Tools | Templates * and open the template in the editor. */

我对以下作业有问题:

创建一个程序,提示用户输入两个值:电影分级和他或她的年龄。使用决策结构,根据输入的评级和年龄确定是否允许用户在影院观看电影。最后,向用户显示此决策的结果

这是我的代码,在发布以下内容之前,我在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");
        } 

    }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装年限;
/**
*
*@作者杰森
*/
导入java.util.Scanner;
公课年龄{
公共静态void main(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
字符串用户年龄;
系统输出打印(“输入您的年龄:”;
user_age=user_input.next();
线切割;
System.out.print(“输入电影分级:”);
mrating=user_input.next();
}
{  
如果(用户年龄<13+m年龄=G){
System.out.print(“你正是这部电影的合适年龄”);
}
否则{
System.out.print(“你的年龄不适合这部电影”);
}
如果(用户年龄>=13+mrating=PG13){
System.out.print(“你正是这部电影的合适年龄”);
}
否则{
System.out.print(“你的年龄不适合这部电影”);
}
如果(用户年龄>=17+mrating=R){
System.out.print(“你正是这部电影的合适年龄”);
}
否则{
System.out.print(“你的年龄不适合这部电影”);
} 
}
}

如果我在
If
语句开始之前将我的结束括号移动到。我可以让显示器询问用户的年龄和等级,然后程序结束,没有结果。如果我把括号留在那里,程序就会完全出错。我是Java新手,我很困惑,我已经花了好几个小时的时间在这本书和网站上,但我开始感到困惑。另外,
user\u age
mrating
收到一个错误,表示未使用变量。

由于这是一个赋值,我无法完全帮助您……您现在必须确保程序的逻辑正确性

我已经解决了所有编译错误…很多语法问题。。。请看一些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");
        }

    }
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装年限;
/**
*
*@作者杰森
*/
导入java.util.Scanner;
公课年龄
{
公共静态void main(字符串[]args)
{
扫描仪用户输入=新扫描仪(System.in);
系统输出打印(“输入您的年龄:”;
int user_age=Integer.parseInt(user_input.next());
线切割;
System.out.print(“输入电影分级:”);
mrating=user_input.next();
如果(用户年龄<13岁和年龄=“G”)
{
System.out.print(“你正是这部电影的合适年龄”);
}
其他的
{
System.out.print(“你的年龄不适合这部电影”);
}
如果(用户年龄>=13&&M年龄==“PG13”)
{
System.out.print(“你正是这部电影的合适年龄”);
}
其他的
{
System.out.print(“你的年龄不适合这部电影”);
}
如果(用户年龄>=17&&M年龄==“R”)
{
System.out.print(“你正是这部电影的合适年龄”);
}
其他的
{
System.out.print(“你的年龄不适合这部电影”);
}
}
}

首先,不能将
字符串
(例如
用户年龄
)与整数进行比较。在与另一个整数进行比较之前,使用
Integer.parseInt(String)
转换为
int

其次,不应使用
=
来比较两个字符串值。使用
equals
方法比较两个字符串值

第三,在条件中使用
&&
布尔运算符表示“and”,而不是
+

第四,删除紧跟在每个if语句的条件后面的分号。否则,编译器将认为
是条件为真时要执行的块


可能还有其他错误;这是我找到的第一个4个。

您需要确保所有代码都在
main()
方法中。这是使用Alt-Ctrl-F格式化代码的原因之一。如果检查代码中的大括号,您将看到
main()
If
语句之前结束。您可以通过在
if
语句之前删除右大括号和右大括号来解决此问题


代码中可能还有其他问题。如果您需要帮助修复这些错误,请发布完整的编译器错误,以便我们能够帮助您。

您的类中有一块代码没有引用未声明的变量。在该块中,if语句的倍数具有悬空分号-例如(
if(user_age<13+mrating=g);{
)。这是您的真实代码吗?请注意,第一个右大括号缩进以与
main()对齐
方法。这意味着
if
语句不在该方法中。请删除该大括号和以下大括号,然后再次执行Alt-Ctrl-F。@如果“不引用尚未声明的变量”,则没有问题.@Code Guru-当然有,if语句的整个块完全在声明用户年龄和其他变量的主方法之外。@因此OP引用的是未声明的变量,这肯定是一个问题并导致编译器错误。但是,不引用变量(d