簡體   English   中英

在我的案例中如何刪除前導零?

[英]How to remove the leading zeros in my case?

我得到了以下練習:

  1. 編寫將任意數字轉換為二進制數的代碼
  2. 反轉二進制數並將其顯示在屏幕上
  3. 計算(並在屏幕上顯示)二進制數開頭有多少個零
  4. 刪除該反轉二進制數開頭的所有零。
  5. 將新的二進制數轉換回常規數字並在屏幕上顯示該數字。

目前我卡在第 4 步,有人可以幫助我嗎? 到目前為止,我有這個:

import java.util.*;


class Main {
  public static void main(String[] args) {

  int woord = 100;

    
  String bin = Integer.toBinaryString(woord);
  String test = new StringBuilder(bin).reverse().toString();

  System.out.println(test);

  String[] parsed = test.split("1");
  System.out.println(parsed.length > 0 ? parsed[0].length() : "0");

  }
}

提前致謝:)

我得到了答案,第一個人已經幫助了我很多。 我也會從其他答案中學習和閱讀。 不要覺得有必要發布更多回復(除非你當然想):)

這里涵蓋了每一點,請參閱一個非常簡單的解決方案:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int inputDecimalNumber = scan.nextInt();

    System.out.println("Input number in decimal: "+inputDecimalNumber);
    
     //1.  Make a code that converts any number into a binary number        
     String inputBinary = Integer.toBinaryString(inputDecimalNumber);
     System.out.println("Input number in binary : "+inputBinary);
     
     //2. Reverse that binary number and show it on screen
      String reverseBinary = new StringBuilder(inputBinary).reverse().toString();
      System.out.println("Reverse binary number: "+reverseBinary);

     //3. Count (and show on screen) how many zeros there are at the beginning of the binary number
      int leadingZeros = reverseBinary.indexOf("1") > 0 ? reverseBinary.indexOf("1") : 0;
      System.out.println("Leading zeroes count in reverse binary : "+leadingZeros);

     //4. Remove all the zeros at the beginning of that reversed binary number.
      String reverseBinaryAfterRemovingLeadingZeros = reverseBinary.substring(reverseBinary.indexOf("1"));
      System.out.println("Reverse binary after removing leading zeroes : "+ reverseBinaryAfterRemovingLeadingZeros);
      
     //5. Convert the new binary number back to a regular number and show that number on screen.
      int decimalValue=Integer.parseInt(reverseBinaryAfterRemovingLeadingZeros,2);
      System.out.println("Reverse binary into decimal number : "+decimalValue);
              
      
}

OUTPUT

Enter a number: 38
Input number in decimal: 38
Input number in binary : 100110
Reverse binary number: 011001
Leading zeroes count in reverse binary : 1
Reverse binary after removing leading zeroes : 11001
Reverse binary into decimal number : 25

首先,您應該使用Scanner並提示用戶輸入一個數字。 查找零的計數等同於查找第一個1 (如果沒有1 ,則為String的長度)。 最后,您可以使用Integer.parseInt(String, int)將二進制String解析回int 喜歡,

Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
System.out.flush();
int val = scan.nextInt();
String bin = Integer.toBinaryString(val);
String rev = new StringBuilder(bin).reverse().toString();
System.out.println(rev);
int count = rev.indexOf('1');
if (count < 0) {
    count = rev.length();
}
System.out.println(count);
System.out.println(Integer.parseInt(rev.substring(count), 2));

要解決第四步,您需要弄清楚您需要刪除的數字部分是什么。

  • 首先,找到字符串的第一個1 ,它將是您將保留在縮短的字符串中的第一個字符。
  • 然后只需要保留這個字符的反向字符串的字符,一定要檢查是否有1否則你將處理java.lang.StringIndexOutOfBoundsException因為如果沒有匹配則indexOf()返回 -1:

    int firstOne = test.indexOf('1');
    
    String shortened;

    if(firstOne >=0) //indexOf method returns -1 if no match has been found
       shortened = test.substring(firstOne);
    else 
       shortened = test;

由於這是您得到的練習,我將在這里停止,如果您需要第 5 部分的幫助,請隨時發表評論。

我只會使用正則表達式:

class Main {
    public static void main(String[] args) {

        int woord = 100;


        String bin = Integer.toBinaryString(woord);
        String test = new StringBuilder(bin).reverse().toString();

        System.out.println(test);
        System.out.println(test.replaceAll("^[0]*", ""));
    }
}

Output:

0010011
10011

只需遍歷字符串並檢查字符串的第一個字符,直到不再有任何零為止,

檢查此代碼

    public static void main(String[] args) {
    int woord = 100;

    // step 1
    String bin = Integer.toBinaryString(woord);
    // step 2
    String test = new StringBuilder(bin).reverse().toString();
    System.out.println(test);
    // step 3

    String[] parsed = test.split("1");
    System.out.println(parsed.length > 0 ? parsed[0].length() : "0");

    // step 4

    for (int i = 0; i < test.length(); i++) {
        char c = test.charAt(i);
        if (c == '0') {
            test = test.replaceFirst("0", "");
            System.out.println(test);
        }
        //Process char
    }
}

暫無
暫無

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

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