Re: [問題] String 取特定的字
String str = "101110";
int count = 0;
for(int i=0 ; i<str.length() ; i++){
if(str.charAt(i) == '1')
count = count + 1;
}
System.out.println("Your number include " + count+ " 1.");
※ 引述《blackall (犽★)》之銘言:
: 要讀取二進位數值後
: 要show出裡面共有幾個1
: 在判斷二進位數值後 若不是二進位數值要重新輸入
: 這邊都還ok
: 但是要算我的字串有幾個1的時候我就卡住了=^=
: 我想到是用IndexOf判斷有沒有1的這個數
: 之後就卡死@@" 判斷有沒有1之後我要怎麼樣count有幾個1
: EX:
: while(inputNumber.contains("1") && inputNumber.contains("0") )
: {
: while(inputNumber.indexOf("1") > 0)
: {
: count++;
: System.out.println("Your number include " + count + " 1. ");
: }
: }
: ~
: ~
: ~
: ~
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.131.84.234
討論串 (同標題文章)