我一直在负责为一疗程我做创造一个小型的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