我是JAVA新手。我想創建一個類並在其中寫入一個函數。然後我想在類main
中使用該功能。Java:在主類中添加類函數
import java.util.Scanner;
public class multi_fun {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a, b, c;
System.out.println("Enter 1st number: ");
a = scan.nextInt();
System.out.println("Enter 2nd number: ");
b = scan.nextInt();
Addition obj = new Addition();
c = obj.add(a,b);
System.out.println("The sum is "+c);
scan.close();
}
}
class Addition{
public int add (int a, int b)
{
return(a+b);
}
}
我收到一條錯誤消息,類型Addition已經定義。 –
你正在做一個好的想法,你的代碼工作正常,那麼你的問題是什麼? –
添加您的錯誤消息,也有一些說明 – Blasanka