黑马程序员技术交流社区
标题:
java入学基础试题。。。
[打印本页]
作者:
zuopiezi
时间:
2015-6-11 14:36
提示:
该帖被管理员或版主屏蔽
作者:
zuopiezi
时间:
2015-6-11 14:37
2、 请列举您了解的一些排序算法,并用Java语言实现一个效率较高的。
答:冒泡排序、直接选择排序、快速排序、堆排序、直接插入排序、折半插入排序
public class Test2 {
public static void bubbleSotr(int [] array)
{
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if (array[i]>array[j]) {
int temp = array[i];
array[i] =array[j];
array[j] = temp;
}
}
}
}
}
复制代码
作者:
zuopiezi
时间:
2015-6-11 14:38
3、判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
public class Test3 {
public static void main(String[] args)
{
String s="abdcdba"; //定义一个字符串
show(s);
}
public static String reverseString(String s)
{
char[] chs = s.toCharArray();
reverse(chs);
return new String(chs);
}
private static void reverse(char[] arr)
{
for (int start =0,end=arr.length-1;start<end ;start++,end-- )
{
swap(arr,start,end);
}
}
private static void swap(char[] arr,int x,int y)
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public static void show(String s)
{
if (s.equals(reverseString(s)))
System.out.println("对称");
else if(s.length()%2!=1)
System.out.println("字符串有问题");
else
System.out.println("不对称");
}
}
复制代码
作者:
zuopiezi
时间:
2015-6-11 14:41
4、 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,
例如: 键盘输入6,打印6秒;
键盘输入60,打印1分;
键盘输入66,打印1分6秒;
键盘输入666,打印11分6秒;
键盘输入3601,打印1小时1秒
public class Test4 {
public static void main(String[] args)
{
int second,minute,hour,day;
Scanner in = new Scanner(System.in);//键盘录入
int time;
//循环读键盘输入的一个整数
System.out.print("请输入一个整数(0表示退出):");
while((time = in.nextInt()) != 0)
{
if(time < 60)
{
second = time;
System.out.println(second+"秒");
}
else if(time>=60 && time < 3600)
{
minute = time/60;
second = time%60;
System.out.println(minute+"分"+second+"秒");
}
else if(time>=3600 && time < 216000)
{
hour = time/3600;
minute = (time%3600)/60;
second = time%60;
System.out.println(hour+"时"+minute+"分"+second+"秒");
}
else if(time >= 216000)
{
day = time/216000;
hour = (time%216000)/3600;
minute = (time%3600)/60;
second = time%60;
System.out.println(day+"天"+hour+"时"+minute+"分"+second+"秒");
}
else
System.out.println("输入数据有误,请输入整数");
}
}
}
复制代码
作者:
zuopiezi
时间:
2015-6-11 14:42
5、 使用带缓冲功能的字节流复制文件。
public class Test5 {
public static void main(String[] args)
{
BufferedInputStream bufis =null;
BufferedOutputStream bufos= null;
try {
bufis = new BufferedInputStream(new FileInputStream("c:\\1.txt"));
bufos = new BufferedOutputStream(new FileOutputStream("D:\\2.txt"));
int line = 0;
while((line=bufis.read())!=-1)
{
bufos.write(line);
}
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("文件拷贝失败");
}
finally
{
try {
if(bufis!=null)
bufis.close();//关闭读取流,并处理异常
} catch (Exception e2) {
// TODO: handle exception
throw new RuntimeException("文件读取关闭失败");
}
try {
if(bufis!=null)
bufos.close();//关闭写入流,并处理异常
} catch (Exception e2) {
// TODO: handle exception
throw new RuntimeException("文件写入关闭失败");
}
}
}
}
复制代码
作者:
zuopiezi
时间:
2015-6-11 14:59
10、使用TCP协议完成一个客户端一个服务器。 客户端从键盘输入读取一个字符串,发送到服务器。
服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。
客户端代码
public class Test10_Client {
public static void main(String[] args)throws Exception
{
Socket s = new Socket( "192.168.0.106" , 8000); // 创建 Socket 对象;
BufferedReader buf = new BufferedReader(
new InputStreamReader(System. in )); // 从键盘获取输入流到缓冲区;
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream())
); // 创建一个输出流
BufferedReader bufIn = new BufferedReader( new InputStreamReader(
s.getInputStream())); // 获取输入流 …… ;
String line = null ;
while ((line = buf.readLine()) != null ) {
if ( "over" .equals(line))
break ;
bufout.write(line);
bufout.newLine();
bufout.flush();
String str=bufIn.readLine();
System.out.print( "server:" +str);
}
s.close();
}
}
复制代码
服务器端:
public class Test10_Server {
public static void main(String []agrs) throws Exception
{
ServerSocket ss= new ServerSocket(8000);//创建ServerSocket对象
Socket s= ss.accept();
String ip = s.getInetAddress().getHostAddress();//获取本地Ip
System.out.println(ip+ "----------------------- connected!" );
BufferedReader buf = new BufferedReader( new InputStreamReader(s.getInputStream()));//创建输入流
BufferedWriter bufout= new BufferedWriter( new OutputStreamWriter(s.getOutputStream())
); // 创建一个输出流
String line= null ;
while ((line=buf.readLine())!= null ){
System.out.println(line);
StringBuffer re= new StringBuffer(line); // 字符串反转处理
String str=re.reverse().toString();
System.out.println(re+ "......" +str); // 服务器本地输出
bufout.write(str); // 将字符反转并输出到客户端
bufout.newLine();
bufout.flush();
}
s.close();
}
}
复制代码
作者:
meng12
时间:
2015-6-11 15:31
赞一个,楼主太给力啦!
作者:
zuopiezi
时间:
2015-6-11 15:44
其他的都太简单了。就没往上复制了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2