简体   繁体   English

如何将二进制转换为十进制?

[英]How to Convert Binary to Decimal?

I have almost completed this code, but it has told me that There is an illegal modifier in the public static int power; 我几乎完成了这段代码,但是它告诉我,公共静态int权力中存在非法修饰符; line. 线。 Can you guys please help? 你们能帮忙吗?

public static int binToDec(int i)
{
    int[] numbers;//initialize variable
    int f = 4;
    String iString = "" + i;
    int result = 0;
    int length = iString.length();
    public static int power;
    for(power = iString.length(); power>=0;power--)
    {
    while(f == length && f >= 0)
    {

        numbers[power] = iString.charAt(power)^power;
    }

    length--;
    f--;
    }
    for(int g = 0; g <= numbers.length; g++)//double check constraints
    {
        result = numbers[g] = numbers[power];
    }

        return result;
}

The modifiers public and static have no meaning for a local variable inside a method and are not valid there. 修饰符publicstatic对于方法内部的局部变量没有意义,并且在那里无效。 The compiler error is suggesting that you remove them. 编译器错误提示您删除它们。

在以下位置删除“公共静态”:

public static int power;

In Java, static means that it's a variable or method within a class. 在Java中,静态表示它是类中的变量或方法。 In other words if you use static it has to belong to the scope of the whole class. 换句话说,如果使用静态,则它必须属于整个类的范围。 If you move that line of code outside of your method it should fix your problem. 如果将代码行移到方法之外,则应该可以解决问题。

You can do this. 你可以这样做。

int power = Istring.length(); And remove the initialization from for loop. 并从for循环中删除初始化。

Declaring and defining a variable inside a method will limit its scope to the method only. 在方法内部声明和定义变量只会将其范围限制为该方法。 public is a keyword that is used to make it visible to every class out there and static makes the variable shared for whole class. public是一个关键字,用于使其对每个班级可见,而static使该变量为整个班级共享。

Therefore, public and static are not allowed for the method variables. 因此,方法变量不允许使用public和static。 Hence, only final keyword is permitted for such variables 因此,此类变量仅允许使用final关键字

Check this out for more on scope, default values and lifetime of method, instance and class variables 请查看此内容,以获取有关范围,方法的默认值和寿命,实例和类变量的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM