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); } }
輸出結果: