Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Am I close to having this right? If statements

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Am I close to having this right? If statements

    write a program that will ask the user to enter five numbers.using If statements if each number entered in greater than zero



    import.java.util.Scanner;

    public class Java3
    {
    public static void main(String[] args)
    {
    function addNumbers(n1, n2, n3, n4, n5){
    var finalNumber = 0;
    if(n1 > 0);
    if(n2 > 0);
    if(n3 > 0);
    if(n4 > 0);
    if(n5 > 0); {
    finalNumber = n1 + n2 + n3 + n4 + n5
    console.log(finalNumber);
    } else {
    console.log("Please enter a number greater than 0")
    }
    }

    addNumbers(1, 2, 3, 4, 5,);


  2. #2
    Junior Member
    Join Date
    May 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I close to having this right? If statements

    if you have semicolon immediately after if condition, it means "do nothing". It must have thrown error at the line contains "else"

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Am I close to having this right? If statements

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    Please post your code correctly per the above link.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I close to having this right? If statements

    import.java.util.Scanner;
     
    public class java3
    {
    	public static void main(String[] args)
    	{
    		  function addNumbers(n1, n2, n3, n4, n5){
    		     var finalNumber = 0;
    		     if(n1 > 0);
    		     if(n2 > 0);
     		     if(n4 > 0);
    		     if(n5 > 0); {
    		       finalNumber = n1 + n2 + n3 + n4 + n5 ;
     
    		      console.log(finalNumber);
    		     } else {
    		      console.log("Please enter a number greater than 0");
    		     }
    		   }
     
    addNumbers(1, 2, 3, 4, 5,);
    Last edited by drew_ch; May 9th, 2014 at 07:04 AM.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: Am I close to having this right? If statements

    Excellent. What do you get from post #2?

  6. #6
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I close to having this right? If statements

    I got it figured out thanks for the advice I just made it more complicated then it really was

    import java.util.Scanner;
     
    public class Java3
    {
    	public static void main(String[] args)
    	{
     
    		int n1, n2, n3, n4, n5, total = 0;
    				Scanner scan = new Scanner(System.in);
     
    				System.out.println("Please enter a number");
    				n1 = scan.nextInt();
     
    				System.out.println("Please enter a number");
    				n2 = scan.nextInt();
     
    				System.out.println("Please enter a number");
    				n3 = scan.nextInt();
     
    				System.out.println("Please enter a number");
    				n4 = scan.nextInt();
     
    				System.out.println("Please enter a number");
    				n5 = scan.nextInt();
     
     
    		     if(n1 > 0)
    		     System.out.println("The number entered is Valid");
    			 else
    	    	 System.out.println("The number entered is invalid");
    		     if(n2 > 0)
    		     System.out.println("The number entered is valid");
    			 else
    	         System.out.println("The number entered is invalid");
    		     if(n3 > 0)
    		     System.out.println("The number entered is valid");
    			 else
    	         System.out.println("The number entered is invalid");
     		     if(n4 > 0)
                 System.out.println("The number entered is valid");
    			 else
    	         System.out.println("The number entered is invalid");
    		     if(n5 > 0)
                 System.out.println("The number entered is valid");
    			 else
    	         System.out.println("The number entered is invalid");
     
    	         total = n1 + n2 + n3 + n4 + n5;
    	         System.out.println("The sum is " + total);
     
    		 }
    	 }

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Am I close to having this right? If statements

    Shorter way for you

    if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0 && n5 > 0)
      do your tasks if those input numbers are valid

Similar Threads

  1. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  2. close class
    By nillson in forum Java Theory & Questions
    Replies: 1
    Last Post: September 28th, 2013, 01:47 PM
  3. Why can't I close my JFrame ?
    By bartolio in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 20th, 2013, 10:23 AM
  4. Close jframe
    By takamine in forum AWT / Java Swing
    Replies: 4
    Last Post: February 3rd, 2013, 01:16 PM
  5. Is this correct.Am I close? Help please
    By eagle09 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 26th, 2011, 07:26 AM