十进制到二进制,打印错误的答案(Decimal to binary, prints wrong answers)
嘿,我想知道是否有人可以发现我的代码有问题? 如果你能帮我解释一下吧! 当我输入99时我得到1100 011它应该是0110 0011。
import java.util.*; public class SchoolHomework { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Program that converts decimal to binary!"); int dec; System.out.println("Please type in a decimal number:"); Scanner input = new Scanner(System.in); Stack<Integer> todec = new Stack<Integer>(); dec = input.nextInt(); if (dec < 0){ System.out.println("Error: Please enter a positive number!"); System.exit(0); } while (dec != 0){ int stackv = dec % 2; todec.push(stackv); dec /= 2; } System.out.println(dec + " To binary is: "); int counter = 0; while (!(todec.isEmpty() )) { String val = todec.pop().toString(); System.out.print(val); counter = counter + 1; if (counter >= 4){ counter = 0; System.out.print(" "); } } } }Hey i was wondering if anyone can find a problem with my code? If you can could you explain it to me please! When i input 99 i get 1100 011 when it should be 0110 0011.
import java.util.*; public class SchoolHomework { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Program that converts decimal to binary!"); int dec; System.out.println("Please type in a decimal number:"); Scanner input = new Scanner(System.in); Stack<Integer> todec = new Stack<Integer>(); dec = input.nextInt(); if (dec < 0){ System.out.println("Error: Please enter a positive number!"); System.exit(0); } while (dec != 0){ int stackv = dec % 2; todec.push(stackv); dec /= 2; } System.out.println(dec + " To binary is: "); int counter = 0; while (!(todec.isEmpty() )) { String val = todec.pop().toString(); System.out.print(val); counter = counter + 1; if (counter >= 4){ counter = 0; System.out.print(" "); } } } }最满意答案
你写的算法看起来非常好。 你接近解决方案。 最简单的方法是继续将零推到堆栈,直到达到4的长度倍数。如果你有一个好的注释,请告诉我;)
import java.util.*; public class SchoolHomework { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Program that converts decimal to binary!"); int dec; System.out.println("Please type in a decimal number:"); Scanner input = new Scanner(System.in); Stack<Integer> todec = new Stack<Integer>(); dec = input.nextInt(); if (dec < 0){ System.out.println("Error: Please enter a positive number!"); System.exit(0); } int size = 0; while (dec != 0){ int stackv = dec % 2; todec.push(stackv); dec /= 2; size++; } if (size % 4 > 0) { for(int i = 0; i < 4 - (size % 4); i++) { todec.push(0); } } System.out.println(dec + " To binary is: "); int counter = 0; while (!(todec.isEmpty() )) { String val = todec.pop().toString(); System.out.print(val); counter = counter + 1; if (counter >= 4){ counter = 0; System.out.print(" "); } } } }The algorithm you wrote looks very good. You were near the solution. The easy one is to continue to push zeroes to your stack until you reach a length multiple of 4. Let me know if you got a good note ;)
import java.util.*; public class SchoolHomework { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Program that converts decimal to binary!"); int dec; System.out.println("Please type in a decimal number:"); Scanner input = new Scanner(System.in); Stack<Integer> todec = new Stack<Integer>(); dec = input.nextInt(); if (dec < 0){ System.out.println("Error: Please enter a positive number!"); System.exit(0); } int size = 0; while (dec != 0){ int stackv = dec % 2; todec.push(stackv); dec /= 2; size++; } if (size % 4 > 0) { for(int i = 0; i < 4 - (size % 4); i++) { todec.push(0); } } System.out.println(dec + " To binary is: "); int counter = 0; while (!(todec.isEmpty() )) { String val = todec.pop().toString(); System.out.print(val); counter = counter + 1; if (counter >= 4){ counter = 0; System.out.print(" "); } } } }更多推荐
发布评论