2017-09-29 3 views
0

最近、私はバイナリ16進数などを学ぶ場所でコンピューター組織のコースを取ってきました。 0から入力番号までですが、カウントはバイナリで行われます。私はいくつかのトラブルに遭遇し、信念を超えて自分自身を混乱させました。何らかの明確化と支援が大いに評価されるでしょう。具体的には、以前の2進数を含む文字列の値を、0と1のどちらかのfor-loopを使用して効率的かつ効果的に置き換えるにはどうすればよいでしょうか。私は文字列をバイナリに直接変換する方法があることを知っています。私はこのより複雑な練習方法をやりたかったのです。ここでバイナリで数えられるJavaプログラムへのアシスタンス

package counting; 
import java.util.Scanner; 
public class counting 
{ 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.println("Hello, this is a number counter, please enter the integer you would like to count to"); 
    int number = input.nextInt(); 
    String start = "0000000000"; 
    // 000~etc is used as the start simply because i'm not sure how to calculate how many digit places 
    //the number input by the user will have 

    StringBuilder cont = new StringBuilder(start); 

    System.out.println(start); 

    /*What i intend to do is have the binary loop counter continue until it reaches 
    * the number input by the user, afterwards, working in a right to left manner, start counting from 
    * 0 up to the number given by the user, starting with 0. then using another loop, still using 
    * the right to left manner, if there is a 0, it should be replaced with a 1, and if there is a 
    * 1, it should be replaced with a 0, and the character before it should be replaced with a 1, if there 
    * is no room, continue to the left until there is a space available for a 1 and then reset all values 
    * after the 1 back to zero, and resume counting. the way i see it is requires a for loop to be used 
    * as the current position of a cursor used to determine what changes must be made 
    */ 

    for(int i = 0; i < number; i++) 
    { 
     int l = start.length(); 
     for(int n = 0; n <= number; n++) 
     { 

      for(int w = 1; w <= l; w++) 
      { 

       if (cont.charAt(l-w) == '0') 
       { 
        cont.setCharAt((cont.length()-w), '1'); 

        System.out.println(cont); 
       } 

       else if (cont.charAt(l-w) == '1') 
       { 
        cont.setCharAt((cont.length()-w), '0'); 
        cont.setCharAt((cont.length()-(w+1)), '1'); 

        System.out.println(cont); 
       } 

      } 
     } 
     System.out.println(cont); 
     } 
} 

}

答えて

1

あなたが探しているものを行います少しループです。バイナリでカウントするには、2のべき乗を覚えておくだけです。

public static char flip(char c){ 
    if(c == '0') 
     return '1'; 
    else 
     return '0'; 
} 

public static void main(String[] args) { 
    String start = "0000000000"; 

    StringBuilder cont = new StringBuilder(start); 

    int number = (int)Math.pow(2,10); 

    for(int i = 0; i < number; i++) 
    { 
     if(i != 0){ 

      int val = (int)Math.floor(i/2); 

      for(int j = 0; j <= val; j++){ 
       // Flip any bit that when modded by 2^j == 0 
       if(i % Math.pow(2,j) == 0){ 

        cont.setCharAt((cont.length() - (j + 1)), flip(cont.charAt(cont.length() - (j + 1)))); 
       } 
      } 
     } 

     System.out.println(cont); 

    } 
} 
関連する問題