天天看點

藍橋-第一個數字

以下的靜态方法實作了:把串s中第一個出現的數字的值傳回。

如果找不到數字,傳回-1

例如:

s = "abc24us43"  則傳回2

s = "82445adb5"  則傳回8

s = "ab"   則傳回-1   

public static int getFirstNum(String s)

{

if(s==null || s.length()==0) return -1;

char c = s.charAt(0);

if(c>='0' && c<='9') return _____________;  //填空

return ___________________;  //填空

}

請分析代碼邏輯,并推測劃線處的代碼。

答案寫在 “解答.txt” 檔案中

注意:隻寫劃線處應該填的内容,劃線前後的内容不要抄寫。

public class Test 
{   public static int getFirstNum(String s)
{
	if(s==null || s.length()==0) return -1;
	
	char c = s.charAt(0);
	if(c>='0' && c<='9') return c-'0' ;  //填空
	
	return getFirstNum(s.replaceFirst("^\\w", ""));  //填空
}
    //start 提示:自動閱卷起始唯一辨別,請勿删除或增加。 
    public static void main(String args[]) 
    { 
        int[] q=new int[10];
    	System.out.print(new Test().getFirstNum("afag3af"));
    } 
    //end //提示:自動閱卷結束唯一辨別,請勿删除或增加。
}