为什么这个程序会立即循环而不是应该何时循环?

时间:2011-10-26 21:21:30

标签: java eclipse

import java.util.Scanner;
import java.util.InputMismatchException;
public class assignment2 {
    public static int t1;
    public static int t2;
    public static int x;
    public static int y1;
    public static int m1;
    public static int d1;
    public static int y2;
    public static int m2;
    public static int d2;
    public static void date1() {
        int x = 0;  // Integer for looping.
        do          // Process to follow if length == 5.
        {           // Notify program what to do while under loop limit.
            Scanner scanner  = new Scanner (System.in);
            try
            {
                System.out.println("Please enter the first date ");
                System.out.println ("Please enter the year: ");
                y1=scanner.nextInt();

                System.out.println("Please enter the month: ");
                m1=scanner.nextInt();

                System.out.println("Please enter the day: ");
                d1=scanner.nextInt();}
            catch ( InputMismatchException inputMismatchException) // Error if  wrong character is inputted.
            {
                scanner.nextLine() ;
                System.err.printf( "You must enter intergers. Please try again.\n" ); // Prompt user to enter integers.
            } // Set loop to three attempts.
        }
        while (x < 3) ;

        int j = 693502;
        if (t1 > j) {
            if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) {
                t1 = ((365*y1)+d1+31);
            }
            else
                if (m1==2) {
                    t1 = ((365*y1)+d1+28);
                }
                else
                    if (m1==4 + 6 + 9 + 11); {
                        t1 = ((365*y1)+d1+30);
                    }
            x = x + 1; // Set loop to three attempts
            while (x < 3) ;
        }
        else {
            System.err.printf ("Error. Please enter a date after Jan 1st 1900.\n") ;
        }
    }

    public static void date2() {
        int x = 0; // Integer for looping
        do         // Process to follow if length == 5
        {
            Scanner scanner  = new Scanner (System.in);
            try
            {
                System.out.println("Please enter the first date ");
                System.out.println ("Please enter the year: ");
                y2=scanner.nextInt();

                System.out.println("Please enter the month: ");
                m2=scanner.nextInt();

                System.out.println("Please enter the day: ");
                d2=scanner.nextInt();}
            catch ( InputMismatchException inputMismatchException) // Error if wrong character is inputted.
            {
                scanner.nextLine() ;
                System.err.printf( "You must enter intergers. Please try again. " ); // Prompt user to enter integers.
            }
            x = x + 1; // Set loop to three attempts
        }
        while (x < 3) ;
        int j = 693502;
        if (t1 > j) {
            if (m1==1 + 3 + 5 + 7 + 8 + 10 + 12) {
                t1 = ((365*y1)+d1+31);
            }
            else
                if (m1==2) {
                    t1 = ((365*y1)+d1+28);
                }
                else
                    if (m1==4 + 6 + 9 + 11); {
                        t1 = ((365*y1)+d1+30);
                    }
            x = x + 1; // Set loop to three attempts.
            while (x < 3) ;
        }
        else {
            System.err.printf ("Error. Please enter a date after Jan 1st 1900. ");
        }
    }

    public static void finaldate1() {
        x = Math.abs(t1-t2);
    }

    public static void main(String[] args) {
        date1();
        date2();
        finaldate1();
        System.out.println("The difference between the two dates is: " + x + " days.");
    }
}

我的循环没有正确响应的原因是什么?如果输入非整数或输入的总天数小于693502(1990年1月1日),我怎么能设置一个正确的错误捕获和循环。

3 个答案:

答案 0 :(得分:1)

您应该删除while行末尾的分号,并将要包含在括号中的循环中的区域附近,即{和}

答案 1 :(得分:0)

您的循环条件错误。例如,在date1输入循环中,您有while (x < 3);x永远不会在该循环内修改。

答案 2 :(得分:0)

首先,查看错误并将其编译。

  1. 一半的while循环是独立的,没有做任何事情。
  2. 所有while循环都引用了一个未设置的x,
  3. 此外,您正在使用局部变量覆盖类级别x变量,该变量当前不是错误,但很可能导致一个。
  4. 一般调试建议,将问题分解成小块,并确认小块工作。在开始下一篇文章之前保存工作(理想的版本控制),这样你就可以一直回到工作代码。