gpt4 book ai didi

java - 我怎样才能把我的二进制,八进制和十六进制放在一个循环中

转载 作者:行者123 更新时间:2023-12-01 09:52:15 26 4
gpt4 key购买 nike

所以我本周的目标是找到十进制的十六进制八进制和二进制。我能够获得十六进制、二进制和八进制,但它们是不同公共(public)类上的单独循环。所以我想知道如何使这段代码合一并在一个循环中读取十六进制、八进制和二进制。

<小时/>

十进制转十六进制

 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();
}
}

我尝试对您的代码进行尽可能少的更改。

关于java - 我怎样才能把我的二进制,八进制和十六进制放在一个循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com