我一直在負責爲一療程我做創造一個小型的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語句導致它被重複的邏輯。
任何幫助將不勝感激。
謝謝大家,
〜山姆。
Hi @Sam。你和我的兄弟有完全一樣的名字!不要把這當成侮辱,而是建設性的批評。你需要更好地格式化你的代碼。看看一個真正的風格:http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS。另外如果你使用eclipse,你可以使用'CTRL-SHIFT-F'來自動格式化。它可以節省很多時間!很多編輯可以爲你自動格式化。看看它,兄弟! ;) – Jess 2013-04-04 14:00:12