目录
1、输出斐波那契数列数列的前20项
2:随机产生一个字母,如果是大写字母则输出“yes”,否则输出“no”
3:随机产生一个字符,判断:如果为大写字母则输出“大写字母”;如果为小写字母则输出“小写字母”;否则怎输出“其他字符”
4:李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗,五遇花和店,喝光壶中酒,试问李白壶中原有多少斗酒?(使用for循环结构编程实现)
5:求两个整数的最大公约数和最小公倍数
6:将100元兑换为1元、5元、10元的零钱,请问有多少种兑换方法?
7:用100元人民币兑换10元,5元,1元的纸币(每种都要有)共50张,计算有多少种兑换方案
8:设计Java程序,假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
1、输出斐波那契数列数列的前20项
public class Exer {
public static void main(String[] args) {
int num1 = 0;//斐波那契数列数列的第1项
int num2 = 1;//斐波那契数列数列的第2项
System.out.println(num1);
System.out.println(num2);
int num3;//斐波那契数列数列的第3项
int num = 2;//记录斐波那契数列数列的总项数,第1项第2项已经存在
while(true) {
num3 = num1 + num2;
num1 = num2;
num2 = num3;
num++;
if(num > 20) {
break;
}
System.out.println(num3);
}
}
}
2:随机产生一个字母,如果是大写字母则输出“yes”,否则输出“no”
public class Exer {
public static void main(String[] args) {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] c = s.toCharArray(); //String类型转换为char型数组
Random random = new Random();
int i = c[random.nextInt(c.length)];
char ch = (char)i;
System.out.println(ch);
if(i >= 65 && i <= 90) {
System.out.println("yes");
}
else if(i >= 97 && i <= 122) {
System.out.println("no");
}
}
}
3:随机产生一个字符,判断:如果为大写字母则输出“大写字母”;如果为小写字母则输出“小写字母”;否则怎输出“其他字符”
public class Exer {
public static void main(String[] args) {
Random random = new Random();
int i = random.nextInt(128);
char ch = (char)i;
System.out.println(ch);
if(i >= 65 && i <= 90) {
System.out.println("大写字母");
}
else if(i >= 97 && i <= 122) {
System.out.println("小写字母");
}
else {
System.out.println("其他字符");
}
}
}
4:李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗,五遇花和店,喝光壶中酒,试问李白壶中原有多少斗酒?(使用for循环结构编程实现)
public class Exer {
public static void main(String[] args) {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] c = s.toCharArray(); //String类型转换为char型数组
Random random = new Random();
int i = c[random.nextInt(c.length)];
char ch = (char)i;
System.out.println(ch);
if(i >= 65 && i <= 90) {
System.out.println("yes");
}
else if(i >= 97 && i <= 122) {
System.out.println("no");
}
}
}
3:随机产生一个字符,判断:如果为大写字母则输出“大写字母”;如果为小写字母则输出“小写字母”;否则怎输出“其他字符”
public class Exer {
public static void main(String[] args) {
Random random = new Random();
int i = random.nextInt(128);
char ch = (char)i;
System.out.println(ch);
if(i >= 65 && i <= 90) {
System.out.println("大写字母");
}
else if(i >= 97 && i <= 122) {
System.out.println("小写字母");
}
else {
System.out.println("其他字符");
}
}
}
4:李白无事街上走,提壶去买酒。遇店加一倍,见花喝一斗,五遇花和店,喝光壶中酒,试问李白壶中原有多少斗酒?(使用for循环结构编程实现)
提示:采用逆向思维分析问题。
public class Exer {
public static void main(String[] args) {
//采用逆向思维分析问题。
//李白行走顺序店花店花店花店花店花
double sum = 0;
boolean flag = true;
for(int i = 1; i<= 10; i++) {
if(flag) {
sum += 1.0;//遇见花喝1,则加1
flag = false;
}
else {
sum /= 2.0;//遇见店乘2,则除2
flag = true;
}
}
System.out.println(sum);
}
}
5:求两个整数的最大公约数和最小公倍数
public class Exer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数:");
int num1 = scanner.nextInt();
int n1 = num1;//把数存储下来,用来求最小公倍数
System.out.println("请输入第二个数:");
int num2 = scanner.nextInt();
int n2 = num2;//把数存储下来,用来求最小公倍数
int approximateNumber;
while(true) {
approximateNumber = num1 % num2;
num1 = num2;
num2 = approximateNumber;
if(num2 == 0) {
System.out.println("最大公约数为:" + num1);
break;
}
}
int commonMultiple = n1 * n2 / num1;
System.out.println("最小公倍数是:" + commonMultiple);
}
}
6:将100元兑换为1元、5元、10元的零钱,请问有多少种兑换方法?
public class Exer {
public static void main(String[] args) {
int num = 0;
for(int i = 0; i <= 10; i ++) {
for(int j = 0; j <= 20; j++) {
for(int k = 0; k <= 100; k++) {
if(10 * i + 5 * j + k == 100) {
System.out.print("十元有"+ i + "张");
System.out.print("五元有"+ j + "张");
System.out.print("一元有"+ k + "张");
System.out.println();
num++;
}
}
}
}
System.out.println("一共有" + num + "种兑换方法");
}
}
7:用100元人民币兑换10元,5元,1元的纸币(每种都要有)共50张,计算有多少种兑换方案
public class Exer {
public static void main(String[] args) {
int num = 0;
for(int i = 1; i <= 10; i ++) {
for(int j = 1; j <= 20; j++) {
for(int k = 1; k <= 100; k++) {
if(10 * i + 5 * j + k == 100 && i+j+k == 50) {
System.out.print("十元有"+ i + "张");
System.out.print("五元有"+ j + "张");
System.out.print("一元有"+ k + "张");
System.out.println();
num++;
}
}
}
}
System.out.println("一共有" + num + "种兑换方法");
}
}
public class Exer {
public static void main(String[] args) {
int num = 0;
for(int i = 0; i <= 10; i ++) {
for(int j = 0; j <= 20; j++) {
for(int k = 0; k <= 100; k++) {
if(10 * i + 5 * j + k == 100) {
System.out.print("十元有"+ i + "张");
System.out.print("五元有"+ j + "张");
System.out.print("一元有"+ k + "张");
System.out.println();
num++;
}
}
}
}
System.out.println("一共有" + num + "种兑换方法");
}
}



