2013-02-22 61 views
0

我寫的程序是確定今年是否是閏年。這是一項任務,所以我需要使用我在程序中編寫的四種方法。該程序編譯並運行,它要求在適當的地方進行用戶輸入,但不會將輸入輸入到程序中。也就是說,無論輸入什麼內容,今年都是閏年。我非常困惑,因爲這個程序看起來與我們給出的例子相匹配。方法不會拿起用戶輸入

import java.util.Scanner; 

public class LeapYear { 
    public static void main(String[] args) { 
     boolean repeat; 
     String computeanother, yes="yes"; 
     Scanner kb=new Scanner(System.in); 
     int year = -1; 
     boolean leap; 

     do 
     { 
      displayInstructions(); 
      getYear(year); 
      leap = isLeap(year); 
      displayResults(year, leap); 
      System.out.println("Would you like to compute another year?"); 
      computeanother = kb.nextLine(); 

      if(computeanother.equals(yes)) 
       repeat=true; 
      else 
       repeat=false; 
     } while(repeat=true); 
    } 

    public static void displayInstructions() 
    { 
     System.out.println("This program is designed to predict whether or not a year is a leap year."); 
     System.out.println("When prompted please enter a positive number for the year."); 
     System.out.println("Once the program has run completely, it will state the year and whether it is a leap year."); 
    } 

    public static void getYear(int year) 
    { 
     Scanner kb = new Scanner(System.in); 
     do { 
      System.out.println("Please enter the year."); 
      year=kb.nextInt(); 
     } while (year < 0); 
    } 

    public static boolean isLeap(int year) 
    { 
     boolean leap; 
     if ((year%4==0 && year%100 != 0) || year%400==0){ 
      leap = true; 
      return true; 
     } else { 
      leap = false; 
      return false; 
     } 
    } 

    public static void displayResults(int year, boolean leap) 
    { 
     if (leap = true) { 
      System.out.println("The year " +year); 
      System.out.println("is a leap year."); 
     } else { 
      System.out.println("The year " +year); 
      System.out.println("is not a leap year."); 
     } 
    } 

} 

感謝大家的幫忙!編輯後的代碼如下所示:

import java.util.Scanner; 

public class LeapYear{ 
public static void main(String[] args){ 
boolean repeat; 
String computeanother, yes="yes"; 
Scanner kb=new Scanner(System.in); 
int year = -1; 
boolean leap; 
do 
{ 
displayInstructions(); 
getYear(year); 
leap = isLeap(year); 
displayResults(year, leap); 
System.out.println("Would you like to compute another year?"); 
computeanother = kb.nextLine(); 
repeat = computeanother.equals(yes); 
}while(repeat); 
} 
public static void displayInstructions() 
{ 
System.out.println("This program is designed to predict whether or not a year is a leap year."); 
    System.out.println("When prompted please enter a positive number for the year."); 
System.out.println("Once the program has run completely, it will state the year and whether it is a leap year."); 
} 

public static int getYear(int year) 
{ 
    Scanner kb = new Scanner(System.in); 
    do{ 
     System.out.println("Please enter the year."); 
     year=kb.nextInt(); 
    }while (year < 0); 
    return year; 
} 

public static boolean isLeap(int year) 
{ 
boolean leap; 
year = getYear(year); 
if ((year%4==0 && year%100 != 0) || year%400==0){ 
    leap = true; 
    return true;} 
else{ 
    leap = false; 
    return false;} 
} 

public static int displayResults(int year, boolean leap) 
{ 
year = getYear(year); 
if (leap == true){ 
    System.out.println("The year " +year); 
    System.out.println("is a leap year.");} 
else{ 
    System.out.println("The year " +year); 
    System.out.println("is not a leap year.");} 
return year; 
} 

} 
+0

看到這個:HTTP://堆棧溢出。com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx,還要注意,形式爲'if(f(y))x = true else x = false'的任何代碼都可以簡化爲x = f(y)' – 2013-02-22 16:27:38

+0

假設這是程序的最終結果,則在isLeap(int year) - 方法中實現的布爾跳躍沒有任何用處。 – 2013-02-22 17:19:43

+0

@FlorisVelleman我可以刪除布爾值,只是告訴它返回true或false? – SMoore 2013-02-22 17:24:38

回答

0

更改此:

while(repeat=true); 

while(repeat==true); 

while(repeat); 

這裏while(repeat=true);你正在分配一個值,而不是比較。 while(repeat==true);while(repeat);這將比較值。測試像這樣while(repeat);而不是明顯的while(repeat==true);總是更好。我希望它有幫助。

而你不year獲得價值爲-1,因爲,你是從這個方法返回getYear(year);而忽視價值。將其更改爲:

year = getYear(year);

這應該工作。

+0

這確實讓程序終止謝謝!但它仍然沒有收集用戶輸入或在閏年條件下測試數字。你知道這是爲什麼嗎? – SMoore 2013-02-22 16:34:30

+0

@SavannahMoore不採摘手段?你能解釋更多嗎? – 2013-02-22 16:41:39

+0

當我運行該程序時,它繼續將年份顯示爲-1的設置值,並且因爲-1不是閏年,所以它只報告年份不是閏年。 – SMoore 2013-02-22 16:45:24

1
while(repeat=true); 

在while循環應該是:

while(repeat == true); 

while(repeat); 
從這個

除了被大家所指出的,可以注意到你犯了這種錯誤兩次:

if (leap = true) { 

應該是:

if (leap == true) { 

if (leap) { 
+1

+ 1的第一個得到第二個錯誤。 – 2013-02-22 16:30:52

+0

因此,雙等於僅在if條件中是必要的嗎?當我告訴程序跳躍是真的時,我只使用一個等號,對嗎? – SMoore 2013-02-22 16:48:13

+0

==是一個比較,while =用於分配一個值。所以:int x = 5;如果(x == 12) – 2013-02-22 17:17:36

1

您也可以縮短你的代碼:

do{ 
     displayInstructions(); 
     getYear(year); 
     leap = isLeap(year); 
     displayResults(year, leap); 
     System.out.println("Would you like to compute another year?"); 
     computeanother = kb.nextLine(); 
     repeat = computeanother.equals(yes) //this line makes code shorter 
    } while(repeat); 

事實上,總是避免這樣的著名模式的冗餘代碼:

if(expression) return true; else return false; 

變成:return expression;

+0

這確實有助於縮短它。我沒有意識到這是一個選項謝謝你! – SMoore 2013-02-22 16:45:56

+0

@Savannah摩爾不客氣:) – Mik378 2013-02-22 16:46:33