簡體   English   中英

我要求用戶輸入他的(int)ID,然后我想獲取 ID 並將其存儲在數組中以繼續

[英]I asked the user to enter his (int)ID and then I want to take the ID and store it in an array to continue

現在,當我在“開關”中調用該方法時,我很困惑如何將用戶輸入的 ID 作為“方程式”方法的參數我已經完成了這段代碼,但仍然不知道該怎么做讓它發揮作用

這是開關:

switch(numberChosen)
    {
        case "1":
            Scanner readID = new Scanner(System.in);
            System.out.println("Enter your ID: ");
            String ID = readID.nextLine();
            int yourID = Integer.parseInt(ID);

            equation();
    }

這是方法:

public static double equation(int[] ID_)
{
    ID_ = new int [];
    int x = 0, y = 0;
    for(int i = 0; i < 2; i++){
        x += ID_[i];
        return x;
    }
    for(int j = ID_.length; j > ID_.length-2; j--){
        y += ID_[j];
        return y;
    }
    double eq = x * ( 3*y-2 ) + 1;
    return eq;
}

這是您可以執行的操作:

在方法參數中,您可以進行 int 輸入。 例如:

public static double equation(int[] ID_, int userID)

這將允許您將用戶在 switch case 語句中插入的 int 放入該方法中。

然后你可以使用變量:

userID

在方程方法中做任何你必須做的事情。 這是一個例子:

public static void main(String[] args){
   //Original int
   int originalInt = 1;

   //Method that takes an int as an argument and does something with it
   doSomethingWithInt(originalInt);

}

//Your method
public static int doSomethingWithInt(int someInt){
      someInt = someInt + 5;

      return someInt;
}.

希望這可以幫助。

暫無
暫無

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

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