Java:虽然(真实)没有得到正确的打印

时间:2016-07-05 17:34:34

标签: java

我自己开始学习Java,而且我一直在做在线练习。

我无法获得正确的打印:每当我想通过输入-1退出程序时,打印-1而不是最后输入的年份。我该怎么解决这个问题,它会打印最后输入的年份而不是-1?我已经尝试了很多东西,例如arrayLists和其他循环,但我似乎无法让它工作。

感谢。

import java.util.*;

public class Miniprojekti4_3 {

    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        int command = 0;
        int year = 0;
        int days = 365;

        try {
            System.out.println("The program will ask you to input years. You can quit by typing -1 ");        

            while (true) {
                System.out.print("Insert year: ");
                year = Integer.parseInt(input.nextLine());   
                if (year == -1) {
                        break;
                } 
            }
        } catch (Exception e) {
             System.out.println("Error: program closing..");
             System.exit(1);
        }

        if (year % 4 == 0 && year % 400 == 0) {
              System.out.println("In the year " + year + " there is " + (days+1) +" days. "); 
        } else {
            // if the year is not a leap year
            System.out.println("In the year " + year + " there is " + days +" days. ");
        }
    }
}

6 个答案:

答案 0 :(得分:1)

如果您移动检查闰年的整个if / else部分并在while(true)块内显示天数,那么您应该得到您想要的内容:

            while (true)
            {     
                System.out.print("Insert year: ");
                year = Integer.parseInt(input.nextLine());   
                if (year == -1)
                {
                    break;
                }

                if (year % 4 == 0 && year % 400 == 0) // if the year is a leap year.
                {

                    System.out.println("In the year " + year + " there is " + (days+1) +" days. "); 
                }
                else // if the year is not a leap year
                {

                    System.out.println("In the year " + year + " there is " + days +" days. ");
                }   
            }

答案 1 :(得分:1)

您只需跟踪用户输入的最后一个有效年份。在这里,我只是将用户输入分配给临时变量,然后如果它不是-1,则将其分配给年变量。这样,如果用户输入-1,它就会从循环中断开,并且年份仍然具有旧值。我还改变了扫描程序使用nextInt()而不是nextLine(),因为有一些奇怪的问题正在发生。

import java.util.*;

public class Miniprojekti4_3 {

    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        int year = 0;
        int days = 365;

        try {
            System.out.println("The program will ask you to input years. You can quit by typing -1 ");

            while (true) {
                System.out.print("Insert year: ");
                int tempYear = input.nextInt();
                if (tempYear == -1) {
                    break;
                }
                year = tempYear;
            }
        } catch (Exception e) {
            System.out.println("Error: program closing..");
            System.exit(1);
        }

        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
            System.out.println("In the year " + year + " there is " + (days + 1) + " days. ");
        } else {
            // if the year is not a leap year
            System.out.println("In the year " + year + " there is " + days + " days. ");
        }
    }
}

编辑:ΦXocę웃Пepeúpa在帖子中添加闰年逻辑。

答案 2 :(得分:0)

我将此代码放在while循环中:

 if (year % 4 == 0 && year % 400 == 0) {
          System.out.println("In the year " + year + " there is " + (days+1) +" days. "); 
    } else {
        // if the year is not a leap year
        System.out.println("In the year " + year + " there is " + days +" days. ");
    }

然后在捕获声明结束后,我会说:

System.out.println("Exiting....");

答案 3 :(得分:0)

输出打印正确。但是你的循环是错误的。实际上,当你给-1时,它实际上并没有终止程序,而是它打破了循环并且仅从while循环中出来。执行catch块下方的if ... else语句并打印年份,在您的情况下,它是-1。所以在if语句中检查year == -1,键入System.exit(1)而不是break语句。

答案 4 :(得分:0)

您的问题是,当您希望最后一个用户输入不是-1时,您将年份设置为-1,因此您应该将命令设置为用户输入检查它是否为-1。如果是-1休息。如果它不是-1 set year to command。

答案 5 :(得分:-1)

闰年的条件应该是:

((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)

enter image description here

来自herehere