Java程序练习-Web浏览
Web浏览
时间限制: 10000ms内存限制: 1024kB
描述
实现浏览器的页面前后访问机制。有四种命令:
1、BACK;
2、FORWARD;
3、VISIT:访问新的页面;
4、QUIT:退出浏览器。
请参考实际的浏览器按钮的功能。
假设浏览器打开时,显示的页面是:http://www.acm.org/
输入
一系列命令:以BACK、FORWARD、VISIT或QUIT开头。如果是VISIT,后面要跟URL,长度不超过70,且不含空格。最后总是以QUIT结尾。(最多5000个命令)
输出
对于每一个命令(除了QUIT),输出浏览页面的URL,如果命令被忽略,输出:Ignored。
样例输入
VISIT http://acm.ashland.edu/
VISIT http://asm.baylor.edu/acmipc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
样例输出
http://acm.ashland.edu/
http://asm.baylor.edu/acmipc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored
参考代码
/*
* Web Browser 2011-10-3 11:10AM Eric Zhou
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
List<String>list = new ArrayList<String>();
list.add("http://www.acm.org/");
int cur = 0;
while(true){
String s[] = cin.readLine().split(" ");
if(s[0].compareTo("QUIT") == 0)
break;
if(s[0].compareTo("BACK") == 0){
if(cur > 0){
-- cur;
System.out.println(list.get(cur));
}else{
System.out.println("Ignored");
}
continue;
}
if(s[0].compareTo("FORWARD") == 0){
if(cur != list.size() - 1){
++ cur;
System.out.println(list.get(cur));
}else{
System.out.println("Ignored");
}
continue;
}
if(s[0].compareTo("VISIT") == 0){
while(cur < list.size() - 1)
list.remove(cur + 1);
list.add(++ cur,s[1]);
System.out.println(list.get(cur));
continue;
}
}
}
}
摘自:冰非寒 blog
补充:软件开发 , Java ,