有人可以指出为什么我的十进制输入1不会传输到输出。这意味着如果我在提示输入整数时输入1,它将除了我的小数之外的所有内容都吐出。小数结果为0.
import java.util.Scanner;
public class Dboh {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int decimal = input.nextInt();
int single;
int val;
int rem;
//Binary
String binaryString = "";
int value = decimal;
while (value != 0) {
binaryString = value % 2 + binaryString;
value = value / 2;
}
//Octal
String octalString = "";
for(val = decimal; val > 0; val = val / 8){
octalString = val % 8 + octalString;
}
//Hexadecimal
String hexstring="";
// Digits in hexadecimal number system
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
do {
rem=decimal%16;
hexstring=hex[rem]+hexstring;
decimal=decimal/16;
}while (decimal > 0);
System.out.println("Decimal" + "\t" + "\t" + "\t" +"Binary" + "\t" + "\t" + "\t" + "Octal" + "\t" + "\t" + "\t" + "Hexadecimal");
System.out.println(decimal + "\t" + "\t" + "\t" + binaryString + "\t" + "\t" + "\t" + octalString + "\t" +"\t" + "\t" + hexstring);
}
}
答案 0 :(得分:0)
在你的代码中,你有一个while循环,它将'decimal'除以它达到0.如果你想使用decimal的值而不实际影响它,可以考虑制作一个复制变量。
这部分
do {
rem=decimal%16;
hexstring=hex[rem]+hexstring;
decimal=decimal/16;
}while (decimal > 0);
也许可以这样做:
int dec = decimal;
do {
rem=decimal%16;
hexstring=hex[rem]+hexstring;
dec/= 16;
}while (dec> 0);
答案 1 :(得分:0)
尝试创建一个像这样的局部变量future
:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>1st Radio</label>
<input type="radio" name="radio-group" id="radio-1" value="1">
<label>2nd Radio</label>
<input type="radio" name="radio-group" id="radio-2" value="2">
<script>
$(document).keyup(function (e) {
if (e.keyCode == 9) {
$('#radio-1').prop('checked', true)
}
});
</script>
并将十进制转换为像这样浮动:
float result
然后像这样更改while循环:
int decimal = input.nextInt();
float result;
这样的sysout:
result=(float)decimal/16;
得到输出:
}while (result < 0);