-1

我对以下作业有疑问:

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

这是我的代码,在发布此代码之前,我在 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_ageandmrating得到一个错误,指出未使用变量。

4

3 回答 3

1

首先,您不能将 a String(eg user_age) 与整数进行比较。用于在与另一个整数比较之前Integer.parseInt(String)转换为一个。int

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

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

第四,删除每个 if 语句的条件后面的分号。;否则,如果条件为真,编译器会认为这是要执行的块。

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

于 2013-03-08T00:53:06.630 回答
0

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

您的代码中可能还存在其他问题。如果您需要帮助修复它们,请发布完整的编译器错误,以便我们为您提供帮助。

于 2013-03-08T00:59:23.600 回答
-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");
        }

    }
}
于 2013-03-08T00:52:44.030 回答