我在解决练习时遇到了问题
在本练习中准备了两个类: ObjectFunctionality和
。您的任务是创建Printer类,它向用户请求两个整数,调用Calculator类的Sum()方法,将输入的整数传递给该方法。方法Sum()计算这些数字的和并返回结果。最后,Sum()在屏幕上打印出总和。
import java.util.Scanner;
public class ObjectFunctionality {
public static void main(String args[]) {
Printer thing = new Printer();
thing.Print();
}
}
// Write the missing class here
// Class is written in the text box below.
class Calculator {
static int Sum(int first, int second) {
int sum = first + second;
return sum;
}
}
下面是我的代码:
class Printer {
Scanner input = new Scanner(System.in);
System.out.print("Type in the first integer: ");
int first = reader.nextInt();
System.out.print("Type in the second integer: ");
int second = reader.nextInt();
System.out.print("Sum of the numbers: " + sum.sum);
}
我知道有些事情很不对劲,但我解决不了这个问题。
PS。我需要这样的输出:
输入第一个整数:输入第二个整数:-1数字的总和:-4
非常感谢!
发布于 2018-09-17 03:40:20
您可以尝试这样做:
public class Printer {
void Print(){
Scanner input = new Scanner(System.in);
System.out.print("Type in the first integer: ");
int first = input.nextInt();
System.out.print("Type in the second integer: ");
int second = input.nextInt();
System.out.print("Sum of the numbers: ");
int sum = Calculator.Sum(first, second);
System.out.println(sum);
}
}
https://stackoverflow.com/questions/52357355
复制相似问题