1

使用している Do/While ループに問題があります。ユーザーが入力する文字または文字列に関係なく、while 条件が満たされない場合でも、if ブロックが表示されます。

より具体的には、このコードは条件 (スコアを計算しますか?) を与え、ユーザーの応答に基づいて if ブロック (user = y ---> What is your name など) を続行することを意図しています。 which ステートメントが満たされない場合は、スキップします (ユーザー = n ----> プログラムが終了します)。

私が使用したコードは以下のとおりです。

このコードは完成しておらず、表示されている一部の変数はまだ使用されていないことに注意してください。

import java.util.Scanner;

public class Test1 {

    public static void main (String [] args) {





        //Declares letters to allow repetition of program
        final String START= "y";

        //Declerations of variables

        //Declares space for user's name
        String Name;

        //Declares space for user's year of birth
        int Year;

        //Declares space for user's double value, day and month of birth entered
        //by user
        double birthDM;

        //Declares space for int value day of birth, calculated from double value
        int day;

        //Declares space for int value month of birth, calculated from double value
        int month;

        //Declares space for int of calculated score
        int Score = 0;

        //Declares space for the string value of test (check wording)
        String confirmation;

        //Declares space for total number of scores calculated
        int Total;

        //Initilisaton of Scanner object
        Scanner sc;

        //Declares the space for the string that will carry the initilising letter 
        String userStart;

        //Declares space for the char that will activate program
        char yesChar;

        // Program Start



        //User input to begin loop or not (test)
        System.out.println(
                "The following program will calculate an score from your name and birthday.");
        System.out.println(" ");

        do {
            System.out.println("Would you like to calculate and score?");
            sc = new Scanner(System.in);
            userStart=sc.next();
            if (uProgStart.equalsIgnoreCase(PROGSTART));
            {
                System.out.println("What is your name?");
                userName=sc.next();
                System.out.println("What is the year of your birth?");
                birthYear = sc.nextInt();
            }
        } while(!uProgStart.equalsIgnoreCase(PROGSTART));

        System.out.println("Program End");
    }
}

ありとあらゆる助けをいただければ幸いです。

4

4 に答える 4

2

ここでセミコロンを削除します。

if (uProgStart.equalsIgnoreCase(PROGSTART));

これで問題は解決します。

于 2013-04-02T11:04:18.103 に答える
1

このような ;afterを指定しました。これは、null ステートメントであることを意味します。ifif(....);;

ブロック{ ... }ifにします。

したがって、状態を確認した後、何も起こりません。

  • true の場合、null ステートメントが実行され;、プログラムはブロックに移動します。
  • false の場合はelse、再びそうではなく、プログラムはブロックされます。

削除する必要があり;ます。

于 2013-04-02T11:09:39.453 に答える
1

;から 外すだけif (uProgStart.equalsIgnoreCase(PROGSTART))

このような :

if (uProgStart.equalsIgnoreCase(PROGSTART))
{
    System.out.println("What is your name?");
    userName=sc.next();

    System.out.println("What is the year of your birth?");
    birthYear = sc.nextInt();
}
else
    System.exit(0);
于 2013-04-02T11:03:15.283 に答える
0

同じ条件を 2 回チェックする代わりに、ifからものを抽出することができます。while;

do {
    System.out.println("Would you like to calculate and Ess-score?");
    sc = new Scanner(System.in);
    uProgStart=sc.next();
} while(!uProgStart.equalsIgnoreCase(PROGSTART))

System.out.println("What is your name?");
userName=sc.next();
System.out.println("What is the year of your birth?");
birthYear = sc.nextInt();
于 2013-04-02T11:05:47.657 に答える