Java 综合实战(一)
健康计算器
需求:开发一个简单的健康计算器应用程序,它可以接受用户的输入(如年龄、性别、体重、身高),并计算出用户的 BMI(身体质量指数)和 BMR(基础代谢率)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package test;
import java.util.Scanner;
public class test { public static void main(String[] args) { Scanner sc=new Scanner(System.in);
System.out.println("请您输入您的身高:"); double height=sc.nextDouble();
System.out.println("请您输入您的体重:"); double weight=sc.nextDouble();
System.out.println("请您输入您的性别(男/女):"); String sex=sc.next();
System.out.println("请您输入您的年龄:"); int age=sc.nextInt();
double bmi=calcBMI(height,weight); System.out.println("您的BMI值为:"+bmi);
double bmr=calcBMR(height,weight,age,sex); System.out.println("您的BMR值为:"+bmr); }
public static double calcBMI(double height,double weight){ return weight/(height*height); }
public static double calcBMR(double height,double weight,int age,String sex){ double bmr=0; if(sex.equals("男")){ bmr=88.362+13.397*weight+4.799*height-5.677*age; } else{ bmr=447.593+9.247*weight+3.098*height-4.330*age; } return bmr; } }
|
简单计算器
需求:设计一个可以执行基本数学运算(加、减、乘、除)的计算器程序。
功能描述:用户输入两个数字、一个运算符(+、-、*、/)。根据所选运算符执行相应的数学运算,显示运算结果。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package test1;
import java.util.Scanner;
public class test1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个数字:"); double num1=sc.nextDouble();
System.out.println("请输入第二个数字:"); double num2=sc.nextDouble();
System.out.println("请输入运算符(+、-、*、/):"); String operator=sc.next();
double result=calculate(num1,num2,operator); System.out.println("计算结果为:"+result); }
public static double calculate(double num1,double num2,String operator){ double result=0; switch (operator){ case "+": result=num1+num2; break; case "-": result=num1-num2; break; case "*": result=num1*num2; break; case "/": result=num1/num2; break; default: System.out.println("输入的运算符有误!"); } return result; } }
|
猜数字小游戏
需求:随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜小提示过小,直到猜中结束游戏。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package test1;
import java.util.Random; import java.util.Scanner;
public class test2 { public static void main(String[] args) { guessNumber(); }
public static void guessNumber(){
Random r=new Random(); int number=r.nextInt(100)+1;
Scanner sc=new Scanner(System.in);
while(true){ System.out.println("请输入一个1~100之间的数字:"); int guess=sc.nextInt(); if(guess<number){ System.out.println("猜小了,再试试吧!"); } else if(guess>number){ System.out.println("猜大了,再试试吧!"); } else{ System.out.println("恭喜你猜对了!"); break; } } } }
|
开发一个验证码
需求:开发一个程序,可以生成指定位数的验证码,每位可以是数字,大小写字母。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package test1;
public class test3 { public static void main(String[] args) { System.out.println(getCode(6)); }
public static String getCode(int n){ String code="";
for(int i=0;i<n;i++){ int type=(int)(Math.random()*3); switch(type){ case 0: int num=(int)(Math.random()*10); code+=num; break; case 1: int num1=(int)(Math.random()*26)+65; code+=(char)num1; break; case 2: int num2=(int)(Math.random()*26)+97; code+=(char)num2; break; } } return code; } }
|
找素数
需求:输出101-200之间的所有素数以及总个数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package test1;
public class test4 { public static void main(String[] args) { int count=0; for(int i=101;i<=200;i++){ if(isPrime(i)){ System.out.println(i); count++; } } System.out.println("素数的个数为:"+count); }
public static boolean isPrime(int num){ for(int i=2;i*i<=num;i++){ if(num%i==0){ return false; } } return true; } }
|
斗地主游戏
需求:开发一个简易版的斗地主游戏,要求只完成做牌(存储54张牌)、洗牌。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package test2;
public class test1 { public static void main(String[] args) { start(); } public static void start(){ String[] poker=new String[54]; String[] colors={"♠","♥","♣","♦"}; String[] nums={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int index=0; for(int i=0;i<colors.length;i++){ for(int j=0;j<nums.length;j++){ poker[index++]=colors[i]+nums[j]; } } poker[index++]="Big Joker"; poker[index++]="Small Joker";
for(int i=0;i<poker.length;i++){ System.out.print(poker[i]+"\t"); } System.out.println();
for(int i=0;i<poker.length;i++){ int j=(int)(Math.random()*poker.length); String temp=poker[i]; poker[i]=poker[j]; poker[j]=temp; }
System.out.println("洗牌后:"); for(int i=0;i<poker.length;i++){ System.out.print(poker[i]+"\t"); } System.out.println(); } }
|
石头迷阵游戏
需求:只需完成数据初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package test2;
public class test2 { public static void main(String[] args) { start(5); } public static void start(int n) { int[][] arr=new int[n][n];
int count=1; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ arr[i][j]=count++; } }
print(arr);
for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ int x=(int)(Math.random()*arr.length); int y=(int)(Math.random()*arr[i].length); int temp=arr[i][j]; arr[i][j]=arr[x][y]; arr[x][y]=temp; } } System.out.println("------------------"); System.out.println("打乱后:"); print(arr); } public static void print(int[][] arr){ for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]+"\t"); } System.out.println(); } } }
|