簡體   English   中英

我不確定如何構造我的算術方法

[英]I'm not sure how to structure my arithmetic methods

我是Java的新手。 我想要一個將使用submethods(?)的方法。 我想要這樣的東西:

Math.Addition(1, 1, X in this case, integer of users choice for the output);

輸出將存儲在變量X中。但是我也想這樣做:

Math.Subtraction(2, 1, X);

我該怎么做?

創建一個名為Math的類。

並在其中包含兩個帶有所需參數的名為“加法”和“減法”的方法,並將每個方法的邏輯放入方法內部。

public class Math {
public static int X; 
public static int Addition(int a, int b, String choice) {
    X = a+b;
    return X;
}

public static int Substraction(int a, int b, String choice) {
    X = a-b;
    return X;
}

}

您可能需要返回該值才能分配給xeg

X = myClass.add(1,1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM