2016-07-05 90 views
0

我开始自学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. "); 
     } 
    } 
} 

回答

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. "); 
    } 

然后catch语句结束后,我把这样的:

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

我试过这个,但它没有打印我想要的结果。重点不是在询问用户的“插入年份”之间循环“...年...”,而是在用户输入-1退出循环后打印它。 – TRMN

-1

你的条件对于闰年应该是:

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

enter image description here

here

+0

是的,但这并不能回答真正的问题。 –

1

如果你将整个的if/else节检查闰年和显示,而(真)块内的天,你应该让你在找什么:

  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. "); 
       } 
      } 
+0

这不是我正在寻找的。是的,今年是正确的,但循环不按预期工作。它应该循环“插入年份”,直到用户输入-1,然后才打印“年份...”,而不是在循环之间。 – TRMN

+0

然后使用continue语句而不是break语句。 –

+0

不过,这不会得到我以后的结果。我正在寻找的是在今年打印''的计划......''仅在用户输入-1后退出循环,而不是在“输入年份”循环之间。现在它打印-1而不是最后输入的年份。 (“在-1年有..”),而不是(“在1992年有...”) – TRMN

0

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

+0

不会停止该计划,并不打印在今年的''''部分所有? – TRMN

0

你的问题是,当你想要最后一个不是-1的用户输入时,你将年设置为-1,所以你应该设置命令给用户输入检查它是否为-1。如果它是-1中断。如果它不是-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的帖子的闰年逻辑中添加。

+0

!非常感谢,这就是我一直在寻找的东西。天哪看起来很简单,现在修复,我看着它,并明白我做错了什么:)编辑:你确定闰年吗?错误(使用代码时((%4 == 0)&&(year%100!= 0))||(年%400 == 0)时的错误(令牌“||”的语法错误,无效OnlySynchronized)当我再添加一个)到结尾||(年%400 == 0)) – TRMN

+0

我在闰年逻辑中编辑它应该现在工作原来有一个括号在错误的位置。 – gottfred