2015-02-10 104 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