-1

次の割り当てに問題があります。

ユーザーに映画の評価と年齢の 2 つの値を入力するよう求めるプログラムを作成します。決定構造を使用して、入力されたレーティングと年齢に基づいて、ユーザーが映画館で映画を見ることを許可するかどうかを決定します。最後に、この決定の結果をユーザーに表示します。

これが私のコードです。これを投稿する前に、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変数mratingが使用されていないというエラーが表示されます。

4

3 に答える 3

1

Stringまず、 (例: user_age) を整数と比較することはできません。別の整数と比較する前にInteger.parseInt(String)に変換するために使用します。int

次に、 を使用==して 2 つの文字列値を比較しないでください。メソッドを使用して、equals2 つの文字列値を比較します。

3 番目に、&&ブール演算子を使用して、条件で ではなく「and」を表します+

4 番目に、各 if ステートメントの条件の直後にあるセミコロンを削除します。それ以外の場合、コンパイラは;、条件が真の場合に実行するブロックであると見なします。

他にもエラーがある可能性があります。これらは私が見つけた最初の4つです。

于 2013-03-08T00:53:06.630 に答える
0

main()すべてのコードがメソッド内にあることを確認する必要があります。これが、Alt-Ctrl-F を使用してコードをフォーマットする理由の 1 つです。コード内の中かっこを確認すると、ステートメント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 に答える