Java基础(0510)
练习题1:接收用户输入的3个整数,并将它们的最大值作为结果输出
public class Test01 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
System.out.print(Math.max(Math.max(a, b), c));
}
}
}
练习题2: BMI 指数测试 BMI = 体重 (kg) / 身高² (m)
接收用户输入的身高和体重,将判断结果输出
过轻:低于18.5
正常:18.5 ~ 22.9
偏胖:23 ~ 24.9
肥胖:25 ~ 29.9
重度肥胖:高于30
极度肥胖:高于40
public class Test02 {
public static void main(String[] args) {
double kg = new Scanner(System.in).nextDouble();
double m = new Scanner(System.in).nextDouble();
double BMI = kg / (m*m);
System.out.println(BMI);
if(BMI < 18.5) {
System.out.print("过轻");
}else if(BMI < 22.9) {
System.out.print("正常");
}else if(BMI < 24.9) {
System.out.print("偏胖");
}else if(BMI < 29.9) {
System.out.print("肥胖");
}else if(BMI < 40) {
System.out.print("重度肥胖");
}else {
System.out.print("极度肥胖");
}
}
}
练习题3: 手机选号:根据用户输入的手机号来确认用户实际支付的价格
如果尾数为8,需支付办卡费50元
如果尾数为4,需支付办卡费用0元
如果是其他尾号,需支付办卡费用20元
public class Test03 {
public static void main(String[] args) {
String tel = new Scanner(System.in).nextLine();
if(tel.length() != 11) {
System.out.print("号码格式错误");
return;
}
char[] tell = tel.toCharArray();
switch(tell[10]) {
case '8' : System.out.println("尾数为8,需支付办卡费50元");break;
case '4' : System.out.println("尾数为4,需支付办卡费0元");break;
default : System.out.println("尾数为其他,需支付办卡费20元");break;
}
}
}
练习题4 : 分别通过for循环/While循环/do-While循环写一个死循环
public class Test04 {
public static void main(String[] args) {
int a = new Scanner(System.in).nextInt();
switch(a) {
case 1 : f1();break;
case 2 : f2();break;
default : f3();break;
}
}
private static void f3() {
// TODO Auto-generated method stub
do {
System.out.println("胡志明是小小弟!");
}while(true);
}
private static void f2() {
// TODO Auto-generated method stub
while(true) {
System.out.println("钟继龙是小小弟!");
}
}
private static void f1() {
// TODO Auto-generated method stub
for(int i = 0;i>=0;i++) {
System.out.println("汪林是小弟!");
}
}
}
练习题5:鸡兔同笼问题(穷举法)
public class Test05 {
public static void main(String[] args) {
int allnum = 35;
int allfootnum = 94;
int chicken = 0;
int rabbit = allnum - chicken;
int footnum = 0;
do {
footnum = 4*rabbit + 2*chicken;
if( footnum == allfootnum) {
System.out.println("兔子有"+rabbit+"只,鸡有"+chicken+"只");
}
chicken++;
rabbit = allnum - chicken;
}while(rabbit != 0 );
}
}
练习题6:求数字阶乘(for循环版)
需求:接收用户输入的数字,计算该数字的阶乘结果
已知:负数不可以有阶乘,0的阶乘结果是1,
5 ! = 5 x 4 x 3 x 2 x 1
public class Test06 {
public static void main(String[] args) {
int a = new Scanner(System.in).nextInt();
int result = 1;
if(a< 0 ) {
System.out.println("输入错误");
}else if(a == 0) {
System.out.println(result);
}else {
for(;a>0;a--) {
result*=a;
}
System.out.println(result);
}
}
}
练习题7:多次生成随机数,并打印第一次出现大于0.999 时的次数与生成的随机数
public class Test07 {
public static void main(String[] args) {
double a = 0;
int i = 0;
do {
a=Math.random();
//a = new Random.nextInt(100);
i++;
}while(a<0.999);
System.out.print("第一次出现大于0.999 时的次数:"+i+" 生成的随机数:"+ a);
}
}
练习题8:打印100以内除了尾数为3,5,7的所有数
public class Test08 {
public static void main(String[] args) {
int result;
for(int i=0;i<100;i++) {
result = i % 10;
if(result != 3 && result != 5 && result != 7 ) {
System.out.print(i+" ");
}
}
}
}
练习题9:求质数:接收用户输入的数字,判断是否为质数
质数的概念:一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数,也称为素数
规定:1既不是质数,也不是合数
public class Test09 {
public static void main(String[] args) {
int a = new Scanner(System.in).nextInt();
int j = a/2;
int result = 0;
int i = 2;
if(a == 2) {
System.out.print("是素数");
return;
}else if(a == 1) {
System.out.print("不是素数");
return;
}else {
while(i<=j) {
result = a % i ;
if(result == 0) {
System.out.print("不是素数");
return;
}
i++;
}
}
System.out.print("是素数");
return;
}
}
练习题10:接收用户输入的数字,判断在此范围内质数的个数
public class Test10 {
public static void main(String[] args) {
int a = new Scanner(System.in).nextInt();
int sum = 0;
for (int i = 2; i <= a; i++) {
if (judge(i)) {
sum++;
}
}
System.out.print("在此范围内质数有" + sum + "个");
}
private static boolean judge(int a) {
// TODO Auto-generated method stub
int j = a / 2;
int result = 0;
int i = 2;
if (a == 2) {
return true;
} else {
while (i <= j) {
result = a % i;
if (result == 0) {
return false;
}
i++;
}
}
return true;
}
}
练习题11:生成一个顺序数组,将这个数组的元素打乱顺序后输出
public class Test11 {
public static void main(String[] args) {
int a[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++) {
int j = new Random().nextInt(5);
int k = new Random().nextInt(5);
int b = 0;
b = a[j];
a[j] = a[k];
a[k] = b;
}
System.out.print(Arrays.toString(a));
}
}