首页 > 代码库 > CS61b homework1

CS61b homework1

Programe1:根据输入建立URL,读取其网站前五行内容并输出,代码:

import java.io.*;
import java.net.URL;
public class programe1{
public static String reverse(String s){
int len=s.length();
StringBuffer sb=new StringBuffer(len);
for(int i=(len-1);i>=0;i--){
sb.append(s.charAt(i));
}
return sb.toString();
}//reverse String方法,感觉比较笨,不过也确实想不出其他的了.。。。
public static void main(String[]args) throws IOException{
String inputLine;
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the name of a company (without spaces): ");
System.out.flush();
inputLine=keyboard.readLine();
URL url=new URL("http://www."+inputLine+".com/");
InputStreamReader streamreader=new InputStreamReader(url.openStream());
BufferedReader webReader=new BufferedReader(streamreader);
for(int i=0;i<5;i++){
String a=webReader.readLine();
String b=reverse(a);
System.out.println(b);
}

}
}

运行结果:技术分享

Programe2:输入String,去掉其第二个元素输出:

代码:

import java.io.*;

public class Programe2 {
public static String change(String s){
char[]c=s.toCharArray();
String s1="";
s1+=c[0];
for(int i=2;i<c.length;i++)
s1+=c[i];
return s1;
}
public static void main(String[]args) throws IOException{
BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your words: ");
System.out.flush();
String s=keyboard.readLine();
String output=change(s);
System.out.print(output);

}

}

输出结果:

技术分享

 

 

CS61b homework1