Java十进制到十六进制程序

时间:2015-02-10 07:40:59

标签: java netbeans

问题:(十进制到十六进制)编写一个程序,提示用户输入一个整数 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个小时了。没有讽刺的评论,因为如果我知道如何解决这个问题,我就不会在这里。我需要一些帮助来理解错误。

UPDATE2:      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();


         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有效,但不会产生任何字符/字母。我必须将其设置为变量吗?

UPDATE3:

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

仍然无效

UPDATE4:工作版。

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

现在按预期工作。谢谢你们!谢谢Pham Trung。

3 个答案:

答案 0 :(得分:2)

使用JDK,尤其是java.lang.String.parseInt(String s)java.lang.Integer.toHexString(int i)。两者都是JDK的基本方法。没有必要弄弄char s。

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

答案 1 :(得分:1)

而不是读入字符串:

    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

答案 2 :(得分:0)

decimal.charAt(0);将为您提供输入的第一个符号。因此,如果您输入&#34; 12&#34;,结果将只是&#34; 1&#34;。您可以使用nextInt()类的Scanner方法。

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

这样的hacky事情