- import java.util.Scanner;
- public class Test_1 {
- /**
- * 需求:键盘录入一个随机字符串,把里面的*全部去掉,但是开头和结尾的*不能除
- */
- public static void main(String args[]) {
- // Scanner类
- System.out.println("Please input a string: "); // 这里试了一下输入String数据,但中间有空格就不能显示,Scanner类还不具有这功能
- Scanner str = new Scanner(System.in);
- char[] inputStr = str.next().toCharArray();
- StringBuffer sb = new StringBuffer();
- int[] flags = new int[inputStr.length];
- boolean flagstart = false;
- boolean flagend = false;
- for (int i = 0, len = inputStr.length; i < (len / 2) + 1; i++) {
- if (i == 0) {
- if (inputStr[i] == '*') {
- flags[i] = 1;
- flagstart = true;
- } else {
- flags[i] = 1;
- }
- if (inputStr[len - 1 - i] == '*') {
- flags[len - 1 - i] = 1;
- flagend = true;
- } else {
- flags[len - 1 - i] = 1;
- }
- } else {
- if (inputStr[i] == '*' && flagstart) {
- flags[i] = 1;
- flagstart = true;
- } else if (inputStr[i] != '*') {
- flags[i] = 1;
- flagstart = false;
- } else {
- flags[i] = 0;
- flagstart = false;
- }
- if (inputStr[len - 1 - i] == '*' && flagend) {
- flags[len - 1 - i] = 1;
- flagend = true;
- } else if (inputStr[len - 1 - i] != '*') {
- flags[len - 1 - i] = 1;
- flagend = false;
- } else {
- flags[len - 1 - i] = 0;
- flagend = false;
- }
- }
- }
- for (int j = 0, lens = inputStr.length; j < lens; j++) {
- if (flags[j] == 1) {
- sb.append(inputStr[j]);
- }
- }
- System.out.println(sb.toString());
- }
- }
复制代码 |
|