Więc moim celem w tym tygodniu było znalezienie szesnastkowejósemkowy i binarny po przecinku. Byłem w stanie uzyskać wartości szesnastkowe, binarne i ósemkowe, ale były to pojedyncze pętle różnych klas publicznych. Zastanawiałem się więc, jak mogę uczynić ten kod jednym i czytać w systemie szesnastkowym, ósemkowym i binarnym.
dziesiętny na szesnastkowy
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);
}
}
dziesiętny na ósemkowy
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);
}
}
dziesiętny na dwójkowy
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);
}
}
Odpowiedzi:
1 dla odpowiedzi № 1Łatwym sposobem byłoby na przykład użycie już obecnych konwersji
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);
Jeśli potrzebujesz wartości całkowitej, a nie ciągu, możesz użyć
int h = Integer.parseInt(hex, 16);
int o = Integer.parseInt(oct, 8);
int b = Integer.parseInt(bin, 2);
Zakładając, że nie chcesz korzystać z tych metod (powiedzmy, że masz swoje powody).
Najpierw musisz umieścić swój kod w metodzie, a nie w main.
Następnie możesz zrobić coś takiego:
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();
}
}
Próbowałem wprowadzić jak najmniej zmian w kodzie.