求助:整数转换字符串和十进制转换二进制
各位前辈大神,这里有个程序希望大家给点帮助,我是菜鸟,如果觉得问题太菜了请不要介意。
使用bin2str类函数来实现dec2bin.写一个测试程序让用户输入一个整数,接着程序打印出如下的结果。如果用户输入0或负数,这个程序结束。
Sample Output:
Please enter an integer number: 12
In binary: 1100
Would you like to continue? (0 or negative for NO) 1
Please enter an integer number: 14
In binary: 1110
Would you like to continue? (0 or negative for NO) -1
Bye
[解决办法]
写了一段,你参考参考吧。
package test;import java.util.Scanner;public class Test { public static void main(String[] e) { Scanner scan = new Scanner(System.in); System.out.println("Please enter an integer number:"); while(true){ int i = scan.nextInt(); if(i > 0){ System.out.println("In binary:" + Integer.toBinaryString(i)); System.out.println("Would you like to continue? (0 or negative for NO)"); }else{ System.out.println("Bye"); break; } } }}