1

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

更具体地说,此代码旨在提供条件(您要计算分数吗?)并根据用户响应,继续 if 块(用户 = y ---> 你的名字等),或者,当不满足 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

只需;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 回答
1

您已经给出了这样的;after if,这 if(....);意味着;是一个空语句。

它使{ ... }if一个

所以在检查条件后没有任何反应。

  • 如果为真,则执行空语句;并且程序转到该块。
  • 如果它是假的,else那么再次没有,程序进入阻塞状态。

您将不得不删除;.

于 2013-04-02T11:09:39.453 回答
0

您可以if从 the 中提取事物,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 回答