我在解决运动方面有问题
本练习中准备了两个类: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。我需要这样的输出:
在第一个整数中键入:-3在第二个整数中键入:-1和 数字:-4
非常感谢!
答案 0 :(得分:0)
您需要像这样在类内部创建方法:
class Printer {
void Print() {
//implementation
}
}
并且您没有正确调用Sum()
方法。试试:
System.out.print("Sum of the numbers: " + Calculator.Sum(first, second));
答案 1 :(得分:-1)
您可以尝试以下方法:
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);
}
}