如果您想快速找到您想要的题目,可以按快捷键【Ctrl + F】进行搜索
作业1:
求第n项的斐波那契数列值;
public class Hw01 {
public static void main(String[] args) {
System.out.println(getFibo(3));;
}
public static int getFibo(int n) {
if (n == 1 | n ==2 ) {
return 1;
}
return getFibo(n-1) + getFibo(n-2);
}
}
作业2:
小明的妈妈看到小明今天高考结束,买了一对刚刚出生的小兔子,小兔子需要3个月长大成兔,
之后每月生产一对小兔子,假如不考虑小兔子的死亡情况,求第n月共有多少对兔子?
1 1
2 1
3 1
4 2
5 3
6 4
7 6
8 9
9 13
public class Hw02 {
public static void main(String[] args) {
System.out.println(getRabbit(9));
}
public static int getRabbit(int n) {
if (n <= 4) {
return 1;
}
return getRabbit(n - 1) + getRabbit(n - 3);
}
}
作业3:
根据完整的路径从路径中分离文件路径、文件名及扩展
名
传递一个路径 c://a//b//c.avi,返回该文件的后缀名
/* 作业3:
根据完整的路径从路径中分离文件路径、文件名及扩展
名
传递一个路径 c://a//b//c.avi,返回该文件的后缀名 */
class Hw03 {
public static void main(String[] args) {
System.out.println(getFile("c://a//b//c.avi"));
}
public static String getFile(String path) {
String temp = path.trim();
temp = temp.substring(temp.lastIndexOf(".")+1);
return temp;
}
}
作业4:
输入一个字符串,判断该字符串是否是回文字符串(面试题)
123454321
/* 输入一个字符串,判断该字符串是否是回文字符串(面试题)
123454321 */
import java.util.Scanner;
class Hw04 {
public static void main(String[] args) {
System.out.print("判断回文【true/false】:");
Scanner sc = new Scanner(System.in);
String text = sc.next();
System.out.println(judge(text));
}
public static Boolean judge(String line) {
// 1221 4 0-3 0 1 2 3
if (line.length() % 2 == 0) {
for (int i = 0; i <= (line.length()-2) / 2; i++) {
if (line.charAt(i) != line.charAt(line.length() - i - 1)) {
return false;
}
}
return true;
} else {
for (int i = 0; i <= (line.length()-3) / 2; i++) {
if (line.charAt(i) != line.charAt(line.length() - i - 1)) {
return false;
}
}
return true;
}
}
}
作业5:去掉字符串中的所有空格
/* 作业5:去掉字符串中的所有空格 */
import java.util.Scanner;
class Hw05 {
public static void main(String[] args) {
System.out.print("去掉空格:");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(reSpace(text));
}
public static String reSpace(String line) {
// 定义一个新的字符串用于接受新的字符串
String temp = "";
for (int i = 0; i <= line.length() - 1; i++) {
if (line.charAt(i) == ' ') {
continue;
} else {
temp += line.charAt(i);
}
}
return temp;
}
}
作业6:
将字母全部转换为大写或小写
/* 将字母全部转换为大写或小写 */
import java.util.Scanner;
class Hw06 {
public static void main(String[] args) {
while (true) {
Scanner sc0 = new Scanner(System.in);
System.out.print("您想转成大写还是小写【大写(0)小写(1)】:");
int flag = sc0.nextInt();
if (flag == 0 | flag == 1) {
Scanner sc = new Scanner(System.in);
System.out.print("输入英文,可以转换大小写:");
// a 97 A 65
String text = sc.nextLine();
System.out.println(change(text, flag));
break;
} else {
System.out.println("输入有误,请重新输入:");
continue;
}
}
}
public static String change(String line, int flag) {
String temp = "";
if (flag == 0) {
temp = line.toUpperCase();
// for (int i = 0; i <= line.length() - 1; i++) {
// char ch = line.charAt(i);
// if (ch >= 'a' && ch <= 'z') {
// ch = (char) (ch - 32);
// }
// temp += ch;
// }
} else {
temp = line.toLowerCase();
// for (int i = 0; i <= line.length() - 1; i++) {
// char ch = line.charAt(i);
// if (ch >= 'A' && ch <= 'Z') {
// ch = (char) (ch + 32);
// }
// temp += ch;
// }
}
return temp;
}
}
作业7:接收用户输入的一句英文,将其中的单词以反序输
出,“hello c sharp”→“sharp c hello”。
/* 作业7:接收用户输入的一句英文,将其中的单词以反序输
出,“hello c sharp”→“sharp c hello”。 */
import java.util.Scanner;
class Hw07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一句英文:");
String line = sc.nextLine();
System.out.println(back(line));
}
public static String back(String line) {
String temp = "";
String[] words = line.split(" ");
for (int i = words.length - 1; i >= 0; i--) {
temp += words[i] + " ";
}
return temp.trim();
}
}
作业8:从请求地址中提取出用户名和域名
http://www.163.com?userName=admin&pwd=123456
/* 从请求地址中提取出用户名和域名
http://www.163.com?userName=admin&pwd=123456 */
class Hw08 {
public static void main(String[] args) {
String url = "http://www.163.com?userName=admin&pwd=123456";
System.out.println(userName(url));
System.out.println(urlAddres(url));
}
public static String userName(String url) {
String temp;
temp = url.substring(url.indexOf("userName")+9, url.lastIndexOf("pwd")-1); // 左闭右开
return temp;
}
public static String urlAddres(String url) {
String temp;
temp = url.substring(url.indexOf("//")+2, url.indexOf("?")); // 左闭右开
return temp;
}
}
作业9:让用户输入一句话,找出所有"呵"的位置。
// 让用户输入一句话,找出所有"呵"的位置。
import java.util.Scanner;
class Hw09 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("找出【呵】的位置:");
String line = sc.nextLine();
System.out.println(find(line));
System.out.println(search(line));
}
// 通过 indexOf()方法
public static String search(String line) {
String temp = "";
int index = 0;
while ((index=line.indexOf("呵", index)) > -1) {
temp += index +" ";
index = line.indexOf("呵", index) + 1;
}
return temp;
}
// 遍历这个字符串
public static String find(String line) {
String temp = "";
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '呵') {
temp += i +" ";
}
}
return temp;
}
}
作业10:让用户输入一句话,判断这句话中有没有邪恶,如果有邪
恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变
成”老牛很**”;
/* 让用户输入一句话,判断这句话中有没有邪恶,如果有邪
恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变
成”老牛很**”; */
import java.util.Scanner;
class Hw10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("和谐【邪恶两字】:");
String line = sc.nextLine();
System.out.println(heXie(line));
}
public static String heXie(String line) {
if (line.contains("邪恶")) {
line = line.replace("邪恶","**");
}
return line;
}
}
作业11:
上楼梯问题:某个人上楼梯,每次只能上一个台阶或者两个台阶,
那么当这个人到达第n个台阶时,共有多少种走法?
/* 上楼梯问题:某个人上楼梯,每次只能上一个台阶或者两个台阶,
那么当这个人到达第n个台阶时,共有多少种走法? */
import java.util.Scanner;
class Hw11 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("你想上多少个台阶:");
int n = sc.nextInt();
System.out.println(getLou(n));
}
public static int getLou(int n) {
switch (n) {
case 0 :
return 0;
case 1 :
return 1;
case 2 :
return 2;
}
return getLou(n-1) + getLou(n-2);
}
}
作业12:
猜字游戏,计算机随机一个0~100的整数,每次用户输出字猜,
提示如下:
猜大了,重新猜
猜小了,重新猜
猜对了
/* 猜字游戏,计算机随机一个0~100的整数,每次用户输出字猜,
提示如下:
猜大了,重新猜
猜小了,重新猜
猜对了 */
import java.util.Scanner;
class Hw12 {
public static void main(String[] args) {
int random = random(0, 100);
Scanner sc = new Scanner(System.in);
System.out.print("请输入0~100中间的整数,开始猜数游戏:");
int num = sc.nextInt();
while (judge(num, random)) {
System.out.print("继续猜:");
num = sc.nextInt();
}
}
// 生成随机数
public static int random(int min, int max) {
return (int) (Math.random() * (max - min) + min);
}
// 猜数判断
public static Boolean judge(int num, int random) {
if (num < random) {
System.out.println("小了!");
return true;
} else if (num > random) {
System.out.println("大了!");
return true;
} else {
System.out.println("恭喜你猜对了!!!!");
return false;
}
}
}
作业13:猜拳游戏,石头剪刀布。
随机数生成石头剪刀布(0:石头 1:剪刀 2:布)
/* 猜拳游戏,石头剪刀布。
随机数生成石头剪刀布(0:石头 1:剪刀 2:布)*/
import java.util.Scanner;
class Hw13 {
public static void main(String[] args) {
int random = random(0, 3);
Scanner sc = new Scanner(System.in);
System.out.print("请输入【0:石头 1:剪刀 2:布】,开始游戏:");
int num = sc.nextInt();
judgeString(num, 0);
judgeString(random, 1);
System.out.println(judge(num, random));
}
// 生成随机数
public static int random(int min, int max) {
return (int) (Math.random() * (max - min) + min);
}
// 判断石头剪刀布
public static void judgeString(int num,int flag) {
String name ;
if (flag == 0) {
name = "你";
} else {
name = "电脑";
}
switch (num) {
case 0:
System.out.println(name+"出的是【0 石头】");
break;
case 1:
System.out.println(name+"出的是【1 剪刀】");
break;
case 2:
System.out.println(name+"出的是【2 布】");
break;
}
}
// 胜负判断
public static String judge(int num, int random) {
int judge = num - random;
String temp;
if (judge == -1 || judge == 2) {
temp = "你输了!";
} else if (judge == 1 || judge == -2) {
temp = "你赢了!";
} else {
temp = "平局!!!!";
}
return temp;
}
}