# Am I close to having this right? If statements

• May 9th, 2014, 12:20 AM
drew_ch
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")
}
}

• May 9th, 2014, 02:40 AM
sabari_sg
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"
• May 9th, 2014, 04:42 AM
GregBrannon
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.

• May 9th, 2014, 06:57 AM
drew_ch
Re: Am I close to having this right? If statements
Code Java:

```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,);```
• May 9th, 2014, 07:27 AM
GregBrannon
Re: Am I close to having this right? If statements
Excellent. What do you get from post #2?
• May 9th, 2014, 08:45 AM
drew_ch
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

Code Java:

```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);   } }```
• May 9th, 2014, 12:43 PM
PitLv
Re: Am I close to having this right? If statements
Shorter way for you

Code :

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