[英]how can i put my my binary, octal, and Hexadecimal in one loop
所以我這周的目標是找到十六進制的八進制和二進制的十進制。 我能夠獲得十六進制、二進制和八進制,但在不同的公共類中是單獨的循環。 所以我想知道如何使這段代碼合一並在一個循環中讀取十六進制、八進制和二進制。
十進制轉十六進制
import java.util.Scanner;
public class uncode {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = "";
while (decimal != 0 ) {
int hexValue = decimal % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ?
(char) (hexValue + '0') : (char)(hexValue - 10 + 'A');
hex = hexDigit + hex;
decimal = decimal / 16;
}
System.out.println("The hex number is " + hex);
}
}
十進制轉八進制
import java.util.Scanner;
public class octal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String octal = "";
while ( decimal > 0 ) {
int remainder = decimal % 8;
octal = remainder + octal;
decimal = decimal / 8;
}
System.out.println("Octal number: " + octal);
}
}
十進制轉二進制
import java.util.Scanner;
public class GuessNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
System.out.println("Binary number: " + binary);
}
}
簡單的方法是使用已經存在的轉換,例如
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = Integer.toHexString(decimal);
String oct = Integer.toOctalString(decimal);
String bin = Integer.toBinaryString(decimal);
如果你需要一個整數值,而不是字符串,你可以使用
int h = Integer.parseInt(hex, 16);
int o = Integer.parseInt(oct, 8);
int b = Integer.parseInt(bin, 2);
假設您不想使用這些方法(假設您有自己的理由)。
首先,您需要將代碼放在一個方法中,而不是放在 main 中。
然后你可以做這樣的事情:
public class Class {
public static void uncode() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String hex = "";
while (decimal != 0) {
int hexValue = decimal % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
: (char) (hexValue - 10 + 'A');
hex = hexDigit + hex;
decimal = decimal / 16;
}
System.out.println("The hex number is " + hex);
}
public static void octal() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String octal = "";
while (decimal > 0) {
int remainder = decimal % 8;
octal = remainder + octal;
decimal = decimal / 8;
}
System.out.println("Octal number: " + octal);
}
public static void GuessNumbers() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
String binary = "";
while (decimal > 0) {
int remainder = decimal % 2;
binary = remainder + binary;
decimal = decimal / 2;
}
System.out.println("Binary number: " + binary);
}
public static void allInOne() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal number: ");
int decimal = input.nextInt();
int hex = decimal;
int oct = decimal;
int bin = decimal;
String hexal = "";
String octal = "";
String binary = "";
while (hex > 0 || oct > 0 || bin > 0) {
if (hex > 0) {
// Get Hexal
int hexValue = hex % 16;
char hexDigit = (hexValue <= 9 && hexValue > 0) ? (char) (hexValue + '0')
: (char) (hexValue - 10 + 'A');
hexal = hexDigit + hexal;
hex = hex / 16;
}
if (oct > 0) {
// Get Octal
int remainder = oct % 8;
octal = remainder + octal;
oct = oct / 8;
}
if (bin > 0) {
// Get Binary
int remainder = bin % 2;
binary = remainder + binary;
bin = bin / 2;
}
}
System.out.println("The hex number is " + hexal);
System.out.println("Octal number: " + octal);
System.out.println("Binary number: " + binary);
}
public static void main(String[] args) {
uncode();
octal();
GuessNumbers();
allInOne();
}
}
我試圖對您的代碼進行盡可能少的更改。
在這里,我通過調用方法 getEveryFromDeci(param1,param2) 從十進制轉換為八進制、二進制、十六進制,其中 param1 - 任何十進制數和 param2 - 它的基值,如 8,2,16。 我還通過調用方法 allToDeci(param1,param2) 將八進制、二進制、十六進制轉換為十進制,其中 param1 - 字符串形式的十六進制、二進制、八進制值和 param2- 十六進制的基值
private String getEveryFromDeci(Integer x,Integer y){
List<String> al = deciBin(x,y,new ArrayList<String>());
StringBuffer buffer = new StringBuffer();
for(String s : al)
buffer.append(s);
return buffer.toString();
}
private List<String> deciBin(Integer a,Integer b,List<String> list){
if(a>=b){
deciBin(a/b,b,list);
list.add(a%b > 9 ? getHexaDecimal(a%b):Integer.toString(a%b));
}else
list.add(Integer.toString(a));
return list;
}
private String getHexaDecimal(int d){
String s= null;
switch(d){
case 10:
s="A";
break;
case 11:
s="B";
break;
case 12:
s="C";
break;
case 13:
s="D";
break;
case 14:
s="E";
break;
case 15:
s="F";
break;
}
return s;
}
private int allToDeci(String applyNum,int type){
int sum =0;
char[] ch = applyNum.toCharArray();
for(int pum=0;pum<ch.length;pum++)
sum += Character.isDigit(ch[pum]) ? getAct(ch.length-(pum+1),type) * Character.getNumericValue(ch[pum]) :getAct(ch.length-(pum+1),type) * getNum(ch[pum]);
return sum;
}
private int getNum(char ch){
int num = 0;
switch(ch){
case 'A':
num =10;
break;
case 'B':
num = 11;
break;
case 'C':
num =12;
break;
case 'D':
num =13;
break;
case 'E':
num =14;
break;
case 'F':
num=15;
break;
default:
num =Character.getNumericValue(ch);
break;
}
return num;
}
private int getAct(int k,int p){
int s=1;
if(k >0){
for(int i=0;i<k;i++)
s *=p;
return s;
}else
return 1;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.