当前位置: 首页 > news >正文

java入门-java方法实现+案例

(一)使用方法实现求和

public class menthod {public static void main(String[] args) {//小莉int s=sum(10,20);System.out.println(s);//小王int t=sum(s,8);System.out.println(t);}public static int sum(int a,int b){//int 是返回类型,sum是方法名称int c=a+b;return c;}
}

//无参数无返回值

public static void print(){

System.out.println(“无参数无返回值”);

}

//传递数组

public class menthod {public static void main(String[] args) {int[] w={1,2,3,6,3};printfnum(w);}public static void printfnum(int[] w){for(int i=0;i<w.length;i++){System.out.println(w[i]);}}
}

 (一)买飞机票

public class BuyFlyTicket {public static void main(String[] args) {double price=123.98;int month=2;int c=1;//1为头等舱,0为经济舱double need=0;switch(month) {case 5:case 6:case 7:case 8:case 9:case 10:if (c == 0) {need =price*0.85;} else if (c == 1) {need =price*0.9;}break;case 11:case 12:case 1:case 2:case 3:case 4:if (c == 0) {need =price*0.65;} else if (c == 1) {need =price*0.7;}break;default:System.out.println("error");}System.out.println("价格为"+need);// System.out.println("月份%的价格为".codePointAt(month));}
}

(二)随机生成验证码

import java.util.Random;public class Code {public static void main(String[] args) {//生成随机验证码String code=creatCode(10);System.out.println(code);}public static String creatCode(int n){//验证码位数String s="";//由于验证码有数字,大写字母和小写字母,随机一个0~2数字,分别表示应该生成什么类型for(int i=1;i<=n;i++){Random R=new Random();int type=R.nextInt(3);if(type==0){//0~9数字int r=R.nextInt(10);s=s+r;}else if(type==1){char r=(char)(R.nextInt(26)+'a');s+=r;}else if(type==2){char r=(char)(R.nextInt(26)+'A');s+=r;}}return s;}
}

(三)找素数

判断101-200之间有多少个素数,并输出所有素数。

//素数是除了1和它本身以外,不能被其他正整数整除的数

eg:3,7是素数,而9,21不是素数

public class primeNumber {public static void main(String[] args) {int total=0;for(int i=101;i<=200;i++){boolean t=check(i);if(!t){total++;System.out.println(i);}}System.out.println("一共"+total+"个");}public static Boolean check(int num){Boolean t=false;for(int i=2;i<num;i++){if(num%i==0){//能整除t=true;}}return t;}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 软件架构之计算机网络
  • 【鸿蒙学习笔记】使用动画
  • Vue3框架搭建:vue+vite+pina+typescript
  • C++ Qt 自制开源科学计算器
  • 2023.2版IDEA复制配置修改端口增加一个当前运行服务的操作流程
  • cv::Mat 操作多维矩阵的思路
  • 快速响应需求:App路由动态化探索
  • 2024 年第十四届亚太数学建模竞赛(中文赛项)浅析
  • 【深度学习实战(44)】Anchor based and Anchor free(无锚VS有锚)
  • 鸿蒙笔记导航栏,路由,还有axios
  • Java基础概念
  • UDP通讯实现
  • 速盾:cdn 缓存图片
  • Vue基础知识:Vue3.3出现的defineOptions,如何使用,解决了什么问题?
  • 2. 年龄问题
  • 2017-08-04 前端日报
  • C++入门教程(10):for 语句
  • CentOS 7 防火墙操作
  • ECMAScript入门(七)--Module语法
  • gitlab-ci配置详解(一)
  • Python_网络编程
  • Vim 折腾记
  • 动态魔术使用DBMS_SQL
  • 开发基于以太坊智能合约的DApp
  • 使用docker-compose进行多节点部署
  • 吐槽Javascript系列二:数组中的splice和slice方法
  • 小程序滚动组件,左边导航栏与右边内容联动效果实现
  • 写代码的正确姿势
  • 自动记录MySQL慢查询快照脚本
  • 关于Kubernetes Dashboard漏洞CVE-2018-18264的修复公告
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​linux启动进程的方式
  • #pragma multi_compile #pragma shader_feature
  • $(function(){})与(function($){....})(jQuery)的区别
  • (1)SpringCloud 整合Python
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (Spark3.2.0)Spark SQL 初探: 使用大数据分析2000万KF数据
  • (web自动化测试+python)1
  • (附源码)ssm旅游企业财务管理系统 毕业设计 102100
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • .jks文件(JAVA KeyStore)
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .net core 6 集成和使用 mongodb
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .NET 读取 JSON格式的数据
  • .Net 路由处理厉害了
  • ?
  • @antv/g6 业务场景:流程图
  • @Bean, @Component, @Configuration简析
  • @cacheable 是否缓存成功_Spring Cache缓存注解
  • @Pointcut 使用
  • @RestControllerAdvice异常统一处理类失效原因