Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

Ok im having a problem trying to use a for loop in the static add method to obtain the sum of the even numbers between two end points, and return the sum to the main method

OpenStudy (anonymous):

my code is: import java.util.Scanner; public class Calculate { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int num1, num2; System.out.print("Enter two integers: "); num1 = keyboard.nextInt(); num2 = keyboard.nextInt(); String input; System.out.println("Enter a string: "); input = keyboard.next(); if(input.equals("add")) { while(num1%2==0 && num1<num2) { int sum; sum = add(num1, num2); System.out.print("The sum of two integers is: " + sum); System.exit(0); } } else if(input.equals("multiply")) { int product; product = multiply(num1, num2); System.out.print("The product of two integers is: " + product); } else { System.out.println("Error, invalid input!"); } } public static int add(int number1, int number2) { int result; result = number1 + number2; return result; } public static int multiply(int number1, int number2) { int result; result = number1 * number2; return result; } }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!