Can anyone tell me why I keep getting a value of 0 returned?

public static void main(String[] args) {
		
		double getSum = 0;
		double getAverage = 0;
		 
		
		
		
		String num1 = JOptionPane.showInputDialog(null, "Please enter your first number");
		String num2 = JOptionPane.showInputDialog(null,"Please enter your second number");
		String num3 = JOptionPane.showInputDialog(null, "Please enter your third number");
		
		
		
		JOptionPane.showMessageDialog(null, "The sum is " + getSum);
		JOptionPane.showMessageDialog(null, "The average is " + getAverage);
		
		 
		 
	
		
		 
		
	
	}
	

		
		
		 
		 
	
	
	
	
		
	public static double getSum(double num1, double num2, double num3) {
		   
		 
			
		 
		  double getSum = num1 + num2 + num3;
		  return getSum;
	}
		  
	
	public static double getAverage1(double num1, double num2, double num3) {
		
		double getAverage = num1 + num2 + num3 / 3;
		return  getAverage;
		
	}

Can anyone tell me why I keep getting a value of 0 returned?

public static void main(String[] args) {
		
		double getSum = 0;
		double getAverage = 0;
		 
		
		
		
		String num1 = JOptionPane.showInputDialog(null, "Please enter your first number");
		String num2 = JOptionPane.showInputDialog(null,"Please enter your second number");
		String num3 = JOptionPane.showInputDialog(null, "Please enter your third number");
		
		
		
		JOptionPane.showMessageDialog(null, "The sum is " + getSum);
		JOptionPane.showMessageDialog(null, "The average is " + getAverage);
		
		 
		 
	
		
		 
		
	
	}
	

		
		
		 
		 
	
	
	
	
		
	public static double getSum(double num1, double num2, double num3) {
		   
		 
			
		 
		  double getSum = num1 + num2 + num3;
		  return getSum;
	}
		  
	
	public static double getAverage1(double num1, double num2, double num3) {
		
		double getAverage = num1 + num2 + num3 / 3;
		return  getAverage;
		
	}

you never ever call the methods to return the answer, you refer to the actual integer getSum which will be 0 until getSum(..) method is called and getSum is initialized to the value ie

JOptionPane.showMessageDialog(null, "The sum is " + getSum( num1,  num2, num3));

also see here:http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html and here:http://www.roseindia.net/java/beginners/ObjectClass.shtml

Edited 4 Years Ago by DavidKroukamp: n/a

This article has been dead for over six months. Start a new discussion instead.