跳过While条件的Java Do / While循环

时间:2013-04-02 10:58:44

标签: java loops while-loop do-while

我遇到麻烦了 我正在使用的Do / While循环。无论用户输入什么字符或字符串,都会显示if块,即使不满足while条件也是如此。

更具体地说,这段代码意味着给出条件(你想计算得分吗?)并根据用户的反应,继续if块(user = y --- >你的名字等等)或者,当不符合哪个陈述时,跳过它(用户= 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 个答案:

答案 0 :(得分:2)

在此处删除分号:

if (uProgStart.equalsIgnoreCase(PROGSTART));

这将解决问题。

答案 1 :(得分: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);

答案 2 :(得分:1)

您在;之后给出了if if(....);表示;是一个空语句。

它会{ ... } if之后阻止

因此在检查条件后没有任何反应。

  • 如果为真,则执行空语句;,程序进入块。
  • 如果为假,则没有else,所以程序再次阻止。

您必须 删除 ;

答案 3 :(得分:0)

您可以从if中提取while内容,而不是两次检查相同的条件 - 并在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();