/**
* 第1题:求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
*
* @param args
*/
public static void main(String args[]) {
int n, fun;// n为第n项,fn为第n项的值
java.util.Scanner in;
while (true) {//循环输出数字
in = new Scanner(System.in);
n = in.nextInt();
if(n==0){//输入0跳出循环结束输入
break;
}
while (n >= 30 || n <= 0) {//输入范围0-30之间
System.out.println("请重新输入n的范围为 0<n<30");
in = new Scanner(System.in);
n = in.nextInt();
}
fun = function(n);
System.out.println("斐波那契数列第" + n + "项为:" + fun);
}
}
public static int function(int n) {
if (n == 1 || n == 2)
return 1;
return function(n - 1) + function(n - 2);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test2 {
/**
*
* 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如:
*
* 键盘输入6,打印6秒;//
* 键盘输入60,打印1分; //
* 键盘输入66,打印1分6秒; //
* 键盘输入666,打印11分6秒;//
* 键盘输入3601,打印1小时1秒//
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(function(60*60*24+1));// 调用函数并打印
}
// 计算时间函数
public static String function(int number) {
String strDate = "键盘输入" + number + ",打印";
int final_day = 60 * 60 * 24;// 天
int final_h = 60 * 60;// 小时
int day = number / final_day;
int h = (number % final_day) / final_h;
int min = ((number % final_day) % final_h) / 60;// 分钟
int s = ((number % final_day) % final_h) % 60;// 秒
if (day > 0) {
strDate = strDate + day + "天";
}
if (h > 0) {
strDate = strDate + h + "小时";
}
if (min > 0) {
strDate = strDate + min + "分钟";
}
if (s > 0) {
strDate = strDate + s + "秒";
}
return strDate + ";";
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test3 {
/**
* 3、 请列举您了解的一些排序算法,并用Java语言实现一个效率较高的。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[] = {1,3,5,7,9,8,6,4,2};
print(function(arr));
}
/**
* 对数组进行排序 :选择排序
* @param arr无须数组
* @return 排好序的数组
*/
public static int[] function(int[] arr) {
int len = arr.length;
//循环数组
for (int i = len - 1; i > 0; i--) {
int index = i;//保存数组小标
for (int j = 0; j < i; j++) {
//比较数组中数字大小
if (arr[j] > arr[index]) {
index = j;
}
}
//如果前面的数组大于后面的数字进行交换
if (arr[index]>arr[i]) {
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
return arr;
}
//打印数组值
public static void print(int arr[]){
for(int a: arr){
System.out.print(a + " ");
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Test4 {
/**
* 4、 什么情况下适合用UDP协议,什么情况下适合用TCP协议?
*
* @param args
*/
// TCP:
// 1.建立连接,形成传输数据的通道。
// 2.在连接中进行大数据量传输
// 3.通过三次握手完成连接,是可靠协议
// 4.必须建立连接,效率会稍低。如:打电话,下载
// UDP:
// 1.将数据及源和目的封装成数据包中,面向无连接
// 2.每个数据包的大小在限制在64k内
// 3.因无连接,是不可靠协议
// 4.不需要建立连接,速度快。如:聊天,视频会议,桌面共享。
//
}
--------------------------------------------------------------------------------------------------------------------------------------------------
public class Test5 {
/**
* 5、 分析运行结果,说明原理。(没有分析结果不得分)
*
* @param args
*
*
* 继承的基本概念:
*
(1)Java不支持多继承,也就是说子类至多只能有一个父类。
*
(2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法。
* (3)子类中定义的成员变量和父类中定义的成员变量相同时,则父类中的成员变量不能被继承。
* (4)子类中定义的成员方法,并且这个方法的名字返回类型 ,以及参数个数和类型与父类的某个成员方法完全相同,则父类的成员方法不能被继承。 //
* 分析以上程序示例,
*
1:虚拟机加载ExtendsDemo类,提取类型信息到方法区。
*
2:通过保存在方法区的字节码,虚拟机开始执行main方法,main方法入栈。
*
3:执行main方法的第一条指令,new BB();这句话就是给BB实例对象分配堆空间。因为BB继承AA父类,所以,虚拟机首先加载AA类到方法区
*
。然后加载BB类到方法区。将BB类的实例对象地址赋值给引用变量b。
*
4:调用b.fun1()方法,通过引用变量b持有的引用找到堆中的实例对象
*
,通过实例对象持有的本类在方法区的引用,找到本类的类型信息,定位到fun1()方法。fun1()方法入栈。
*
开始执行fun1()方法中的字节码,在执行中调用fun2();优先在子类方法区找所以以打印456开始。
*
5:AA a = b;原理同上,
*
5:fun1(),fun2()方法执行完毕,fun2,fun1方法出栈,程序回到main方法,main方法执行完毕出栈,主线程消亡,虚拟机实例消亡,程序结束。 //
*
总结:相同的方法会被重写,
* 变量没有重写之说,如果子类声明了跟父类一样的变量,那意味着子类将有两个相同名称的变量。一个存放在子类实例对象中
*
,一个存放在父类子对象中。父类的private变量,也会被继承并且初始化在子类父对象中,只不过对外不可见。
*/
}
class AA {
void fun1() {
System.out.println(this.fun2());
}
int fun2() {
return 123;
}
}
class BB extends AA {
int fun2() {
return 456;
}
public static void main(String args[]) {
BB b = new BB();
b.fun1();
AA a = b;
a.fun1();
}
}
---------------------------------------------------------------------------------------------------------------------
public class Test6 {
/**
* 6、 声明类Student,包含3个成员变量:name、age、score,要求可以通过 new Student("张三", 22, 95)
* 的方式创建对象, 并可以通过set和get方法访问成员变量
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class Student {
private String name;
private int age;
private int score;
public Student(String name) {
this.name = name;
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public Student( int age, int score) {
this.age = age;
this.score = score;
}
public Student(String name, int age, int score) {
this(age,score);
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
public class Test7 {
/**
* 7、 在打印语句中如何打印这3个x变量?
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new A7().new B().func();
}
}
class A7 {
int x = 1;
class B {
int x = 2;
void func() {
int x = 3;
System.out.println(A7.this.x + " : " + this.x + " : " + x);
}
}
}
-------------------------------------------------------------------------------------------------------------------
public class Test8 {
/**
* 8、 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符,例如:
*
*
* 原始字符串是"abc",打印得到下列所有组合情况:
* "a" "b" "c"
* "ab" "bc" "ca" "ba" "cb" "ac" "abc"
* "acb" "bac" "bca" "cab" "cba"
*
* @author Administrator
*
*/
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String str = s.next();
System.out.println(str);
s.close();
show(str);
}
/**
* @param str
* str是给定的字符串
*/
private static void show(String str) {
TreeSet<String> set = saveInSet(new StringBuilder(str), 0,
new StringBuilder(), new TreeSet<String>());
for (String s : set) {
System.out.println(s);
}
}
/**
* 返回集合,集合包含字符串所有字符的可能组合
*
* @param str
* 给定字符串转换成的StringBuilder对象,主要是为了操作字符方便
* @param count
* 计数,对第count进行排列组合
* @param buff
* 暂存放某种可能
* @param set
* 集合,去除重复元素,例如"aab"以第一个a开头会有aba,以第二个a开头也会有aba
* @return 返回TreeSet集合
*/
private static TreeSet<String> saveInSet(StringBuilder str, int count,
StringBuilder buff, TreeSet<String> set) {
for (int i = 0, len = str.length(); i < len; i++) {
// 获取字符
char c = str.charAt(i);
// 去掉原字符串的某个字符(保证某个字符不被重复利用)
str.deleteCharAt(i);
// 缓存添加该字符
buff.append(c);
// 将该种可能组合存入集合
set.add(buff.toString());
// str仍包含字符,则递归调用,开始取第二位字符
// 若还有第三位则继续递归……以此类推
if (str.length() != 0) {
// count用于记录目前在进行排列组合的第count位
count++;
// 递归
saveInSet(str, count, buff, set);
// 第n位递归结束后,需要继续对n-1位排列,位数-1
count--;
}
// 递归结束后,需要继续对n-1位排列,因此清除第n位的记录
buff.deleteCharAt(count);
// 删除的字符插回str
str.insert(i, c);
}
// 返回集合
return set;
}
}
----------------------------------------------------------------------------------------------------------------
public class Test9 {
/**
* 9、 编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
File from = new File("C:\\Users\\Administrator\\Desktop\\java");
File to = new File("C:\\Users\\Administrator\\Desktop\\java\\copy");
if (!to.exists())
to.mkdir();
ArrayList<File> al = new ArrayList<File>();
getFile(from, al);
BufferedInputStream bufi = null;
BufferedOutputStream bufo = null;
for (File file : al) {
bufi = new BufferedInputStream(new FileInputStream(file));// 输入流
String s = file.toString()
.substring(file.toString().lastIndexOf("\\"))
.replace(".java", ".txt");// 获取文件名并进行后缀名的修改
bufo = new BufferedOutputStream(new FileOutputStream(
new File(to, s)));// 输出流
byte[] buf = new byte[1024 * 1024];
int len = 0;
while ((len = bufi.read(buf)) != -1) {
bufo.write(buf, 0, len);
bufo.flush();
}
bufi.close();
bufo.close();
}
}
public static void getFile(File from, ArrayList<File> al)// 递归函数,获取所有.java的File.
{
File[] files = from.listFiles();
for (File file : files) {
if (file.isDirectory())
getFile(file, al);
else {
if (file.toString().endsWith(".java"))
al.add(file);
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------
public class Test10 {
/**
* 10、
* 一位老农带着猫、狗、鱼过河,河边有一条船,每次老农只能带一只动物过河。当老农不和猫狗鱼在一起时,狗会咬猫,猫会吃鱼,当老农和猫狗鱼在一起时,
* 则不会发生这种问题。编程解决猫狗鱼过河问题。
*/
static List<String> there = new ArrayList<String>();
static List<String> here = new ArrayList<String>();
public static void main(String[] args) {
here.add("cat");
here.add("dog");
here.add("fish");
Test10 test10 = new Test10();
test10.take();
}
// 判断动物是否安全
public boolean isSafty(List<String> list) {
// 如果一个集合中同时出现了猫和狗,猫和鱼的都认为不安全
if (list.add("dog") && list.add("cat") || list.add("cat")
&& list.add("fish")) {
return false;
}
return true;
}
public void take() {
// 得到要带走的动物
String anim = here.get(0);
// 从剩下的集合中删除
here.remove(here.get(0));
if (anim.equals("cat")) {
// 添加到以带过去的集合里面
there.add(anim);
// 继续带下一个动物
if (here.isEmpty()) {
System.out.println();
System.out.println("最后在把" + anim + "带走了");
return;
} else {
System.out.println("带走了" + anim);
System.out.print("剩下:");
for (String s : here) {
System.out.print(s + " ");
}
System.out.println();
System.out.println("----------------------------");
take();
}
} else if (anim.equals("dog")) {
there.add(anim);
System.out.println("然后把" + anim + "带走");
// 判断是否安全,之后继续带下一个动物
if (isSafty(there)) {
take();
} else {
String animal = there.get(0);
there.remove(animal);
here.add(animal);
System.out.println("然后把" + animal + "带回");
// 继续带下一个动物
take();
}
} else if (anim.equals("fish")) {
System.out.print("之后把" + anim + "带走");
take();
}
}
}