gpt4 book ai didi

java - 为什么我的方法看不到传递给它们的参数?

转载 作者:行者123 更新时间:2023-11-29 05:53:40 25 4
gpt4 key购买 nike

我一直在做 Java 作业。这是我的说明:

Interface Programming Assignment

Create an interface named ISum. The interface should define 2 methods. The first method should take two integers as arguments and return their sum. The second method should take 2 strings as arguments and return their concatenation. The second method should be an overloaded version of the first one.

Define another interface named IAverage. The interface should define one method that takes 2 integer arguments and returns the average.

Create a class named Calculator that implements both the interfaces defined above. Your class MUST implement exception handling. Create a driver class that allows a user to call each of the methods in your class.

我目前的工作:

主要类:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Calculator calculatorObject = new Calculator();
Scanner scannerObject = new Scanner(System.in);

System.out.println("Enter 1 for integer or 2 for string");
int test1 = scannerObject.nextInt();

switch (test1){
case 1:
System.out.println("Enter 1st number");
int int1 = scannerObject.nextInt();

System.out.println("Enter 2nd number");
int int2 = scannerObject.nextInt();

System.out.println("Enter 1 for sum or 2 for average");
int test2 = scannerObject.nextInt();

switch (test2){
case 1:
int sum = calculatorObject.intSum(int1,int2);
System.out.println("The sum is " + sum);
break;

case 2:
int avg = calculatorObject.intAvg(int1,int2);
System.out.println("The avg is " + avg);
break;

default:
System.out.println("You entered an invalid option");
break;
}

break;

case 2:
System.out.println("Enter 1st string");
String string1 = scannerObject.nextLine();

System.out.println("Enter 2nd number");
String string2 = scannerObject.nextLine();

String stringConcat = calculatorObject.stringSum(string1,string2);
System.out.println("The sum is " + stringConcat);
break;

default:
System.out.println("You entered an invalid option");
break;
}
}
}

ISum 接口(interface):

public interface ISum {

public void intSum();
public void intAvg();

}

IAverage 接口(interface):

public interface IAverage {

public void intAvg();

}

计算类:

abstract class Calculator implements IAverage, ISum { 

public int intSum (int1,int2){
int int1;
int int2;
int sum = int1 + int2;
return sum;
}

public String stringSum (string1,string2){
String string1;
String string2;
String stringConcat = string1.concat(string2);
return stringConcat;
}

public int intAvg(int1,int2){
int int1;
int int2;
int avg = (int1 + int2)/2;
return avg;
}

}

最佳答案

乍一看,您的方法似乎看不到参数,因为 ISum 中的方法没有参数。

试试这个,你应该上路了:

public interface ISum {

public int intSum(int first, int second);
public int intAvg(String first, String second);

}

关于java - 为什么我的方法看不到传递给它们的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13004130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com