在字符串的静态方法搜索特定的字符 [英] Searching for specific character in string with static method
本文介绍了在字符串的静态方法搜索特定的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须作出计数字符串中的字母B的数量的方案。我得到的那部分了,但同时也要求我使用返回true或基于如果字符串中有我任何虚假烧烤静态方法实在看不出如何适应这一部分。
进口java.util.Scanner中;
公共类CountB {//这里写静态方法isThisB
公共静态无效的主要(字串[] args){
扫描仪键盘=新的扫描仪(System.in);
的System.out.println(输入一个字符串:);
字符串W = keyboard.nextLine();
诠释计数= 0;
的for(int i = 0; I< w.length();我++)
{
如果(w.charAt(I)=='B'|| w.charAt(I)=='B')
{
算上++;
}
}
的System.out.println(B和b的数量:+计数);
}
}
解决方案
事情是这样的:
静态布尔检查(字符串str){
如果(str.indexOf('B'),大于0 || str.indexOf('B')0)
返回true;
其他
返回false;
}
I have to make a program that counts the number of the letter B in a string. I got that part already, but it also requires me to use a static method that returns true or false based on if the string has any Bs in it and i really don't see how to fit that part in.
import java.util.Scanner;
public class CountB {
// write the static method "isThisB" here
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a string: ");
String w = keyboard.nextLine();
int count=0;
for (int i=0; i<w.length(); i++)
{
if (w.charAt(i)=='B'||w.charAt(i)=='b')
{
count++;
}
}
System.out.println("Number of B and b: "+ count);
}
}
解决方案
Something like this:
static boolean check(String str){
if(str.indexOf('B')>0 || str.indexOf('b')>0 )
return true;
else
return false;
}
这篇关于在字符串的静态方法搜索特定的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文