十进制到二进制,打印错误的答案(Decimal to binary, prints wrong answers)

编程入门 行业动态 更新时间:2024-10-24 14:20:51
十进制到二进制,打印错误的答案(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(" "); } } } }

更多推荐

本文发布于:2023-07-23 18:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   答案   Decimal   十进制到   wrong

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!