2015-02-10 105 views
-1

问题:(十进制到十六进制)编写一个程序,提示用户输入 0和15之间的整数,并显示其相应的十六进制数。下面是一些样品运行:Java十进制到十六进制程序

输入一个十进制值(0至15):11 的十六进制值是乙

输入一个十进制值(0至15):5 的十六进制值是5

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

以下是我的代码。 1.我并不真正了解charAt(0),也不知道我做错了什么。 1-9 = 1-9和10-15 = A-F。我只能使用代码中看到的内容。没有特别的toHexStringscasesarrays。这是基础知识的基础。我不明白为什么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"); 
     } 
    } 
} 

我觉得我接近,但结果仍然是无效的规则1

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。

+0

你现在如何看待'charAt'呢? – immibis 2015-02-10 07:43:22

+0

我'想'是假设召唤一个与其数字对应的字符/符号。 – 2015-02-10 08:37:33

+0

不,它会返回字符本身。所以“15”.charAt(0)='1',但'1'不是1. – immibis 2015-02-10 08:48:10

回答

1

而是在一个String读:

String decimal = input.nextLine(); 

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

刚才读的整数:

int ch = input.nextInt(); 

因为,当你作为字符串,例如, “15” 读入,作为结果,第一个字符将是1,类似地,每当输入数字大于9时,您的char ch始终为1

对于规则1中的更新,您n EED做到这一点:

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

因为,对于“A”,相当于整数值是65,因此上述技巧会帮助你。更多详情here

+0

哦......这就解决了这个问题。借给我一些时间来重做,并找出是否有其他路障块 – 2015-02-10 07:50:46

+0

如果您可以请看一看,我已经更新了我的回复。 TY。 – 2015-02-10 08:16:39

+0

@ChrisRedfieldea更新我的答案:) – 2015-02-10 08:19:44

0

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

要从十进制转换为十六进制,可以使用Integer.toHexString()方法。没有必要做类似+'A'的黑客东西...

+0

谢谢你为我清理。借给我一些时间去重新考虑是否还有其他障碍。谢谢你,先生。 – 2015-02-10 07:51:46

+0

我已经更新了我的回复,如果你可以请看一看。 TY。 – 2015-02-10 08:14:39

+0

我已经更新了我的答案。 – 2015-02-10 08:41:16

2

使用JDK,尤其是和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 
+0

“没有特殊的'toHexStrings''cases'或'arrays'。”我不太了解书中的内容以便理解'parse' – 2015-02-10 07:48:52

+0

我讨厌那些不允许你使用甚至是最基本工具的问题:( – 2015-02-10 07:50:22

+0

@ChrisRedfieldea如果你使用'Scanner',只需使用'nextInt ' – 2015-02-10 07:50:33