本文介紹了如何在 Java 中將 char 轉換為 int?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
(我是 Java 編程新手)
(I'm new at Java programming)
例如:
char x = '9';
我需要得到撇號中的數字,即數字 9 本身.我嘗試執行以下操作,
and I need to get the number in the apostrophes, the digit 9 itself. I tried to do the following,
char x = 9;
int y = (int)(x);
但是沒有用.
那么我應該怎么做才能得到撇號中的數字呢?
So what should I do to get the digit in the apostrophes?
推薦答案
排列ASCII表,使得字符'9'
的值比'的值大90'
;字符'8'
的值比'0'
的值大8;等等.
The ASCII table is arranged so that the value of the character '9'
is nine greater than the value of '0'
; the value of the character '8'
is eight greater than the value of '0'
; and so on.
所以你可以通過減去'0'
得到一個十進制數字char的int值.
So you can get the int value of a decimal digit char by subtracting '0'
.
char x = '9';
int y = x - '0'; // gives the int value 9
這篇關于如何在 Java 中將 char 轉換為 int?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!