协助一个java程序,该程序以二进制形式计数到一个数字

协助一个java程序,该程序以二进制形式计数到一个数字,java,loops,for-loop,binary,Java,Loops,For Loop,Binary,我最近参加了一个计算机组织课程,学习二进制十六进制等,我自己尝试创建一个从0到输入数字的程序,但是计数是用二进制完成的。我遇到了一些麻烦,困惑得难以置信,请给予澄清和帮助。具体地说,如何使用某种形式的for循环,用0和1有效地替换包含先前二进制数的字符串的值。不过,我知道有一些方法可以直接将字符串转换为二进制;我想用这个更复杂的方法来练习 package counting; import java.util.Scanner; public class counting { public st

我最近参加了一个计算机组织课程,学习二进制十六进制等,我自己尝试创建一个从0到输入数字的程序,但是计数是用二进制完成的。我遇到了一些麻烦,困惑得难以置信,请给予澄清和帮助。具体地说,如何使用某种形式的for循环,用0和1有效地替换包含先前二进制数的字符串的值。不过,我知道有一些方法可以直接将字符串转换为二进制;我想用这个更复杂的方法来练习

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);
        }
}

}

这里有一个小循环,它将完成您所寻找的内容。你只需要记住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);

    }   
}