简体   繁体   中英

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; 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. 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. 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.

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.

Therefore, public and static are not allowed for the method variables. Hence, only final keyword is permitted for such variables

Check this out for more on scope, default values and lifetime of method, instance and class variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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