hw1打卡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); } }
运行结果:
以上是关于hw1打卡的主要内容,如果未能解决你的问题,请参考以下文章