在句子中找出一個詞,比如用戶輸入了 "My friend is a cowboy"。應用就能根據數(shù)組檢測出 cowboy。String[] words = {"cowboy", "animal", "monster"};代碼: String[] words = {"cowboy", "animal", "monster"};
Boolean b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById (R.id.editText1);
view = (TextView) findViewById (R.id.textView1);
ok = (Button) findViewById (R.id.button1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String string = text.getText().toString();
b = string.indexOf("cowboy") > 0;
view.setText(b.toString());
}
});
}但是給出的結果不對。
2 回答

慕村225694
TA貢獻1880條經驗 獲得超4個贊
String[] words = {"cowboy", "animal", "monster"}; String s = "My friend is a Cowboy"; boolean check = false; for (int i = 0; i < words.length; i++) { if (s.toLowerCase().contains(words[i].toLowerCase())) { check = true; } else { } } if (check) { System.out.println("Yes"); } else { System.out.println("No"); }
添加回答
舉報
0/150
提交
取消