亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取字符并返回鍵盤等效項

獲取字符并返回鍵盤等效項

慕婉清6462132 2022-08-17 12:23:43
因此,我正在編寫代碼,該代碼將采用一個字符并返回其等效于字符的字母數字鍵盤。問題是,我在一個盒子里得到了一個問號作為回報。我已經檢查了輸入是否正確。例如,使用char 'h'輸入,我應該得到一個char '4'返回。希望有人能夠發現我的錯誤。代碼如下:public char getDigit(char letter) throws Exception{    switch (letter) {               case 'a': case 'b': case 'c': case '2':        return 2;    case 'd': case 'e': case 'f': case '3':        return 3;    case 'g': case 'h': case 'i': case '4':        return 4;    case 'j': case 'k': case 'l': case '5':        return 5;    case 'm': case 'n': case 'o': case '6':        return 6;    case 'p': case 'q': case 'r': case 's': case '7':        return 7;    case 't': case 'u': case 'v': case '8':        return 8;    case 'w': case 'x': case 'y': case 'z': case '9':        return 9;    default:        throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");    }}
查看完整描述

2 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

方法的返回類型為 char。

現在采用您的開關語句。返回介于 2 到 9 之間的 char 值?,F在看一下 ASCII 表。

驚喜:這些字符都是“不可打印”的控制字符。因此,當您打印它們時,您的控制臺會給您“?”!

如果你想要“4”,你的代碼必須返回“4”,而不是4!或 52,因為 ASCII 表中的該條目表示“4”。


查看完整回答
反對 回復 2022-08-17
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您沒有得到正確的輸出,因為您正在用作函數的返回類型。您應該使用int作為返回類型而不是char,因為在開關情況下,您正在與字符進行比較并返回數字值因此將代碼替換為以下代碼,這將起作用:chargetDigit(..)


public int getDigit(char letter) throws Exception{


switch (letter) {           

case 'a': case 'b': case 'c': case '2':

    return 2;

case 'd': case 'e': case 'f': case '3':

    return 3;

case 'g': case 'h': case 'i': case '4':

    return 4;

case 'j': case 'k': case 'l': case '5':

    return 5;

case 'm': case 'n': case 'o': case '6':

    return 6;

case 'p': case 'q': case 'r': case 's': case '7':

    return 7;

case 't': case 'u': case 'v': case '8':

    return 8;

case 'w': case 'x': case 'y': case 'z': case '9':

    return 9;

default:

    throw new IllegalArgumentException("Must be a letter or number on the Alpha-Numeric Keypad.");

}

}


查看完整回答
反對 回復 2022-08-17
  • 2 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號