2013-04-04 178 views
1

我一直在負責爲一療程我做創造一個小型的Java程序,以及我在與下面的代碼塊中的一些問題:if語句邏輯

 System.out.println("Please enter your first name:"); 
    userName = sc.nextLine(); 
    System.out.println("What year were you born in?"); 
    birthYear = sc.nextInt(); 
    System.out.println("In the 'dd.mm' format, what are the day and month of your birth?"); 
    dayMonth = sc.nextDouble(); 
    //Cast the user input of type double to an int for the day of Birth 
    dayOfBirth = dayMonth.intValue(); 

    //Cast the double value for month and cast it to an int 
    dayMonth = dayMonth - dayOfBirth; 
    dayMonth = dayMonth * 100; 
    dayMonth = Math.rint(dayMonth); 
    monthOfBirth = dayMonth.intValue(); 
if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941) 
{ 
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11) 
    { 
     System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear); 
    } 
     else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11) 
     { 
      System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear); 
     } 
} 
if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941) 
{ 
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11) 
    { 
     System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear); 
    } 
     else if(monthOfBirth != 9 || monthOfBirth != 10 || monthOfBirth != 11) 
     { 
      System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear); 
     } 
} 

但是我的輸出是現身如下:

Please enter your first name: 

Samuel 

What year were you born in? 

1977 

In the 'dd.mm' format, what are the day and month of your birth? 

09.09 

Samuel was born during Spring on September 9 in Snake year 1977 

Samuel was born during Spring on September 9 

我顯然不希望重複輸出,但是我無法看到凡在我if語句導致它被重複的邏輯。

任何幫助將不勝感激。

謝謝大家,

〜山姆。

+0

Hi @Sam。你和我的兄弟有完全一樣的名字!不要把這當成侮辱,而是建設性的批評。你需要更好地格式化你的代碼。看看一個真正的風格:http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS。另外如果你使用eclipse,你可以使用'CTRL-SHIFT-F'來自動格式化。它可以節省很多時間!很多編輯可以爲你自動格式化。看看它,兄弟! ;) – Jess 2013-04-04 14:00:12

回答

1

而不是單獨使用2 if,使它們分別爲if-elseif-else if

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){ 
    .... 
} 
else if(birthYear != 2013 || birthYear != 2001 ||...){ 
.... 
} 

因爲,如果你給一個if-if,無論是if語句將被執行,而如果你給一個if-else if,第二else if將僅在第一if語句失敗執行。

+0

第二個'sysout'是第二個'if'被執行的結果,即使執行了'if'。因此,答案。 OP希望避免打印第二個「if」。 – SudoRahul 2013-04-04 10:58:45

+1

根據我的回答,'else if'可以替換爲'else'。如果'birthYear'不是龍的一年,那麼它不是。無需再次檢查所有年份。不要重複自己。 – AlexanderBrevig 2013-04-04 11:01:19

+0

@AlexanderBrevig - 我已經在我的答案的第一行中提到過了。但是你不能認爲OP在第二個'if'停下來。如果他以後有'龍年'和'不是龍年',該怎麼辦? – SudoRahul 2013-04-04 11:03:51

0

使用else語句如有可能,所以如果你測試2013,2001年... 剛過,作出else語句

0

此時應更換第二,如果這裏的一切!=用一個簡單的「其他」因爲你已經檢查過這些陳述是否屬實。因此,簡單地寫else就會在邏輯上相等並且更具可讀性。

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941){ 
    .... 
} 
else { //not snake year 
    .... 
} 
0

問題是與

if(birthYear != 2013 || birthYear != 2001 || birthYear != 1989 || birthYear != 1977 || birthYear != 1965 || birthYear != 1953 || birthYear != 1941 

如果你仔細看,我們正在確保表達總是導致嘗試。只有一個人可以一次最多假,其他人都會是真的,因此最終總是如此(困惑?)。說出生年份是2013年,這使得第一個條件(生日年!= 2013)爲假,但所有其他條件都是真實的。以任何其他的例子,你會看到我想說的。

其實一個小修改會爲您修復代碼。你已經檢查過if (birthyear is in given years),沒有任何條件的一個簡單的東西會爲你工作。

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941) 
{ 
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11) 
    { 
     System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear); 
    } 
     else// NO CHECK REQUIRED NOW 
     { 
      System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " in Snake year " + birthYear); 
     } 
} 
//SEE THIS 
else{ 
    if(monthOfBirth == 9 || monthOfBirth == 10 || monthOfBirth == 11) 
    { 
     System.out.println(userName + " was born during Spring on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear); 
    } 
//AND 
     else 
     { 
      System.out.println(userName + " was born on " + monthOfBirthName + " " + dayOfBirth + " " + birthYear); 
     } 
} 
0

如果一部分是錯誤的。第二:

if(birthYear != 2013 || birthYear != 2001 || ....) 

,因爲它常是真實的。 只需檢查一個視圖示例: 如果您有2012年,它不2013年,所以你的表達式成爲現實。 如果你採取2013年,那麼第一部分將變成錯誤的,但是當你評估2013!= 2001時,它會再次變爲真實。

你可能希望有什麼是

if(birthYear == 2013 || birthYear == 2001 || birthYear == 1989 || birthYear == 1977 || birthYear == 1965 || birthYear == 1953 || birthYear == 1941) 
{ 
..... 
} 
else { 
..... 
} 

順便說一句:如果你想否定式(X = = ||一個== X ||乙== X C)則邏輯否定是(x!= A & & x!= B & & x!= C)。

1

您可以選擇以下解決方案之一:使用& &操盤手)

1 ||在第二個if語句

2)如果(){}其他{} //卡邁勒的解決方案

0

答案就在你的條件。

承擔dob = 1

if(dob == 1 || dob == 2 || dob == 3 ...) 

將評估爲true,因爲DOB == 1

if (dob != 1 || dob != 2 || dob != 3 ...) 

也將評估爲true,因爲DOB!= 2

要使用什麼是AND/&&而不是OR/||

1

更好地使用集合而不是多個if(... == .. || ... = = || ...)

像這樣:

List<String> wrongYears = Arrays.asList("2013", "2001", "1989", "1977", ...); 

,並檢查:

if(wrongYears.contains(birthYear) 
{ 
... 
} 

和幾個月的數字相同

你的邏輯錯誤會很明顯