欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 综合小案例

综合小案例

2025/2/24 11:57:51 来源:https://blog.csdn.net/m0_74344909/article/details/142989919  浏览:    关键词:综合小案例

目录

1、简单计算器 

2、猜数字小游戏

 3、开发一个验证码

4、找素数 


1、简单计算器 

目标:

  • 设计一个可以执行基本数学运算(加、减、乘、除)的计算器程序。

功能描述:

  • 用户输入两个数字、一个运算符(+、-、*、/)。
  • 根据所选运算符执行相应的数学运算,显示运算结果。
import java.util.Scanner;public class Cal {public static void main(String[] args) {System.out.println("请输入算式(如1 - 1): ");Scanner scanner = new Scanner(System.in);double a=scanner.nextInt();String c=scanner.next();double b=scanner.nextInt();double result=cal(a,c,b);System.out.println("输出结果:"+a+c+b+"="+result);}public static double cal(double a,String c,double b){boolean Conditions=false;double result = 0;do {switch (c) {case "+":result = a + b;break;case "-":result = a - b;break;case "*":result = a * b;break;case "/":if (b == 0) {System.out.println("Error:The dividend cannot be 0!");break;}result = a / b;break;default:System.out.println("算式输入错误,请重新输入!");Conditions=true;}}while (Conditions);return result;}
}

2、猜数字小游戏

需求:

  • 随机生成一个1-100之间的数据,提示用户猜测,猜大提示过大,猜小提示过小,直到猜中结束游戏。

分析:

  1. 先随机生成一个1-100之间的数据。
  2. 定义一个死循环让用户可以一直猜测。
  3. 在死循环里,每次都提示用户输入一个猜测的数字,猜大提示过大,猜小提示过小,猜中则结束游戏。
import java.util.Scanner;public class Rad {public static void main(String[] args) {System.out.println("----------游戏开始----------");rad();System.out.println("----------游戏结束----------");}public static void rad(){int GuessNum=(int)(Math.random()*100)+1;//System.out.println("内部测试数据:"+GuessNum);Scanner sc=new Scanner(System.in);int count=0;while(true) {System.out.print("请输入你所想的数字(1~100):");int num = sc.nextInt();count++;if (num > GuessNum) {System.out.println("猜大了!");} else if (num == GuessNum) {System.out.println("恭喜你,猜对了!");System.out.println("总共猜了" + count + "次!");break;} else{System.out.println("猜小了!");}}}
}

注:

得到随机数的两种方式:

1、 通过Random类

        Random random=new Random();

        int randomNum=random.nextInt(100)+1; //[1,100]

2、通过Math类(Math.random()返回值为[0,1)的小数)

        int randomNum=(int)(Math.random()*100)+1;//[0,100]


 3、开发一个验证码

需求:

  • 开发一个程序,可以生成指定位数的验证码,每位可以是数字、大小写字母。

分析:

  • 使用for循环依次生成每位随机字符 并使用一个String类的变量把每个字符连接起来,最后返回该变量即可。
import java.util.Scanner;public class Check {public static void main(String[] args) {System.out.println("------验证开始------");Scanner sc= new Scanner(System.in);String Code=GetCode(5);System.out.println("验证码:"+Code);System.out.print("请输入验证码:");String Input=sc.nextLine();while(!Input.equals(Code)){Code=GetCode(5);System.out.println("验证码错误,请重新输入!");System.out.println("验证码:"+Code);System.out.print("请输入验证码:");Input=sc.nextLine();}System.out.println("验证码输入正确!");System.out.print("------验证成功------");}public static String GetCode(int n){int type=0;String Code="";for (int i = 0; i < n; i++) {type =(int)(Math.random()*3);switch (type){case 0:int num1=(int)(Math.random()*10);Code+=num1;break;case 1:int num2=(int)(Math.random()*26);char ch1=(char)('A'+num2);Code+=ch1;break;case 2:int num3=(int)(Math.random()*26);char ch2=(char)('A'+num3);Code+=ch2;break;}}return Code;}
}

4、找素数 

说明:除了1和它本身以外,不能被其他正整数整除,就叫素数。

比如:3、7就是素数,而9、21等等不是素数。

public class ISPRIME {public static void main(String[] args) {int count=0;for (int i = 2; i <1000; i++) {boolean log=Isprime(i);if(log) {count++;System.out.print(i+" ");if(count%10==0)System.out.println();}}}public static boolean Isprime(int n){for(int i=2;i<=Math.sqrt(n);i++){if(n%i==0){return false;}}return true;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com