簡體   English   中英

不能通過方法調用轉換將String []轉換為int嗎?

[英]String[] cannot be converted to int by method invocation conversion?

這段代碼試圖做什么

所需結果:

使用代碼解密以下引用的含義:

另外,您可以使用模運算符從數字中提取最右邊的一個或多個數字。 例如,x%10產生x的最右邊數字(以10為底)。 同樣,x%100會產生最后兩位數字。 -想想Java,艾倫·唐尼,綠茶出版社

問題

  • 如何修復此錯誤?
  • 我的代碼可以簡化還是合並(請參見注釋)?

錯誤信息

extRight.java:11: error: method extRight in class 
extRight cannot be applied to given types;
                int s = extRight(args);
                        ^
  required: int
  found: String[]
  reason: actual argument String[] cannot be 
  converted to int by method invocation conversion
1 error

我的密碼

class extRight {

    public static void extRight(int x) {

        x = x % 10; //initially tried `x % 10;`

    }

    public static void main(String[] args){

            //initially the below was: extRight(args);
            //then: System.out.print(extRight(args));
        int s = extRight(args);
        String o = Integer.toString(s);
        System.out.print(o);

    }

}

現有功能

首先,將函數的名稱extRight更改為其他名稱! 它是函數類的名稱,該類將用於定義該類的構造函數。

如何為參數分配新值

x是一個參數,您不能修改x = x % 10;類的參數x = x % 10; 要么聲明一個靜態變量int x ,然后直接訪問它; 或者,從您的函數返回int! 否則,您的功能將無用。 例:

//return x
public static int extRight1(int x) {

    x = x % 10;
    return x;

}

傳遞錯誤的類型

您正在將args傳遞給extRight ,它需要一個int類型的參數: int s = extRight(args);

自動字符串對話

您不必執行此步驟: String o = Integer.toString(s); 您可以直接編寫System.out.print(s); ,因為它將在打印到控制台之前將sysout中的int自動轉換為String。

工作代碼

工作代碼如下所示(根據我對您要實現的目標的理解):

class extRight {

public static int extRight1(int x) {

    x = x % 10; 
     return x;

}

public static void main(String[] args){
    //do not forget  to pass command line args to program like this "java extRight 1"   
    int s = extRight1(Integer.parseInt(args[0]));// change indices to assign different arg[] values
   //  String o = Integer.toString(s)  you dont have to do this you can directly write
    System.out.print(s);

}

}
extRight(args) // extRight() expects an int, you are passing a String[]

校驗

public static void extRight(int x) { // expecting int. Also int s = extRight(args); --> this method returns nothing i.e, void

    x = x % 10; //initially tried `x % 10;`

}

乍一看,這里有兩件事是錯誤的(可能還有很多)。 查看您對extRight方法的定義:

public static void extRight(int x)

具體類型。 參數intreturn值為void 現在看看如何使用它:

int s = extRight(args);

argsString[] (因此會發生編譯器錯誤),如果沒有返回類型,您期望s變成什么?

甚至還不清楚該代碼試圖做什么,但是要“更正錯誤”,您需要正確對齊類型。 僅將參數更改為String[]並不會真正起作用,因為該方法的邏輯需要一個int 也許您需要將第一個args值轉換為int並將其傳遞給方法? 然后,您還想從該方法中返回一些東西,似乎...

如你所見

String[] args

argsString -Objects的數組

你的方法

void extRight(int x)

但是以int作為參數。 基本類型int和對象類型String不兼容。 另外,對象數組與相同類型的單個對象不兼容。 您需要遍歷參數數組,並將String值顯式轉換/解析為int值。

該聲明

x = x % 10;

不會產生您期望的效果,因為在Java參數中按值(也就是引用)傳遞參數,所以當您在方法內部修改x時,修改后的值僅在該方法中可見。 (這與在C中可以將指針傳遞給int的方式不同!)

最后您的方法extRight的返回類型為void ,這將使您在行上出錯

int s = extRight(args);

您的方法需要

int extRight(int x)

這是您課程的修改版本

class ExtRight {
    public static int extRight(int x) {
        return x % 10;
    }

    public static void main(String[] args){
        for(String s : args) {
            int i = extRight(Integer.parseInt(s));
            System.out.print(i);
        }
    }
}

另請注意,您不應使用與該類相同的名稱來命名方法,並且該類名應始終以大寫字母開頭。

args是字符串類型的。 您需要將arg轉換為Int類型。

這是修改后的代碼:

class extRight {

public static int extRight(int x) {

    x = x % 10; //initially tried `x % 10;`
    return x;

}

public static void main(String[] args) {

    int i = Integer.parseInt(args[0]) //convert to int
    int s = extRight(i);
    String o = Integer.toString(s);
    System.out.print(o);
}
}

您必須將“ args”字符串標簽解析為整數:

int t = 0;
try {
    t = Integer.parseInt(args[0]);
} catch (Exception e) {
    // error : the first argument is not an integer
    t = 0;
}
s = extRight(t);

您的代碼有一些非常基本的問題。

第一個問題是您擁有String[]並嘗試將其視為int 一個String[]可能包含多個值(甚至0個值!),因此不清楚要將哪個數字視為數字。 我假設您要第一個。

第二個問題是extRight函數中的返回類型和按值調用的問題。 在函數內部為x賦值將僅覆蓋局部變量x 它不會更改調用函數(您的main )的變量。

這是我對您要實現的目標以及我建議的解決方案的猜測:

class extRight {
    //this function now returns a value so that the changes by your calculations
    // can be used by the main function
    public static int extRight(int x) {
        return x % 10;
    }

    public static void main(String[] args){
        //this converts the first argument to an int:
        int arg = Integer.valueOf(args[0]);
        //the return value of your extRight function is store to the variable s
        int s = extRight(arg);
        //instead of your code, you can simply print the int variable
        System.out.print(s);
    }
}

暫無
暫無

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

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