我在静态方法中无法正确获取 for 循环,因此我可以获得两个端点之间偶数的总和并将总和返回给 main 方法。怎么做呢?
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)
{
for(add = 0, add > 0, ++add)
{
int result;
result = number1 + number2;
return result;
}
}
public static int multiply(int number1, int number2)
{
int result;
result = number1 * number2;
return result;
}
}