简体   繁体   English

有关JAVA中轻松“返回”的问题

[英]Questions about easy “return” in JAVA

I want to make a code which has function of changing binary to decimal. 我想制作一个具有将二进制更改为十进制的功能的代码。

So i made a public long tonum() 所以我公开了一个长tonum()

and tried to return on Main method. 并尝试返回Main方法。

but there is anything shown on screen. 但屏幕上没有显示任何内容。 Where is the problem? 问题出在哪儿? Plz give me some hints. 请给我一些提示。

public class Bitmap { 
       byte[] byteArr; //byte array for saving 0 or 1 
       char[] charArr; //char array for casting from string to byte array 

    public static void main(String[] args) throws Exception { 
          Bitmap test1 = new Bitmap("100110"); 
          test1.tonum();              
       } 
    public Bitmap(String val) throws Exception {  
              byteArr = new byte[val.length()];  //To make the array length of Bitmap should e same as that of string
              charArr = val.toCharArray();       //casting from string to char 

              for(int i = 0; i < charArr.length; i++) { 
                 if (charArr[i] == '0')  
                     byteArr[i] = 0; 
                 else if (charArr[i] == '1') 
                     byteArr[i] = 1; 
                 else throw new Exception("Bitmap are should be sequences of zeros and ones!"); 
              } 
           } 

    public long tonum() {
       int temp = 0;
       String str = "";
       String str2 = "";
       for (int i = 0; i < this.byteArr.length; i++){
           temp = this.byteArr[i];
           str = Integer.toString(temp);
           str2 = str2 + str;
       }
       long decimal = (long)Integer.parseInt(str2,10);
       System.out.println(str2);
       return decimal;
   }
}
       long decimal = (long)Integer.parseInt(str2,10);

That doesn't change binary to decimal. 不会将二进制更改为十进制。 It changes decimal to binary. 它将十进制更改为二进制。 And you don't have decimal in the first place, you have a string or presentation of binary. 而且您首先没有十进制,而是一个字符串或二进制表示形式。 Try a radix of 2. But why you have both a char array and a byte array when all you do is deconstruct and reconstruct the original String is anybody's guess. 尝试以2为基数。但是,当您要做的所有事情都是解构并重建原始String时,为什么同时拥有char数组和byte数组却是所有人的猜测。

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

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