0

以下のコードを使用すると、18 より大きい数値を入力しても、この結果が得られ
ます。 run: How old are you? 21 まだ成年に達していません!BUILD SUCCESSFUL (合計時間: 3 秒)

私はJavaが初めてで、自己学習しようとしている人は誰でも助けてくれますか?

import java.util.Scanner;
public class Chapter8 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner reader = new Scanner (System.in);
        // TODO code application logic here

        //Excercise 15
        System.out.print("How old are you? ");
        int x = Integer.parseInt(reader.nextLine());
        if (x > 18){
            System.out.println("You have not reached the age of Majority yet!");
        }else {
            System.out.println("You have reached the age of Majority!");
        }
4

3 に答える 3

5

あなたのコードは現在、「年齢が 18 歳以上 (つまり 19 歳以上) の場合、年齢ではありません」と表示されます。

私はあなたが意味したと思いますx < 18

于 2013-11-08T15:49:01.677 に答える
0

逆にする必要があります:

if (x < 18){
       System.out.println("You have not reached the age of Majority yet!");
}else {
        System.out.println("You have reached the age of Majority!");
}
于 2013-11-08T15:51:38.537 に答える