博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hw1打卡
阅读量:7191 次
发布时间:2019-06-29

本文共 2136 字,大约阅读时间需要 7 分钟。

 修仙修仙!

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), opens11    *  the Web site corresponding to www.X.com, and prints the first five lines12    *  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:输入一串字符,去掉第二个字符并输出:

找了几种不同的键盘输入的方法: 我这里打算试试用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

运行结果:

转载于:https://www.cnblogs.com/jxtang/p/7174262.html

你可能感兴趣的文章
采药(动态规划背包问题总结)
查看>>
exec与xargs区别
查看>>
Linux 实现服务器之间时间同步
查看>>
python时间模块time详解
查看>>
jquery的outerHeight,outerWidth方法
查看>>
YII2表单中上传单个文件
查看>>
node.js中 express + multer 处理文件上传
查看>>
new 到底发生了什么
查看>>
Centos 32位兼容库安装
查看>>
php验证手机号记录
查看>>
C# 多态和接口
查看>>
2017 计蒜之道 初赛 第五场 C. UCloud 的安全秘钥(中等)
查看>>
CodeForces 660D Number of Parallelograms
查看>>
Ajax.BeginForm 防止跳转到新页面
查看>>
C# 将扁平状数据链接成树状结构的通用方法
查看>>
Visual Studio-IIS Express 支持局域网访问配置
查看>>
关于unity里pbr技术和材质 unity5默认shader和传统的对比
查看>>
前端性能监控方案window.performance 调研(转)
查看>>
Simotion 监控问题:Could not add self-signed certificate to certificate store.
查看>>
常微分方程_阿诺尔德 1.1节,问题6 擴張相空間沿時間軸的平移變換將積分曲線變爲積分曲線...
查看>>