Java十进制转十六进制程序

Java Decimal to Hexadecimal Program

问题:(十进制转十六进制)编写一个程序,提示用户输入一个介于两者之间的整数 0 和 15 并显示其对应的十六进制数。以下是一些示例运行:

请输入十进制值(0 到 15):11 十六进制值为 B

输入十进制值(0 到 15):5 十六进制值为 5

请输入十进制值(0 到 15):31 31 是无效输入

下面是我的代码。 1. 我不是很明白 charAt(0) 也不是我做错了什么。 1-9=1-9 和 10-15=A-F。我只能使用代码中看到的内容。没有特殊的 toHexStrings casesarrays。这是基础中的基础。我不明白为什么 RULE1 被忽略了,或者是否还有更多问题。

import java.util.Scanner;

public class NewClass1 {
    public static void main(String args[]) {              
    Scanner input = new Scanner(System.in);

        System.out.print("Enter a decimal value (0 to 15): ");
        String decimal = input.nextLine();

    // RULE1 
        char ch = decimal.charAt(0);
        if (ch <= 15 && ch >= 10) {
             System.out.println("The hex value is " + (char)ch);
        }

    // RULE2
        else if (ch <= 10 && ch >= 0) {
                System.out.println("Tsshe hex value is " + ch);
        }

    // RULE3
        else {
            System.out.println("Invalid input");
        }
    }
}

RULE1 被忽略,我不明白为什么。现在是凌晨 2 点,我已经在这里待了 4 个小时了。没有刻薄的评论,因为如果我知道如何解决这个问题,我就不会在这里。我需要一些帮助来理解这些错误。

更新 2: 导入 java.util.Scanner;

 public class NewClass1 {
     public static void main(String args[]) {              
     Scanner input = new Scanner(System.in);

         System.out.print("Enter a decimal value (0 to 15): ");
         int decimal = input.nextInt();


         if (decimal <= 15 && decimal  >= 10) {
              System.out.println("The hex value is " + (char)decimal );
         }

         else if (decimal  < 10 && decimal  >= 0) {
                 System.out.println("The hex value is " + decimal );
         }

         else {
             System.out.println("Invalid input");
        }
     }
 }

RULE1 有效但不会从数字生成 character/letter。我必须将它设置为变量吗?

更新 3:

import java.util.Scanner;

public class NewClass1 {
    public static void main(String args[]) {              
    Scanner input = new Scanner(System.in);

        System.out.print("Enter a decimal value (0 to 15): ");
        int decimal = input.nextInt();

   // RULE1
        if (decimal <= 15 && decimal  >= 10) {
            int value = decimal - 10 + 10;
             System.out.println("The hex value is " + (char)value );
        }
   // RULE2
        else if (decimal  < 10 && decimal  >= 0) {
                System.out.println("The hex value is " + decimal );
        }
   // RULE3
        else {
            System.out.println("Invalid input");
        }
    }
}

我觉得我很接近,但结果仍然对RULE1无效

更新 4:工作版本。

import java.util.Scanner;

public class NewClass {
    public static void main(String args[]) {              
    Scanner input = new Scanner(System.in);

        System.out.print("Enter a decimal value (0 to 15): ");
        int decimal = input.nextInt();


        if (decimal <= 15 && decimal  >= 10) {
            int value = ('A' + decimal - 10);
            System.out.println("The hex value is " + (char)value );
        }

        else if (decimal  <= 10 && decimal  >= 0) {
                System.out.println("The hex value is " + decimal );
        }

        else {
            System.out.println("Invalid input");
        }
    }
}

现在按预期工作。谢谢你们!谢谢范忠

而不是读入字符串:

    String decimal = input.nextLine();

// RULE1 
    char ch = decimal.charAt(0);

只读入整数:

   int ch = input.nextInt();

因为当你读入字符串时,例如,"15",结果第一个字符将是 1,同样,当输入数字大于 9 时,你的 char ch 总是 1

对于规则 1 中的更新,您需要这样做:

  System.out.println("The hex value is " + (char)(value + 'A'));

因为对于'A',等价的整数值是65,所以上面的技巧会对你有所帮助。更多详情 here

decimal.charAt(0); 会给你输入的第一个符号。因此,如果您输入“12”,结果将只是“1”。您可以使用 Scanner 类的 nextInt() 方法。

要从十进制转换为十六进制,您可以使用 Integer.toHexString() 方法。没有必要做像 +'A'...

这样的骇人听闻的事情

使用JDK,尤其是java.lang.String.parseInt(String s) and java.lang.Integer.toHexString(int i)。两者都是JDK的基本方法。不需要fiddle which chars.

String[] sa = new String[]{"-1", "0", "1", "11", "15", "31"};
for (String s : sa) {
    int i = Integer.parseInt(s);
    if (i > 0 && i <= 15) {
        System.out.println("hex(" + s + ")= " + Integer.toHexString(i));
    } else {
        System.err.println("invalid input: " + s);
    }
}

输出:

invalid input: -1
invalid input: 0
hex(1)= 1
hex(11)= b
hex(15)= f
invalid input: 31