协助使用二进制数字计算最多数字的java程序

时间:2017-09-29 20:35:16

标签: java loops for-loop binary

我最近开始学习二进制十六进制等计算机组织课程,我自己尝试创建一个从0到输入数字的程序,但计数是用二进制完成的。我遇到了一些麻烦,让自己感到困惑,一些澄清和帮助将不胜感激。具体来说,如何使用某种for-loop有效地替换包含前一个二进制数的字符串的值,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);
        }
}

}

1 个答案:

答案 0 :(得分: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);

    }   
}