2016-07-05 79 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