java 保存输入的字符 只输出s开头的字符

来源:百度知道 编辑:UC知道 时间:2024/06/03 19:30:57
创建一个名字为ex2的application,在此应用程序里,创建一个叫Exam2的类,该类反复要求用户输入若干个字符串,每输入一个字符串,它将被保存下来直到用户输入字符串quit(或Quit或QUit或QUIt或QUIT或这个单词任何大小字的组合)为止。然后,在屏幕上只输出以字母s或S开头的字符串,输出时应以这些字符串输入的逆序输出。
提示:

您的application class 应以以下形式开头:
import java.util.*;
public class Exam2 {

在这个class里面,请执行以下方法:
public static void main (String args[])

您可以用一个栈(Stack)存储您的字符串(String)

说白了 就是把输入的所有字符保存后只输出有关S打头的字符
例子:
Please enter a String>hello there
Please enter a String>what is your name?
Please enter a String>my name is Fred
Please enter a String>sit down please
Please enter a String>where should I sit?
Please enter a String>Somewhere near here
Please enter a String>OK I will sit on the sofa
Please enter a String>sitting there is fine
Please enter a String>it is time to quit now
Please enter a String>bye
Please enter a String>qUIt
输出:
sitting there is fine
Somew

import java.util.*
import java.io.*
public class Exam {
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new BufferedInputStream(System.in()));
System.out.println("Please Enter a String>");
String s = reader.readLine();
List<String> list = new ArrayList<String>();
while(1){
if(s.equals("#")) {
break;
}
System.out.println("Please Enter a String>");
String s = reader.readLine();
list.add(s);
}
reader.close();
for(Iterator iter=list.iterator(); iter.hasnext;) {
String str = (String)iter.next();
if(str.startWith("s")) {
System.out.println(str);
}
}
}
}