简体   繁体   English

以二进制表示反转整数

[英]Reversing an integer in binary representation

I tried to create a code to take in a whole number in Java and output it in binary.我试图创建一个代码来在 Java 中接收一个整数并以二进制形式输出它。 The problem would seem that the binary is printing out backward.问题似乎是二进制文件向后打印。 For instance, 6 should output as 011 but comes out as 110 .例如, 6应该输出为011但输出为110

import java.util.Scanner;

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

    int userNum;

    Scanner in =new Scanner(System. in );

    userNum = in.nextInt();

    binary(userNum);
    System.out.print("\n");
  }

  private static void binary(int userNum) {
    int remainder;

    while (userNum <= 1) {
      System.out.print(userNum);
      return;
    }

    remainder = userNum % 2;
    binary(userNum >> 1);
    System.out.print(remainder);
  }
}

I tried incorporating a push stack to push the remainder into a stack that I can pull later, but couldn't quite get it to land.我尝试合并一个推栈,将剩余部分推入一个我稍后可以拉出的堆栈中,但无法让它落地。

private static void reverse(int userNum) {

  String backwards;

  while (userNum >= 0) {
    backwards.push(int userNum);
    System.out.println(backwards);
    return;
  }
}

It is part of a class assignment which asks the following.它是要求以下内容的课堂作业的一部分。

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.编写一个程序,将一个正整数作为输入,并输出一个由 1 和 0 组成的字符串,以二进制表示该整数。 For an integer x, the algorithm is:对于整数 x,算法为:

As long as x is greater than 0
   Output x % 2 (remainder is either 0 or 1)
   x = x / 2

Note: The above algorithm outputs the 0's and 1's in reverse order.注意:上述算法以相反的顺序输出 0 和 1。

Ex: If the input is:例如:如果输入是:

6

the output is:输出是:

011

6 in binary is 110 ;二进制中的6110 the algorithm outputs the bits in reverse.该算法反向输出这些位。

These are the tests the program applies and my results.这些是程序应用的测试和我的结果。

Input 6输入6

Your output binary is:110你的输出binary is:110

Expected output 011预期产出011

Input 19输入19

Your output 10011你的输出10011

Expected output 11001预期产量11001

Input 255输入255

Your output 11111111你的输出11111111

Expected output 11111111预期输出11111111

Any help or guidance in this, I would be greatly appreciative of it.这方面的任何帮助或指导,我将不胜感激。

Per the requirement and not taking into consideration of negative numbers根据要求而不考虑负数

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        int userNum;

        Scanner scnr = new Scanner(System.in);

        userNum = scnr.nextInt();

        while(userNum > 0){
            System.out.print(userNum % 2);
            userNum = userNum / 2;
        }
        System.out.print("\n");
    }
}

I tried to create a code to take in a whole number in Java and output it in binary.我试图创建一个代码来在 Java 中接收一个整数并以二进制形式输出它。 The problem would seem that the binary is printing out backward.问题似乎是二进制文件向后打印。 For instance, 6 should output as 011 but comes out as 110 .例如, 6应该输出为011但输出为110

import java.util.Scanner;

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

    int userNum;

    Scanner in =new Scanner(System. in );

    userNum = in.nextInt();

    binary(userNum);
    System.out.print("\n");
  }

  private static void binary(int userNum) {
    int remainder;

    while (userNum <= 1) {
      System.out.print(userNum);
      return;
    }

    remainder = userNum % 2;
    binary(userNum >> 1);
    System.out.print(remainder);
  }
}

I tried incorporating a push stack to push the remainder into a stack that I can pull later, but couldn't quite get it to land.我尝试合并一个推栈,将剩余部分推入一个我稍后可以拉出的堆栈中,但无法让它落地。

private static void reverse(int userNum) {

  String backwards;

  while (userNum >= 0) {
    backwards.push(int userNum);
    System.out.println(backwards);
    return;
  }
}

It is part of a class assignment which asks the following.它是要求以下内容的课堂作业的一部分。

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.编写一个程序,输入一个正整数作为输入,并输出一个由 1 和 0 组成的字符串,以二进制表示该整数。 For an integer x, the algorithm is:对于整数 x,算法为:

As long as x is greater than 0
   Output x % 2 (remainder is either 0 or 1)
   x = x / 2

Note: The above algorithm outputs the 0's and 1's in reverse order.注意:上述算法以相反的顺序输出 0 和 1。

Ex: If the input is:例如:如果输入是:

6

the output is:输出是:

011

6 in binary is 110 ;二进制中的6110 the algorithm outputs the bits in reverse.该算法反向输出这些位。

These are the tests the program applies and my results.这些是程序应用的测试和我的结果。

Input 6输入6

Your output binary is:110你的输出binary is:110

Expected output 011预期产出011

Input 19输入19

Your output 10011你的输出10011

Expected output 11001预期产量11001

Input 255输入255

Your output 11111111你的输出11111111

Expected output 11111111预期输出11111111

Any help or guidance in this, I would be greatly appreciative of it.这方面的任何帮助或指导,我将不胜感激。

I tried to create a code to take in a whole number in Java and output it in binary.我试图创建一个代码来在 Java 中接收一个整数并以二进制形式输出它。 The problem would seem that the binary is printing out backward.问题似乎是二进制文件向后打印。 For instance, 6 should output as 011 but comes out as 110 .例如, 6应该输出为011但输出为110

import java.util.Scanner;

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

    int userNum;

    Scanner in =new Scanner(System. in );

    userNum = in.nextInt();

    binary(userNum);
    System.out.print("\n");
  }

  private static void binary(int userNum) {
    int remainder;

    while (userNum <= 1) {
      System.out.print(userNum);
      return;
    }

    remainder = userNum % 2;
    binary(userNum >> 1);
    System.out.print(remainder);
  }
}

I tried incorporating a push stack to push the remainder into a stack that I can pull later, but couldn't quite get it to land.我尝试合并一个推栈,将剩余部分推入一个我稍后可以拉出的堆栈中,但无法让它落地。

private static void reverse(int userNum) {

  String backwards;

  while (userNum >= 0) {
    backwards.push(int userNum);
    System.out.println(backwards);
    return;
  }
}

It is part of a class assignment which asks the following.它是要求以下内容的课堂作业的一部分。

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary.编写一个程序,输入一个正整数作为输入,并输出一个由 1 和 0 组成的字符串,以二进制表示该整数。 For an integer x, the algorithm is:对于整数 x,算法为:

As long as x is greater than 0
   Output x % 2 (remainder is either 0 or 1)
   x = x / 2

Note: The above algorithm outputs the 0's and 1's in reverse order.注意:上述算法以相反的顺序输出 0 和 1。

Ex: If the input is:例如:如果输入是:

6

the output is:输出是:

011

6 in binary is 110 ;二进制中的6110 the algorithm outputs the bits in reverse.该算法反向输出这些位。

These are the tests the program applies and my results.这些是程序应用的测试和我的结果。

Input 6输入6

Your output binary is:110你的输出binary is:110

Expected output 011预期产出011

Input 19输入19

Your output 10011你的输出10011

Expected output 11001预期产量11001

Input 255输入255

Your output 11111111你的输出11111111

Expected output 11111111预期输出11111111

Any help or guidance in this, I would be greatly appreciative of it.这方面的任何帮助或指导,我将不胜感激。

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

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