- 戒骄戒躁!
- 不要小看任何一个知识点!
书写一个类,类名为Itheima;
类中有一个方法,方法名bobThere;
给你一个字符串,若是包含bob就返回true
注意若是bob中的字符o,能够是任何字符
例如:bob返回true, bpb返回true正则表达式
方法调用 | 指望值 |
---|---|
bobThere("abcbob") | true |
bobThere("b9b") | true |
bobThere("bac") | false |
public class Itheima { public boolean bobThere(String str){ String regex = "b.b"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); if (m.find()) { return true; } else { return false; } } }
书写一个类,类名为Itheima;类中有一个方法,方法名countYZ;数组
给定一个字符串,找出里面以“y”或者“z”结尾的单词的个数,也就是说,跟在“y”或者“z”后面的字符不是英文单词。
“y”在“heavy”中和“z”在“fez”中计数,而“y”在“yellow”中不计数,(不区分大小写)app
方法调用 | 指望值 |
---|---|
countYZ("fez day") | 2 |
countYZ("day fez") | 2 |
countYZ("day fyyyz") | 2 |
public class Itheima { public int countYZ(String str){ int count = 0; String str1 = str + " "; char[] c = str1.toCharArray(); for (int i = 0; i < c.length - 1; i++) { if(c[i] == 'y' || c[i] == 'z' || c[i] == 'Y' || c[i] == 'Z'){ char t = c[i + 1]; if(!((t >= 'a' && t <= 'z') || (t >= 'A' && t <= 'Z'))){ count++; } } } /* if(c[c.length - 1] == 'y' || c[c.length - 1] == 'z' || c[c.length - 1] == 'Y' || c[c.length - 1] == 'Z'){ count++; }*/ return count; } }
书写一个类,类名为Itheima;
类中有一个方法,方法名makePi;
返回一个包含pi(参考Math.PI)的前n位数字的整数数组长度,n为方法接收的参数。
例如:n为3,则返回{3,1,4}。oop
方法调用 | 指望值 |
---|---|
makePi(1) | [3] |
makePi(2) | [3,1] |
makePi(3) | [3,1,4] |
public class Itheima { public int[] makePi(int n) { String str = Math.PI + ""; String str2 = str.replaceAll("\\.", ""); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(str2.charAt(i)+""); } return arr; /*int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = (int) (Math.PI * Math.pow(10, i) % 10); } return arr;*/ } }
书写一个类,类名为Itheima;code
类中有一个方法,方法名gHappy;索引
若是字符串中的’g’的相邻左边或者右边有另一个’g’,则’g’在这个字符串中是happy的,
若是字符串中全部的’g’都是happy的则返回true,不然返回false。rem
方法调用 指望值
gHappy("xxggxx") true
gHappy("xxgxx") false
gHappy("xxggyygxx") false字符串
public class Itheima { public boolean gHappy(String str){ boolean sumFlag = true; boolean flag = true; boolean hasG = false; String newStr = " " + str + " "; for (int i = 1; i < newStr.length() - 1; i++) { char cBefore = newStr.charAt(i - 1); char c = newStr.charAt(i); char cLater = newStr.charAt(i + 1); if(c == 'g'){ if(c == cBefore || c == cLater){ flag = true; } else { flag = false; } hasG = true; } sumFlag = sumFlag && flag; } return sumFlag && hasG; } }
书写一个类,类名为Itheima;get
类中有一个方法,方法名maxBlock;string
给定一个字符串,返回该字符串中连续出现个数最多的字符的数量。
方法调用 | 指望值 |
---|---|
maxBlock("hoopla") | 2 |
maxBlock("abbCCCddBBBxx") | 3 |
maxBlock("") | 0 |
int count = 1;//用于做为中间变量,记录正在遍历的字符的个数 HashMap<Character,Integer> map = new HashMap<>();//Key:字符 Value:最大连续出现次数 for (int i = 0; i < str.length() - 1; i++) { char c1 = str.charAt(i); char c2 = str.charAt(i + 1); map.put(c1,count); if(c1 == c2){ count++; if(count > map.get(c1)){ map.put(c1, count); } } else { count = 1; } } int totalMax = 0; for (Character c : map.keySet()) { if(map.get(c) >= totalMax){ totalMax = map.get(c); } } return totalMax;
书写一个类,类名为Itheima;
类中有一个方法,方法名sum13;
有一个整型数组,返回的结果是数组全部元素的总和,若是数组中出现整型数13的时候,
那么元素13和13后面的一个元素也不计入总和里面。
方法调用 | 指望值 |
---|---|
sum13([1,2,2,1]) | 6 |
sum13([1,1]) | 2 |
sum13([1,2,2,1,13]) | 6 |
public class Itheima { public int sum13(int[] arr){ HashSet<Integer> hs = new HashSet<>(); int sum = 0; for (int i = 0; i < arr.length; i++) { if(arr[i] == 13){ hs.add(i); hs.add(i + 1); } } for(int i = 0; i < arr.length; i++){ if(hs.contains(i)){ continue; } sum += arr[i]; } return sum;
书写一个类,类名为Itheima;
类中有一个方法,方法名sum67;
有一个整型数组,返回数组中的数字的总和,若是数组含有数6和7
那么忽略不计入从6开始并延伸到有7的数字段,7后面的数字须要参与运算(每6个将跟随至少一个7)。
返回0表示没有数字。
方法调用 | 指望值 |
---|---|
sum67([1,2,2]) | 5 |
sum67([1,2,2,6,99,99,7]) | 5 |
sum67([1,1,6,7,2]) | 4 |
public class Itheima { public int sum67(int[] arr){ boolean switch67 = true; boolean sumFlag = true; int sum = 0; for (int i = 0; i < arr.length; i++) { if(arr[i] == 6 && switch67){ switch67 = false; sumFlag = false; } if(arr[i] == 7 && !switch67){ switch67 = true; sumFlag = true; sum -= 7; } if(sumFlag){ sum += arr[i]; } } return sum;
书写一个类,类名为Itheima;
类中有一个方法,方法名withoutString;
给定两个字符串参数base和remove,返回删除了remove字符串的base字符串(不区分大小写),
而且返回的base字符串不含有remove的重叠事例。
例如:("This is a FISH", "IS") -> "Th a FH" (注意Th和a之间有两个空格哦)
注意: 指望值里的一个空格能够表明多个空格.
方法调用 | 指望值 |
---|---|
withoutString("Hello there","llo") | "He there" |
withoutString("Hello there","e") | "Hllo thr" |
withoutString("Hello there","x") | "Hello there" |
public class Itheima { public String withoutString(String base,String remove){ for (int i = 0; i < base.length() - remove.length(); i++) { String str = base.substring(i,remove.length()+ i); if(str.equalsIgnoreCase(remove)){ base = base.replace(str, ""); } } return base;