繁体   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