Note that there are some explanatory texts on larger screens.

plurals
  1. POif statement logic
    primarykey
    data
    text
    <p>I've been tasked with creating a small java program for a course i'm doing, and i'm having some issues with the following block of code:</p> <pre><code> 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); } } </code></pre> <p>However my output is coming out as follows:</p> <pre><code>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 </code></pre> <p>I obviously do not want repeated output, however i'm having trouble seeing where the logic in my if statements are causing it to be repeated.</p> <p>Any help would be greatly appreciated.</p> <p>Thanks all,</p> <p>~ Sam.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload