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);

}

}

 

 

輸出結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM