Method and return


Submit solution

Points: 50 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

題目說明

要求使用者輸入兩個數值,並透過方法(Method)計算平方值或立方值。
兩個方法: squarecube,需在 main 中呼叫它們以傳遞/回傳值。

  1. square 方法會接收int資料型態的參數,並回傳該數的平方值(nxn)。
  2. cube 方法會接收int資料型態的參數,並回傳該數的立方值(nxnxn)。
請務必依照下方程式碼模板作答,不得修改主程式的結構,否則不予計分!

程式碼模板 (請務必依照此模板撰寫)

import java.util.Scanner;

public class MathFunctions {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num1=input.nextInt();//1:平方值,2:立方值
        int num2=input.nextInt();//計算平方或立方的值

        //判斷要呼叫square或cube方法,並根據回傳值印出結果。

    }

    // 請實作 square 方法,使用return使其可以回傳平方值

    // 請實作 cube 方法,使用return使其可以回傳立方值

}

輸入

輸入兩個數值。

  • 第一個數值:
    輸入1,選擇平方。
    輸入2,選擇立方。
  • 第二個數值:
    用來計算平方或立方值。

輸出

main 方法會接收squarecube的回傳值,並輸出平方或立方值。

範例輸入 #1

1 4

說明:1是計算平方值,4x4。

範例輸出 #1

16

範例輸入 #2

2 3

說明:2是計算立方值,3x3x3。

範例輸出 #2

27

Comments

There are no comments at the moment.