练习一:如何求一个0~255范围内的整数的十六进制的值?例如60的十六进制表示形式为3C。
如何求一个0~255范围内的整数的十六进制的值?例如60的十六进制表示形式为3C。
public class Test1{
public static void main(String[] args){
//方式一
String str1 = Integer.toBinaryString(60);
String str2 = Integer.toHexString(60);
//方式二
int i1 = 60;
int i2 = i1 & 15;
String j = (i2 > 9) ? (char)(i2 - 10 + 'A') : i2 + "";
int temp = i1 >>> 4;
i2 = temp & 15;
String k = (i2 > 9) ? (char)(i2 - 10 + 'A'):i2 + "";
System.out.println(k + "" + j);
}
}
练习二:键盘输入三个整数,将他们从大到小排序输出?(用条件判断)
1.键盘输入三个整数,将他们从大到小输出?
import java.util.Scanner;
class IfTest2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
if(num1 >= num2){
if(num3 >= num1){
System.out.println(num3+","+num1+","+num2);
}else if(num3 <= num2){
System.out.println(num1+","+num2+","+num3);
}else{
System.out.println(num1 +","+num3 +","+num2);
}
}else{
if(num3 >= num2){
System.out.println(num3+","+ num2+","+ num1);
}else if(num3 < num1){
System.out.println(num2+","+ num1+","+ num3);
}else{
System.out.println(num2+","+ num3+","+ num1);
}
}
}
}
练习三:我家的狗5岁了,5岁的狗相当于人类多大呢?其实,狗的前两年每
一年相当于人类的10.5岁,之后每增加一年就增加四岁。那么5岁的狗
相当于人类多少年龄呢?应该是:10.5 + 10.5 + 4 + 4 + 4 = 33岁。
编写一个程序,获取用户输入的狗的年龄,通过程序显示其相当于人
类的年龄。如果用户输入负数,请显示一个提示信息。
/*
我家的狗5岁了,5岁的狗相当于人类多大呢?其实,狗的前两年每
一年相当于人类的10.5岁,之后每增加一年就增加四岁。那么5岁的狗
相当于人类多少年龄呢?应该是:10.5 + 10.5 + 4 + 4 + 4 = 33岁。
编写一个程序,获取用户输入的狗的年龄,通过程序显示其相当于人
类的年龄。如果用户输入负数,请显示一个提示信息。
*/
import java.util.Scanner;
class IfTest3 {
public static void main(String[] args) {
System.out.println("请输入狗的年龄:");
Scanner scan = new Scanner(System.in);
Double ageDog = scan.nextDouble();
if(ageDog < 0){
System.out.println("你输入的年龄不合法");
else{
if(ageDog <= 2){
Double ageHuman = ageDog * 10.5;
System.out.println(ageHuman);
}else{
Double ageHuman = (2 * 10.5) + (ageDog - 2) * 4;
System.out.println(ageHuman);
}
}
练习四:假设你想开发一个玩彩票的游戏,程序随机地产生一个两位数的彩票,提示用户输入
一个两位数,然后按照下面的规则判定用户是否能赢。
1)如果用户输入的数匹配彩票的实际顺序,奖金10 000美元。
2)如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金 3 000美元。
3)如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1 000美元。
4)如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元。
5)如果用户输入的数字没有匹配任何一个数字,则彩票作废。
/*
假设你想开发一个玩彩票的游戏,程序随机地产生一个两位数的彩票,提示用户输入
一个两位数,然后按照下面的规则判定用户是否能赢。
1)如果用户输入的数匹配彩票的实际顺序,奖金10 000美元。
2)如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金 3 000美元。
3)如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1 000美元。
4)如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元。
5)如果用户输入的数字没有匹配任何一个数字,则彩票作废。
*/
import java.util.Scanner;
class IfTest4 {
public static void main(String[] args) {
int random = (int)(Math.random()*90 + 10);
System.out.println(random);
System.out.println("请输入一个两位数:");
int num = new Scanner(System.in).nextInt();
int baiRandom = random / 10;
int shiRandom = random % 10;
int baiNum = num / 10;
int shiNum = num % 10;
int bonus = 0;
if(random == num){
System.out.println(bonus = 10000);
}else if(baiRandom == shiNum && shiRandom == baiNum){
System.out.println(bonus = 3000);
}else if(baiRandom == baiNum || shiRandom == shiNum){
System.out.println(bonus = 1000);
}else if(baiRandom == shiNum || shiRandom == baiNum){
System.out.println(bonus = 500);
}else{
System.out.println("彩票作废");
}
}
}
练习五:大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出
一定的条件:高:180cm以上;富:财富1千万以上;帅:是。
如果这三个条件同时满足,则:“我一定要嫁给他!!!”
如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
如果三个条件都不满足,则:“不嫁!”
/*
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出
一定的条件:高:180cm以上;富:财富1千万以上;帅:是。
如果这三个条件同时满足,则:“我一定要嫁给他!!!” 如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
如果三个条件都不满足,则:“不嫁!”
*/
import java.util.Scanner;
class IfTest5 {
public static void main(String[] args) {
System.out.println("请输入你的身高(cm):");
Double height = new Scanner(System.in).nextDouble();
System.out.println("请输入你的财富(千万):");
Double deposit = new Scanner(System.in).nextDouble();
System.out.println("请输入你的外貌(回答是或否):");
String isHandsome = new Scanner(System.in).next();
if(height > 180 && deposit > 10000000 && isHandsome.equals("是") ){
System.out.println("我一定要嫁给他!!!");
}else if(height > 180 || deposit > 10000000 || isHandsome.equals("是")){
System.out.println("嫁吧,比上不足,比下有余。");
}else{
System.out.println("不嫁");
}
}
}
remark:
什么是真正的白富美?身为女子:洁身自好为白,经济独立为富,内外兼修为美。
什么是真正的高富帅?身为男子:大智若愚宠辱不惊是为高,大爱于心福泽天下是为帅,大略宏才智勇双全是为帅。
这是我认为最好的诠释。
练习六:使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输
import java.util.Scanner;
class SwitchCaseTest2 {
public static void main(String[] args) {
/*
使用 switch 把小写类型的 char型转为大写。只转换 a, b, c, d, e. 其它的输
出 “other”。
*/
System.out.println("请输入:");
String str = new Scanner(System.in).next();
char c = str.charAt(0);
switch(c){
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println('B');
break;
case 'c':
System.out.println('C');
break;
case 'd':
System.out.println('D');
break;
case 'e':
System.out.println('E');
break;
default:
System.out.println("other");
}
}
}
练习七:对学生成绩大于60分的输出“合格”。低于60分的输出“不合格”(使用switch-case结构)。
/*对学生成绩(整型)大于60分的,输出“合格”。低于60分的,输出“不合格”,使用switch-case结构完成。*/
System.out.println("请输入成绩:");
int score = new Scanner(System.in).nextInt();
switch(score / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不合格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("合格");
break;
}
//更优的解决方案
switch(score / 60){
case 0:
System.out.println("不合格");
break;
case 1:
System.out.println("合格");
}
练习八:根据用于指定月份,打印该月份所属的季节。 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
System.out.println("请输入月份:");
int month = new Scanner(System.in).nextInt();
switch(month){
case 3:
case 4:
case 5:
System.out.println("春季");
case 6:
case 7:
case 8:
System.out.println("夏季");
case 9:
case 10:
case 11:
System.out.println("秋季");
case 12:
case 1:
case 2:
System.out.println("冬季");
}
*练习九:编写程序:从键盘分别输入年、月、日,判断这一天是当年的第几天
注:判断一年是否是闰年的标准:
1)可以被4整除,但不可被100整除
或
2)可以被400整除
import java.util.Scanner;
class SwitchCaseTest3 {
public static void main(String[] args) {
System.out.println("请输入year:");
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();
System.out.println("请输入month:");
int month = scan.nextInt();
System.out.println("请输入day:");
int day = scan.nextInt();
int sumDays = 0;
switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 31;
case 6:
sumDays += 31;
case 5:
sumDays += 31;
case 4:
sumDays += 31;
case 3:
if((year % 4 == 0 && year % 100 !=0) || year % 400 == 0){
sumDays += 28;
}else{
sumDays += 29;
}
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.println(year+"年"+month+"月"+day+"天是当年的第"+sumDays+"天。");
}
}
练习十:
/*
使用switch语句改写下列if语句:
int a = 3;
int x = 100;
if(a==1)
x+=5;
else if(a==2)
x+=10;
else if(a==3)
x+=16;
else
x+=34;
*/
class SwitchTest3 {
public static void main(String[] args) {
int a = 3;
int x = 100;
switch(a){
case 1:
x += 5;
break;
case 2:
x += 10;
break;
case 3:
x += 16;
break;
default:
x += 34;
}
}
}
练习十一:编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的
值输出其对应的成绩等级:
score>=90 等级: A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D
方式一:使用if-else
方式二:使用switch-case
/*
编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的
值输出其对应的成绩等级:
score>=90 等级: A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D
方式一:使用if-else
方式二:使用switch-case: score / 10: 0 - 10
*/
import java.util.Scanner;
class SwitchCaseTest4 {
public static void main(String[] args) {
System.out.println("请输入成绩:");
int score = new Scanner(System.in).nextInt();
//方式一:switch-case
switch(score / 10){
case 10:
case 9:
System.out.println("A");
break;
case 8:
case 7:
System.out.println("B");
break;
case 6:
System.out.println("C");
break;
default:
System.out.println("D");
}
//方式二:if-else
if(score >= 90){
System.out.println("A");
}else if(score >= 70){
System.out.println("B");
}else if(score >= 60){
System.out.println("C");
}else{
System.out.println("D");
}
}
}
练习十二:编写一个程序,为一个给定的年份找出其对应的中国生肖。
中国的生肖基于12年一个周期,每年用一个动物代表:
rat(鼠)、ox(牛)、tiger(虎)、rabbit(兔)、dragon(龙)、snake(蛇)、
horse(马)、sheep(羊)、monkey(候)、rooster(鸡)、dog(狗)、pig(猪)。
提示:2019年:猪 2019 % 12 == 3
import java.util.Scanner;
class Test1{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
switch(year%12){
case 0:
System.out.println("猴年");
break;
case 1:
System.out.println("鸡年");
break;
case 2:
System.out.println("狗年");
break;
case 3:
System.out.println("猪年");
break;
case 4:
System.out.println("鼠年");
break;
case 5:
System.out.println("牛年");
break;
case 6:
System.out.println("虎年");
break;
case 7:
System.out.println("兔年");
break;
case 8:
System.out.println("龙年");
break;
case 9:
System.out.println("蛇年");
break;
case 10:
System.out.println("马年");
break;
case 11:
System.out.println("羊年");
break;
}
}
}
练习十三:编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行
上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印
输出“baz”。
/*
编写程序从1循环到150,并在每行打印一个值,另外在每个3的倍数行
上打印出“foo”,在每个5的倍数行上打印“biz”,在每个7的倍数行上打印
输出“baz”。
*/
class ForTest {
public static void main(String[] args){
for(int i = 1;i <= 150;i++){
System.out.print(i+" ");
if(i % 3 == 0){
System.out.print("foo ");
}
if(i % 5 == 0){
System.out.print("biz ");
}
if(i % 7 == 0){
System.out.print("baz");
}
System.out.println();
}
}
}
练习十四:输入两个正整数m和n,求其最大公约数和最小公倍数。
比如:12和20的最大公约数是4,最小公倍数是60。
/*
输入两个正整数m和n,求其最大公约数和最小公倍数。
比如:12和20的最大公约数是4,最小公倍数是60。
*/
import java.util.Scanner;
class ForTest {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int m = scan.nextInt();
int n = scan.nextInt();
int min = m <= n? m : n;
for(int i = m;i >= 1;i--){
if(m % i == 0 && n % i == 0){
System.out.println("最大公约数为:"+i);
break;
}
}
//最小公倍数
int max = m >= n? m : n;
for(int i = max;i <= m*n;i++){
if(i % m == 0 && i % n == 0){
System.out.println("最小公倍数为:"+i);
break;
}
}
}
}
练习十五:输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数
字立方和等于其本身。
/*
输出所有的水仙花数,所谓水仙花数是指一个3位数,其各个位上数
字立方和等于其本身。
*/
import java.util.Scanner;
class ForTest1 {
public static void main(String[] args) {
for(int i = 100;i <= 999;i++){
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if(i == ge * ge * ge + shi * shi * shi + bai * bai *bai){
System.out.println(i);
}
}
}
}
练习十六:计算100以内的偶数的和,和偶数的个数(使用do-while结构)
class DoWhileTest {
public static void main(String[] args) {
//计算100以内的偶数的和,和偶数的个数
int i = 1;
int sum = 0;
int count = 0;
do{
if(i % 2 == 0){
sum += i;
count++;
}
i++;
}while(i <= 100);
System.out.println(sum+" "+i);
}
}
练习十七:从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
为0时结束程序。
/*
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
为0时结束程序。
*/
import java.util.Scanner;
class For2Test {
public static void main(String[] args) {
int positiveNumber= 0;
int negativeNumber= 0;
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("请输入整数:");
int num = scan.nextInt();
if(num < 0){
negativeNumber++;
}else if(num > 0){
positiveNumber++;
}else{
break;
}
}
System.out.println("正数的个数为:"+positiveNumber+",负数的个数为:"+negativeNumber);
}
}
练习十八:打印图形
class ForForTest {
public static void main(String[] args) {
/*
******
******
******
******
******
*/
for(int i = 0;i < 5;i++){
for(int j = 1;j <= 6;j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("***********************");
/*
*
**
***
****
*****
*/
for(int i = 1;i <= 5;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println();
}
/*
*****
****
***
**
*
*/
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 5 - i;j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("*************************************");
/*
*
**
***
****
*****
****
***
**
*
*/
for(int i = 1;i <= 5;i++){
for(int j = 1;j <= i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 5 - i;j++){
System.out.print("*");
}
System.out.println();
}
System.out.println("**************************************");
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
for(int i = 1;i <= 5;i++){
for(int j = 1;j <= 5 - i;j++){
System.out.print(" ");
}
for(int t = 1;t <= i;t++){
System.out.print("*");
}
for(int n = 1;n <= i - 1;n++){
System.out.print("*");
}
System.out.println();
}
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= i;j++){
System.out.print(" ");
}
for(int t = 1;t <= 5 - i;t++){
System.out.print("*");
}
for(int n = 1;n <= 4 - i;n++){
System.out.print("*");
}
System.out.println();
}
//九九乘法表
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print(i+"*"+j+"="+i*j+'\t');
}
System.out.println();
}
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
}
}
*练习十九:输出100以内所有的质数
//输出100以内所有的质数
class PrimeNumberTest{
public static void main(String[] args){
//方式一
for(int i = 2;i <= 100;i++){
boolean isFlag = true;
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
isFlag = false;
break;
}
}
if(isFlag == true){
System.out.println(i);
}
}
//方式二
label:for(int i = 2;i <= 100;i++){
for(int j = 2;j <= Math.sqrt(i);j++){
if(i % j == 0){
continue label;
}
}
System.out.println(i);
}
}
}
练习二十:一个数如果恰好等于它的因子之和,这个数就称为"完数"
(因子:除去这个数本身正的约数)
例如6=1+2+3.编程 找出1000以内的所有完数
/*
一个数如果恰好等于它的因子之和,这个数就称为"完数"
(因子:除去这个数本身正的约数)
例如6=1+2+3.编程 找出1000以内的所有完数
*/
class Exer {
public static void main(String[] args) {
int factor = 0;
for(int i = 1;i <= 1000;i++){
for(int j = 1;j <= i/2;j++){
if(i % j == 0){
factor += j;
}
}
if(factor == i){
System.out.println(i);
}
//重置factor
factor = 0;
}
}
}
*练习二十一:使用二维数组打印一个 10 行杨辉三角。 【提示】
1. 第一行有 1 个元素, 第 n 行有 n 个元素
2. 每一行的第一个元素和最后一个元素都是 1
3. 从第三行开始, 对于非第一个元素和最后一个元
素的元素。即:
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
package cn.tedu.exer;
/*
使用二维数组打印一个 10 行杨辉三角。 【提示】
1. 第一行有 1 个元素, 第 n 行有 n 个元素
2. 每一行的第一个元素和最后一个元素都是 1
3. 从第三行开始, 对于非第一个元素和最后一个元
素的元素。即:
yanghui[i][j] = yanghui[i-1][j-1] + yanghui[i-1][j];
*/
public class YangHuiTest {
public static void main(String[] args) {
//1.定义数组
int[][] yangHui = new int[10][];
//2.遍历数组,给元素赋值
for(int i = 0;i < yangHui.length;i++){
yangHui[i] = new int[i+1];
//2.1给首末元素赋值
yangHui[i][0] = yangHui[i][i] = 1;
//2.2给非首末元素赋值
//if(i > 1){
for(int j = 1;j < yangHui[i].length-1;j++){
yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
}
//}
}
//给元素赋值
for(int i = 0;i < yangHui.length;i++){
for(int j = 0;j < yangHui[i].length;j++){
System.out.print(yangHui[i][j]+ " ");
}
System.out.println();
}
}
}
练习二十二:定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,
然后求出所有元素的最大值,最小值,和值,平均值,并输出出来。
要求:所有随机数都是两位数
package cn.tedu.exer;
import java.util.Arrays;
/*
定义一个int型的一维数组,包含10个元素,分别赋一些随机整数,
然后求出所有元素的最大值,最小值,和值,平均值,并输出出来。
要求:所有随机数都是两位数
*/
public class ArrayTest {
public static void main(String[] args) {
int[] arr = new int[10];
int sum = 0;
int avg = 0;
//2.给元素赋值
for(int i = 0;i < arr.length;i++){
arr[i] = (int)(Math.random()*90 + 10);
}
//3.求最大值
int maxValue = arr[0];
for(int i = 1;i < arr.length;i++){
if(maxValue < arr[i]){
maxValue = arr[i];
}
}
//4.求最小值
int minValue = arr[0];
for(int i = 1;i < arr.length;i++){
if(minValue > arr[i]){
minValue = arr[i];
}
}
//5.求总和
for(int i = 0;i < arr.length;i++){
sum += arr[i];
}
avg = sum / arr.length;
System.out.println(Arrays.toString(arr));
System.out.println("最大值为:"+maxValue+",最小值为:"+minValue+",和为:"+sum+",平均数为:"+avg);
}
}
练习二十三:自定义数组,实现数组的复制、反转、线性查找和二分查找
public static void main(String[] args) {
//数组的复制
String[] arr = new String[]{"A","B","C","D","E"};
String[] arr1 = new String[arr.length];
for(int i = 1;i < arr1.length;i++){
arr1[i] = arr[i];
}
// 数组的查找或搜索
// 线性查找
String dest = "B";
boolean isFlag = true;
for(int i = 0;i < arr.length;i++){
if(dest.equals(arr[i])){
System.out.println("找到了指定的元素,它在:"+i);
isFlag = false;
}
}
if(isFlag){
System.out.println("很遗憾,没找到元素哦~");
}
// 二分查找(折半查找)
// 前提:该数组是有序的
int[] array = new int[]{-23,-12,-4,45,78,98,103};
int dest1 = 78;
int head = 0;
int end = array.length-1;
boolean isFlag1 = true;
while(head <= end){
int middle = (head + end)/2;
if(dest1 == array[middle]){
System.out.println("目标值在:"+middle);
isFlag = false;
break;
}else if(dest1 < array[middle]){
end = middle - 1;
}else{
head = middle + 1;
}
}
if(isFlag){
System.out.println("很遗憾,没找到元素哦~");
}
//数组的反转
//方式一:
for(int i = 0; i < arr.length/2;i++){
String temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
System.out.println(arr);
//方式二:
for(int i = 0, j = arr.length - 1;i < j;i++,j--){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.println(arr);
}
*练习二十四:定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
package cn.tedu.java;
import java.util.Arrays;
/*
定义类Student,包含三个属性:学号number(int),年级state(int),成绩
score(int)。 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
问题一:打印出3年级(state值为3)的学生信息。
问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
*/
public class Exer3 {
public static void main(String[] args) {
Student[] students = new Student[20];
for(int i = 0;i < students.length;i++){
Student s = new Student();
s.number = i + 1;
s.state = (int)(Math.random() * (6 - 1 + 1)+1);//[1,6]
s.score = (int)(Math.random() * 100 + 1);//[0,100]
students[i] = s;
}
// 遍历学生数组
print(students);
System.out.println("*****************************");
// 问题一:打印出3年级(state值为3)的学生信息。
printState(students);
System.out.println("*******************************");
// 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
sort(students);
print(students );
}
public static void printState(Student[] students){
for(int i = 0;i < students.length;i++){
if(students[i].state == 3){
System.out.println(students[i].info());
}
}
}
public static void sort(Student[] students){
// 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
for(int i = 0; i < students.length - 1;i++){
for(int j = 0;j < students.length - 1 -i;j++){
if(students[j].score > students[j+1].score){
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
public static void print(Student[] students){
// 遍历学生数组
for(int i = 0;i < students.length;i++){
System.out.println(students[i].info());
}
}
}
class Student{
int number;
int state;
int score;
// 显示学生信息
public String info(){
return "学号:"+number+",年级:"+state+",成绩"+score;
}
}
练习二十五:已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),其中n是大于0
的整数,求f(10)的值。
/*
已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),其中n是大于0
的整数,求f(10)的值。
*/
public class RecursionTest {
public static void main(String[] args) {
RecursionTest test = new RecursionTest();
int value = test.f(10);
System.out.println(value);
}
public int f(int n){
if(n == 0){
return 1;
}else if(n == 1){
return 4;
}else{
return 2*f(n - 1)+f(n-2);
}
}
}
练习二十六:输入一个数据n,计算斐波那契数列(Fibonacci)的第n个值
1 1 2 3 5 8 13 21 34 55
规律:一个数等于前两个数之和
要求:计算斐波那契数列(Fibonacci)的第n个值
输入一个数据n,计算斐波那契数列(Fibonacci)的第n个值
1 1 2 3 5 8 13 21 34 55
规律:一个数等于前两个数之和
要求:计算斐波那契数列(Fibonacci)的第n个值
测试略
public int Fibonacci(int n){
if(n == 1){
return 1;
}else if(n == 2){
return 1;
}else{
return Fibonacci(n-1)+Fibonacci(n-2);
}
}