首页 > 代码库 > hw1打卡

hw1打卡

 修仙修仙!

PART1:给你了一部分源代码,让你根据输入建立URL,读取网站HTML的前五行内容,逆序输出。。

 1 import java.net.*;
 2 import java.io.*;
 3 
 4 /**  A class that provides a main function to read five lines of a commercial
 5  *   Web page and print them in reverse order, given the name of a company.
 6  */
 7 
 8 class OpenCommercial {
 9 
10   /** Prompts the user for the name X of a company (a single string), opens
11    *  the Web site corresponding to www.X.com, and prints the first five lines
12    *  of the Web page in reverse order.
13    *  @param arg is not used.
14    *  @exception Exception thrown if there are any problems parsing the 
15    *             user‘s input or opening the connection.
16    */
17   public static void main(String[] arg) throws Exception {
18 
19     BufferedReader keyboard;
20     String inputLine;
21 
22     keyboard = new BufferedReader(new InputStreamReader(System.in));
23 
24     System.out.print("Please enter the name of a company (without spaces): ");
25     System.out.flush();        /* Make sure the line is printed immediately. */
26     inputLine = keyboard.readLine();
27     /* Replace this comment with your solution.  */
28     URL url=new URL("http://www."+inputLine+".com"); 
29     InputStream in=url.openStream();
30     InputStreamReader ina=new InputStreamReader(in);
31     BufferedReader inb=new BufferedReader(ina);
32     String[] out=new String[5];
33     for(int i=0;i<5;i++){
34     out[i]=inb.readLine();//read strings in right way.
35     }
36     for(int j=4;j>=0;j--){
37     System.out.println(out[j]);//output strings in reverse way.
38     }
39   }
40 }

逆序输出倒不难。。就是这个网站的部分研究了半天,还有感觉这个BufferedReader甚是麻烦。。

运行结果:

技术分享

PART2:输入一串字符,去掉第二个字符并输出:

找了几种不同的键盘输入的方法:http://soft.chinabyte.com/database/191/12466191.shtml 我这里打算试试用Scanner

 

技术分享
import java.util.*;
public class Nuke2 {
 public static String process(String s,int b){
 return s.substring(0, b)+s.substring(b + 1);
 }
 
 public static void main(String[] arg) {
     
      System.out.println("Please input a string:");
      System.out.flush();
      Scanner sc=new Scanner(System.in);
      String line=sc.nextLine();
      String output=process(line);
      System.out.print(output);
 }

}
View Code

运行结果:

技术分享

hw1打卡