java学习笔记 part1
java
- Test.java,文件名和java类名称一致
public class Test{
public static void main(String[] args){
System.out.println("hello word");
}
}
- jvm,java虚拟机,真正运行java程序的地方
- 核心类库,java自带的程序,给程序员调用
- jRE:java运行环境,包括jvm,核心类库
- JDK:java开发工具包
- java跨平台的原理就是jvm虚拟机
java 基础语法
注释
- 注释不影响程序运行的原因就是,编译后的class文件中,没有注释
//,单行注释
/* 多行注释 /
/* 文档注释 */,可以自动提取到程序说明中去
字面量
数据在程序中的书写格式
- 整数
- 小数
- 字符
- 字符串
- 布尔
变量
存储数据的内存区域
变量定义的格式 int i = 1;
public class Test{
public static void main(String[] args){
double moeny = 6.0;
System.out.println(moeny);
moeny += 4.0;
System.out.println(moeny);
System.out.println("------------------------");
int age = 21;
System.out.println(age);
age = 25;
System.out.println(age);
}
}
变量使用注意事项
public class Test{
public static void main(String[] args){
//变量要先声明后使用
int a = 23;
//变量声明后不能存储其他类型数据
//变量的有效范围,一对大括号直接,同一范围内不能定义两个相同的便利名
{
int b =1;
}
System.out.println(b);//找不到b
//变量定义时,可以没有初始化值,使用的时候必须初始化值
int c ;
c = 12;
}
}
数据类型
基本数据类型-8种
1. 整数类型
java各整数类型有固定的表数范围和字段长度
java整型常量默认为int,long类型复制时,要在变量值后面加L,long i = 3L
类型 | 占用存储空间 | 表数范围 |
---|---|---|
byte | 1字节 | -128 - 127 |
short | 2字节 | -215 - 215-1 |
int(默认) | 4字节 | -231 - 231-1 |
long | 8字节 | -263 - 263-1 |
2. 浮点类型
java浮点型常量默认为double
float类型变量需要在变量值之后加F,float i = 0.1f
类型 | 占用存储空间 | 表数范围 | 精度 |
---|---|---|---|
单精度float | 4字节 | -2128 - 2128 | 7位有效数字 |
双精度double(默认) | 8字节 | -21024 - 21024 | 16位有效数字 |
3. 字符类型
char,用单引号括起来的单个字符,char i = 'z',范围0-65535,占用两个字节
转义字符
转义字符 | 解释 |
---|---|
\b | 退格符 |
\n | 换行 |
\r | 回车 |
\t | 制表 |
\" | 双引号 |
\' | 单引号 |
\ | 反斜线 |
4. 布尔类型
boolean类型只允许true或false,不允许0或其他数代替
boolean b = true
练习一下
public class Test{
public static void main(String[] args){
System.out.print("333\n");//使用换行符
System.out.println("666");//相当于另起一行
System.out.println("444");
byte b = 1;
System.out.println(b);
short s = 2;
System.out.println(s);
int i = 3;
System.out.println(i);
long k = 3L;
System.out.println(k);
float u = 0.3F;
System.out.println(u);
double d1 = 0.7;
System.out.println(d1);
char o = '6';
System.out.println(o);
boolean b1 = true;
System.out.println(b1);
}
}
引用数据类型
引用类型,都可以用null作为值,初始化时可以赋值为null
String类
值是不可变的,用来接收字符串
//可以拼接
String s3 = "h" + "e";
String s = "hello word";
System.out.println(s);
int i1 = 1;
int i2 = 1;
//以上会在内存中存储两个1的值
String s1 = "hello";
String s2 = "hello";
//内存中只存一个,两个变量名会引用同一个值,,两个变量名指向同一地址
关键字和标识符
关键字
是java自己保留的单词,特殊功能,不能用做命名
标识符
是用来给类和方法起名字的规矩
- 数字,字母,下划线和$组成
- 不能以数字开头,不能是关键字,区分大小写
- 变量名称:全英文,有意义,首个单词首字母小写,后面的单词首字母大写,,驼峰模式
- 类名:全英文,所有单词首字母大写,驼峰模式
类型转换,运算符,键盘录入
自动类型转换
- 容量小的类型,可以直接转换为容量大的类型
- byte,short,char之间不能相互转换,三个在计算时,会首先都转换成int类型,解决计算后最大的范围
- 多种数据混合计算的时候,系统会首先将所有数据转换成容量最大的进行计算
- 数字类型的运算中,多个相同类型的变量参与运算,变量要先转化为相对应的数据类型的默认类型,(两个byte类型进行运算时,先把byte类型转换为整型的默认类型int然后进行计算,结果也是int类型),变量的数据类型的容量比默认的小的情况
public class Test{
public static void main(String[] args){
/* int i = 0;//声明并初始化变量
i = 1;//重新赋值
int k = 20;
i = k ;
short s =9;
short s0 = 11;
s0 = s;//将右边的赋值左边的
byte b = 1;
int m = b;//都属于整数型,小的容量可以直接赋值给大容量
int a = 0;
byte b= a;//编译期异常,javac的时候才能发现
int i = 1;
short s = 2;
byte b = 3;
int res = i + s + b ;
//计算过程中int范围最大,short和byte先转换成int类型,然后再计算
char c = 'a';
byte bb = 2;
int k = c + bb;
//char类型在与数字进行数学运算时,转换为对应的ascii的值,在进行计算
String str = "abb";
int a = 1;
*/
//任何类型的值和字符串使用连接符,最后会变成字符串
System.out.println(3 + 4 + "hello");//分部计算,先进行数学运算,然后和字符串拼接7hello
System.out.println("hello" + 3 + 4);//第一个为String,后面会拼接,hello34
System.out.println('a' + 1 + "hello");//char可以进行数学运算,所以先相加后拼接98hello
//使用+进行运算时候,其中有字符串的情况下,从字符从之前的+开始后面都需要拼接
}
}
强制类型转换
强制将范围大的类型转换为范围小的类型
public class Test{
public static void main(String[] args) {
//强制类型转换
int a = 2110;
byte b =(byte)a;
System.out.println(b);//62,只把二进制形式的最后8位放进byte的存储区域
//强制类型转换可能造成数据的溢出
//浮点型转换成整型,直接丢掉小数部分,保留整数部分返回
double sss = 22.22;
int ss =(int)sss;
System.out.println(ss);//22
}
}
运算符
基本算数运算符
+,-,*,/,%
public class Test{
public static void main(String[] args) {
/*
int a = 10;
int b = 3;
System.out.println(a - b);
System.out.println(a + b);
System.out.println(a * b);
System.out.println(a / b);//两个int型,最后会转为int,只有整数,丢弃小数
System.out.println(a * 1.0 /b);//就会有小数
System.out.println(a % b);//1,
*/
//实例,数值拆分,三位数拆分打印
int data = 234;
int ge = data % 10;
int shi = data / 10 % 10;
int bai = data / 100;
System.out.println(bai);
System.out.println(shi);
System.out.println(ge);
}
}
+,做连接符
+,与字符串进行运算的时候用作连接符,结果依然是一个字符串
自增,自减
public class Test{
public static void main(String[] args) {
//自增自减
//int a = 10;
//int b = a++;//先赋值后运算
//int a = 10;
//int b = ++a;//先运算后赋值
//int a = 10;
//int b = a--;//先赋值后运算
//int a = 10;
//int b = --a;//先运算后赋值
//案例
int c = 10;
int d = 5;
int rs3 = c++ + ++c - --d - ++d + 1 + c;
System.out.println(c);//12
System.out.println(d);//5
System.out.println(rs3);//26
}
}
赋值运算符
+=,-=,*=,/=,%=
会自动使用,强制类型转换
public class Test{
public static void main(String[] args) {
//赋值运算符
int i= 10;
i += 1;
System.out.println(i);
int a = 10;
a -= 2;//a = (int)(a-2),进行强制类型抓换
System.out.println(a);
byte b = 10;
byte n = 20;
b += n;//b = (byte)(b+n)强制类型转换
System.out.println(b);
}
}
关系运算符
对数据进行条件判断的符号,最终返回一个boolean
==,<= ,>=,<,>,!=
public class Test{
public static void main(String[] args) {
//关系运算符
int a = 10;
int b = 10;
boolean rr = a == b;
boolean tt = a != b;
System.out.println(rr);
System.out.println(tt);
}
}
逻辑运算符
异或^:两个值相同结果为假,两个值不同结果为真
- 异或:两个值相同时为假,两个值不同时为真
- &:无论左边真假右边都要参与运算
- &&:左边为真时,右边参与运算,左边为假时,右边不参与运算
- ||:左边为真右边不参与运算
a | b | a&b | a|b | !a | a^b | a&&b | a||b |
---|---|---|---|---|---|---|---|
true | true | true | true | false | false | true | true |
true | false | false | true | false | true | false | true |
flase | true | false | true | true | true | false | true |
flase | flase | false | false | true | false | false | false |
public class Test{
public static void main(String[] args) {
//逻辑运算符
double size = 99;
double ss = 16;
System.out.println(size > 80 & ss > 2);
System.out.println(size > 80 | ss > 288);
System.out.println(size > 80 & ss > 2);
}
}
三元运算符
条件表达式 ? 表达式1 :表达式2;
条件表达式为真,执行表达式1 ,否则执行表达式2
public class Test{
public static void main(String[] args) {
/*
//三元运算符
double s = 99;
String ss = s >= 60 ? "考试通过" : "挂科";
System.out.println(ss);
//案例,两个整数最大值
int a = 10;
int b = 2000;
int max = a > b ? a : b;
System.out.println(max);
*/
//三个整数最大值
int a = 10;
int b = 20;
int c = 30;
int max = a > b ? a : (b > c ? a : c);
System.out.println(max);
}
}
运算符的优先级
键盘录入
API应用程序编程接口
-
java写好的程序,可以直接调用
-
提供了api文档,代表得到键盘扫描器对象
import java.util.Scanner;
//导入包
public class Test{
public static void main(String[] args) {
try (//得到键盘扫描器对象
var SC = new Scanner(System.in)) {
//调用sc对象的功能,等待接收用户输入的数据
//SC.nextInt();//等待用户输入数据,直到用户输入完成,安下回车键
System.out.println("输入年龄");
int age = SC.nextInt();
System.out.println("年龄是" + age);
System.out.println("输入名称");
String name = SC.next();
System.out.println("名称是" + name);
}
}
}
程序流程控制
if分支结构
import java.util.Scanner;
import javax.lang.model.util.ElementScanner14;
//导入包
public class Test{
public static void main(String[] args) {
//if分支结构,格式1:if(条件表达式){代码..}
int heartBeat = 30;
if(heartBeat < 60 || heartBeat > 100){
System.out.println("数据是," + heartBeat + ",不合适");
}
System.out.println("结束");
//格式2,if(条件表达式){代码...} slse {代码...}
double money = 9999;
if(money >= 666){
System.out.println("有钱");
}
else{
System.out.println("穷逼");
}
//格式3,if(){..}else if(){...}
int score = 199;
if(score >= 0 && score < 60){
System.out.println("c");
}else if(score >= 60 && score < 80){
System.out.println("b");
}else if(score >= 80 && score < 90){
System.out.println("a");
}else if(score >= 90 && score <= 100){
System.out.println("a+");
}else{
System.out.println("输入不正确");
}
}
}
switch分支结构
匹配条件执行分支
- 执行表达式的值,使用值和case后的值进行匹配
- 匹配哪个case为true就执行,遇到break,跳出switch
- 如果都不匹配,执行default
注意事项
- 表达式类型只能是byte,int,char,jdk5开始枚举,jdk7开始String,不支持double,float,long
- case给出的值只能是字面量,不能是变量
- 要写break,不然会穿透
import javax.print.DocFlavor.STRING;
public class Test{
public static void main(String[] args) {
//switch
String day = "周三";
switch (day){
case "周一":
System.out.println("今天周一");
break;
case "周二":
System.out.println("今天周二");
break;
case "周三":
System.out.println("今天周三");
break;
case "周四":
System.out.println("今天周四");
break;
case "周五":
System.out.println("今天周五");
break;
case "周六":
System.out.println("今天周六");
break;
case "周日":
System.out.println("今天周日");
break;
default:
System.out.println("今天周八");
}
//
}
}
- 穿透现象。多个case分支的功能代码一样,可以利用穿透性把流程集中起来出来,简化代码
import javax.print.DocFlavor.STRING;
public class Test{
public static void main(String[] args) {
//穿透
int m = 7;
switch (m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(m + "月31天");
break;
case 2:
System.out.println(m + "月闰年29天,非闰年28");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(m + "月30天");
break;
default:
System.out.println("数据错误");
}
}
}
for循环结构
//if (初始化语句;循环条件;迭代语句)
if (int i = 0; i < 3; i++){
System.ouy,println("helloword")
}
public class Test{
public static void main(String[] args) {
/*
//穿透
for(int i = 0; i < 3; i++){
System.out.println("helloword");
}
//案例,1-5的和
int sum = 0;//进行存储
for (int i = 1; i < 6; i++) {
sum += i;//累加
}
System.out.println(sum);
//1-10之间的奇数和,不能被2整除的数叫做奇数
int sum = 0;
for (int i = 1; i < 11; i++) {
if (i % 2 == 1){
sum += i;
}
}
System.out.println(sum);
int sum = 0;
for (int i = 1; i < 11; i += 2) {
//1.3.5.7.9
sum += i;
}
System.out.println(sum);
*/
//水仙花数。三位数,个位,十位,百位,数字的立方=原数
int count = 0; //统计个数
for (int i = 100; i < 1000; i++) {
//识别除个位,十位,百位
int bai = i / 100;
int shi = i / 10 % 10;
int ge = i % 10;
//判断是不是水仙花数
if (i == bai * bai * bai + shi * shi * shi + ge * ge * ge){
System.out.print(i + "\t");
count += 1;
}
}
System.out.println("\n" + count + "个");//先换行
}
}
while循环
- 知道循环次数用for,不知道循环次数用while
初始化语句
while (循环条件){
循环体语句(被重复执行的代码);
迭代语句;
}
public class Test{
public static void main(String[] args) {
/*
//穿透
for(int i = 0; i < 3; i++){
System.out.println("helloword");
}
//案例,1-5的和
int sum = 0;//进行存储
for (int i = 1; i < 6; i++) {
sum += i;//累加
}
System.out.println(sum);
//1-10之间的奇数和,不能被2整除的数叫做奇数
int sum = 0;
for (int i = 1; i < 11; i++) {
if (i % 2 == 1){
sum += i;
}
}
System.out.println(sum);
int sum = 0;
for (int i = 1; i < 11; i += 2) {
//1.3.5.7.9
sum += i;
}
System.out.println(sum);
*/
//水仙花数。三位数,个位,十位,百位,数字的立方=原数
int count = 0; //统计个数
for (int i = 100; i < 1000; i++) {
//识别除个位,十位,百位
int bai = i / 100;
int shi = i / 10 % 10;
int ge = i % 10;
//判断是不是水仙花数
if (i == bai * bai * bai + shi * shi * shi + ge * ge * ge){
System.out.print(i + "\t");
count += 1;
}
}
System.out.println("\n" + count + "个");//先换行
}
}
do while
- 先执行在判断循环条件
初始化语句;
do{
循环体语句;
迭代语句;
}while(循环条件);
public class Test{
public static void main(String[] args) {
//do while,先执行一次,再判断
int i = 0;
do{
System.out.println("HELLO");
i += 1;
}while(i < 3);
}
}
死循环
- 一致循环执行下去,没有干预不会停下来
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
//for (;;) {
// System.out.println("hello");
//}
//while(true){//经典写法
// System.out.println("word");
//}
//do{
// System.out.println("sss");
//}while(true);
//案例
int truepasswd = 520;//正确密码
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入正确密码");
int passwd = sc.nextInt();
if(passwd == truepasswd){
System.out.println("登录成功");
break;//可以跳出循环
}else{
System.out.println("密码错误");
}
}
}
}
循环嵌套
- break:用于跳出并结束循环,或者结束所在switch分支
- continue:跳出当前循环,进入下一次循环
public class Test{
public static void main(String[] args) {
//break,continue
for (int i = 0; i < 5; i++) {
System.out.println("heoole");
if (i == 2){
break;
}
}
//continue,跳出循环档次执行,进入下一次执行
for (int i = 1; i < 6; i++) {
if(i==3){
continue;//跳出当此执行,进入下一次
}
System.out.println("hello" + i);
}
}
}
Random类,随机数
- nestint(n),功能只能生成,0-(n-1)之间的随机数
- random r = new random();
- int number = r.nextint(10) + 1 //1-10
import java.util.Random;
public class Test{
public static void main(String[] args) {
//JAVA随机数
//1.导包
//2.创建随机数对象
Random r = new Random();
//3.调用nestint功能,可以返回一个整型的随机数
for (int i = 0; i < 20; i++) {
int data = r.nextInt(10);
System.out.println(data);
}
//1-10==>-1==>(0-9)+1
//3-17==>-3==>(0-14)+3
int data = r.nextInt(10) + 1;
System.out.println(data);
}
}
案例
import java.util.Random;
import java.util.Scanner;
import javax.lang.model.element.Element;
import javax.lang.model.util.ElementScanner14;
public class Test{
public static void main(String[] args) {
//猜数字游戏1-100
Scanner sc = new Scanner(System.in);//键盘录入新对象
Random num = new Random();//随机数新对象
int data = num.nextInt(100) + 1;//减加法,生成1-100
while(true){
System.out.println("请猜测数字1-100");//提示信息
int number = sc.nextInt();//接收用户输入的数字
if(data == number){
System.out.println("***猜对了***" + "\n");
break;//猜对退出
}else if(data > number){
System.out.println("猜小了" + "\n");
}else{
System.out.println("猜大了" + "\n");
}
}
}
}
数组
- 用来存储一批数据类型相同的数据
10,20,30,40,50
int[] arr={10,20,30,40,50};
张三 李四
String[] names={"张三","李四"}
数组定义
- 静态初始化数组
- 当前已知道存入到数据,用静态初始化
- 当前还不清楚存入的数据,用动态初始化
定义数组的时候给数组赋值
数组一旦定义出来,程序执行的 过程中,长度和类型就固定了
public class Test{
public static void main(String[] args) {
//静态初始化数组
//数据类型[] 数组名称 = new 数据类型[]{元素1,元素2}
//String[] name = new String[]{"n","v"};
String[] name ={"n","v"};//简化写法
System.out.println(name);
//数组变量名中存储的是数组在内存中的地址,数组是引用类型
}
}
- 数组的访问
public class Test{
public static void main(String[] args) {
int[] ages = {22,33,44,55};//定义数组
//取值
System.out.println(ages[1]);
//赋值
ages[2] = 100;
System.out.println(ages[2]);
//访问数组长度
System.out.println(ages.length);
//最大索引,长度-1
}
}
- 动态初始化数组
在定义数组时,只确定元素的类型和数组的长度,之后再存入具体数据。
元素默认值:
基本类型 | byte,short,int,long,char | 0 |
---|---|---|
基本类型 | float,double | 0.0 |
基本类型 | boolean | false |
引用类型 | 类,接口,数组 ,String | NULL |
int[] arr = new int[3];
//后赋值
int[0] = 10;
public class Test{
public static void main(String[] args) {
//数组动态初始化
double[] score = new double[3];//默认值0.0,0.0,0.0
//赋值
score[0] = 9.9;
//System.out.println(score[0]);
//System.out.println(score[2]);//输出默认值
String[] names = new String[900];
names[0] = "dilireba";
names[30] = "di";
System.out.println(names[0]);
System.out.println(names[3]);
System.out.println(names[30]);
}
}
数组的遍历
把数据一个一个访问
public class Test{
public static void main(String[] args) {
int[] numm = {11,22,33,44,55,66,77,88,99};
//数组的遍历
for (int i = 0; i < numm.length; i++) {
System.out.println(numm[i]);
}
//数组元素求和
int sum = 0;//存储和
for (int i = 0; i < numm.length; i++) {
sum += numm[i];
}
System.out.println(sum);
}
}
案例1
public class Test{
public static void main(String[] args) {
//数组求最值
int[] face = {15,200,24,56,789};//静态初始化数组
int max = face[0];
for (int i = 0; i < face.length; i++) {
if(face[i] > max){
max = face[i];
}
}
System.out.println(max);
}
}
案例2
猜数字,随机生成1-20,5个数字,猜中输出索引和所有答案
import java.util.Random;
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
int[] num = new int[5];//动态初始化数组
//生成5个1-20之间的数字
Random r = new Random();//初始化一个新对象
Scanner sc = new Scanner(System.in);//扫描器对象
for (int i = 0; i < num.length; i++) {
num[i] = r.nextInt(20) +1;
}
//死循环,用户进行猜测
OUT://退出死循环
while(true){
System.out.println("输入1-20之间的数字");
int guessDate = sc.nextInt();//接收输入的
for (int i = 0; i < num.length; i++) {
if(guessDate == num[i]){
System.out.println("索引是[" + i + "]答对了");
break OUT;//代表结束整个死循环
}
}
System.out.println("数据不存在");
}
//猜对后,退出死循环,进入遍历
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] + "\t");
}
}
}
案例3
随机排序
import java.util.Random;
import java.util.Scanner;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;
public class Test{
public static void main(String[] args) {
////动态初始化数组
int[] Num = new int[5];
//循环输入5次
Scanner Sc = new Scanner(System.in);//键盘输入
for (int i = 0; i < Num.length; i++) {
System.out.print("输入第" + (i +1) + "工号");
int data = Sc.nextInt();
Num[i] = data;//将键盘输入的数据,存在数组里
}
//遍历数组中的元素,随机索引一个出来,该元素和随机索引位置的元素进行交换
Random r = new Random();
for (int i = 0; i < Num.length; i++) {
int index = r.nextInt(Num.length);
int b = Num[i];//临时变量
Num[i] = Num[index];
Num[index] = b;
}
//变量数组元素输出,随机名单
System.out.println("随机排序结果是");
for (int i = 0; i < Num.length; i++) {
System.out.print(Num[i] + "\t");
}
}
}
案例4-冒泡排序
冒泡排序:每次从数组中找到最大的值放在数组的后面,从头开始两辆比较,每轮把最大的元素存入到数组当前末尾
- 对数组中的元素,进行升序或者降序的排序
public class Test{
public static void main(String[] args) {
//定义数组
int[] arr = {5,4,3,2};
//定义一个循环,控制笔记轮数
for (int i = 0; i < arr.length - 1; i++) {
//控制每轮比较次数,占位
//i==1,比较三次
//i==1,比较两次
//i==1,比较一次
for (int j = 0; j < arr.length - i - 1; j++) {
//i是0,arr.length - i,后面就需要-1
//判断j当前位置的元素值,是否大于后面的位置
if(arr[j] > arr[j+1]){
int b = arr[j+1];
arr[j+1] = arr[j];
arr[j] = b;
}
}
}
//遍历数组输出
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + "\t");
}
}
}
java内存分配
- 方法区:字节码文件加载时进入的内存
- 栈内存:方法运行时,进入的内存,变量也在这里
- 堆内存:new出来的对象,再这块内存中开辟空间并产生地址
- 两个数组变量指向同一个数组对象
public class Test{
public static void main(String[] args) {
int[] arr = {11, 22, 33};
int[] arr1 = arr;//地址赋值给arr1
System.out.println(arr);
System.out.println(arr1);
}
}
数组常见问题
- 如果访问的元素超过最大索引,执行会出现,数组索引越界异常
- 数组变量中如果没有存储地址,是null,会出现空指针异常
方法
方法的定义,使用
- 一种语法结构,可以把一段代码封装成一个功能,方便重复调用
public class Test{
public static void main(String[] args) {
//调用
int c = summ(2, 4);
System.out.println(c);
pp();
}
//定义
public static int summ(int a, int b){
int c = a + b;
return c;
}
//无返回值类型void,方法里面不需要使用return返回数据
//无参数,无返回值
public static void pp(){
for (int i = 0; i < 3; i++) {
System.out.println("hello");
}
}
/*
方法的编写顺序无所谓
方法与方法之间平级,不能嵌套定义
方法返回值是void时,方法内不能使用return返回数据
方法的返回值写了具体类型,方法内部使用return返回对应类型
return下面的语句,不能编写代码,永远执行不到,无效语句
调用方法时,匹配方法的参数
有返回值的方法,可以选择定义变量接收结果,或者直接输出调用
无返回值直接调用
*/
}
案例
public class Test{
public static void main(String[] args) {
System.out.println(sunmm(10));
System.out.println(sunmm(100));
ch(4);
ch(13);
int[] arg = {11, 22, 33, 444, 55, 555};
System.out.println(maxx(arg));
}
//定义1-n的和并返回
public static int sunmm(int n){
int sum = 0;
for (int i = 0; i < n + 1 ; i++) {
sum += i;
}
return sum;
}
//判断整数是奇数还是偶数
public static void ch(int n){
if(n % 2 == 1){
System.out.println ( n + "奇数");
}else{
System.out.println ( n + "偶数");
}
}
//找数组中的最大值
public static int maxx(int[] arr){
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
}
方法的参数传递机制
- 基本类型的参数传递
在传输实参给方法的形参时候,并不是传输实参变量本身,传输实参中存储的值
public class Test{
public static void main(String[] args) {
//java基本类型的参数传递,值传递
int a = 10;
change(a);
System.out.println(a);//10
}
public static void change(int a){
System.out.println(a);//10
a = 20;
System.out.println(a);//20
}
}
- 引用类型的参数传递
引用类型传值,传的是内存地址值
public class Test{
public static void main(String[] args) {
int[] arr = {1, 2, 4};
change(arr);
System.out.println(arr[1]);
}
//引用类型的参数传递
public static void change(int[] arr){
System.out.println(arr[1]);
arr[1] = 333;
System.out.println(arr[1]);
}
}
方法接收应用类型值,案例
public class Test{
public static void main(String[] args) {
int[] arr = {11, 22, 33, 44, 55};
pp(arr);
int[] sss = null;
pp(sss);
}
//用于输出任意整型数组的内容,格式要求
public static void pp(int[] arrr){
System.out.print("[");
if(arrr != null && arrr.length > 0){
for (int i = 0; i < arrr.length; i++) {
/*
if(i == arrr.length -1){
System.out.print(arrr[i]);
}else{
System.out.print(arrr[i] + ", ");
}
*/
//三位运算符
System.out.print(i == arrr.length ? arrr[i] : arrr[i] + ",");
}
}
System.out.println("]");
}
}
public class Test{
public static void main(String[] args) {
int arr[] = {11, 22, 33};
int a = 33;
int tt = searchoIndex(arr, a);
System.out.println(a + "的索引是" + tt);
}
//从整型数组中查询元素索引返回
//接收整型数组,和要查询的元素值,返回元素在数组中的索引,如果数组中不存在该元素则返回-1
public static int searchoIndex(int[] arr, int a){
for (int i = 0; i < arr.length; i++) {
if(a == arr[i]){
return i;
}
}
return -1;
}
}
public class Test{
public static void main(String[] args) {
int[] a1 = {11, 22, 33};
int[] a2 = {11, 22, 33};
int[] a3 = {11, 22};
System.out.println(compare(a1, a3));
}
//比较两个数组内容是否一样
//数组类型,元素个数,元素顺序,内容
public static boolean compare(int[] a1, int[] a2){
if(a1.length == a2.length){
for (int i = 0; i < a2.length; i++) {
if(a1[i] != a2[i] ){
return false;
}
}
return true;
}else{
return false;
}
}
}
方法重载
- 同一个类中,出现多个方法名称相同,但是形参列表是不同的,这些方法就是方法重载
- 可读性好,方法名称相同提示同一类型提示,通过形参不同实现差异化功能
import java.security.cert.PKIXBuilderParameters;
public class Test{
public static void main(String[] args) {
fire();
fire("b");
fire("b", 8);
}
//方法重载
public static void fire(){
fire("B地");
}
public static void fire(String location){
fire(location, 1);
}
public static void fire(String location, int f){
System.out.println("向" + location + "发" + f + "个");
}
}
方法重载的识别技巧
-
同一个类中,方法名称相同,形参列表不同,就是方法重载
-
形参列表不同:形参的个数,类型,顺序不同
return关键字
return;可以立即跳出并结束当前方法的执行
import java.security.cert.PKIXBuilderParameters;
public class Test{
public static void main(String[] args) {
chu(2, 0);
}
//return关键字
public static void chu(int a, int b){
if(b == 0){
System.out.println("错误");
return;
}
int c = a / b;
System.out.println(c);
}
}
编程案例
案例1
输入机票原价,月份,头等舱或经济舱,5-10月头等9折,经济8.5这,11--4月。头等七折,经济6.5折
import java.security.cert.PKIXBuilderParameters;
import java.util.Scanner;
import javax.lang.model.util.ElementScanner6;
import javax.print.DocFlavor.STRING;
public class Test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入机票原价");
double money = sc.nextDouble();
System.out.println("输入机票月份(1-12)");
int month = sc.nextInt();
System.out.println("选择仓位类型");
String type = sc.next();
//System.out.println("机票优惠后的价格是" + calc(money, month, type));
System.out.println("机票优惠后的价格是" + cc(money, month, type));
}
//先进行月份判断,然后和使用switch进行仓位判断,进行计算
public static double calc(double money, int month, String type){
if(month >= 5 && month <= 10){//进行月份的判断
switch(type){
case "头等舱":
money *= 0.9;
break;
case "经济舱":
money *= 0.85;
break;
default:
System.out.println("输入有误");
money = -1;
}
}else if(month == 11 || month == 12 || month >= 1 && month <= 4){
switch(type){
case "头等舱":
money *= 0.7;
break;
case "经济舱":
money *= 0.65;
break;
default:
System.out.println("输入有误");
money = -1;
}
}else{
System.out.println("输入的月份出错");
money = -1;
}
return money;
}
}
案例2
- 信号位
找素数
package moc;
public class model2 {
/*判断101到200之间的素数,并输出
除1和本身之外,不能被其他正整数整除,就是素数*/
public static void main(String[] args) {
//1.定义循环,101-200之间数据
for (int i = 101; i < 200; i++) {
//信号位:标记
boolean flag = true;//一开始认为当前数据是素数
//2.判断遍历的数据是不是素数
for (int j = 2; j < i / 2; j++) {
if(i % j == 0){
flag = false;
break;
}
}
//3.根据判定输出
if(flag){
System.out.print(i + "\t");
}
}
}
}
案例3
验证码
package moc;
import java.util.Random;
import java.util.Scanner;
public class model3 {
public static void main(String[] args) {
//调用获取验证码的方法
System.out.println(createCode());
}
/**
1.定义一个方法,返回验证码,String,声明形参
*/
public static String createCode(){
Scanner sc = new Scanner(System.in);
Random r = new Random();//生成随机的新对象
StringBuilder code = new StringBuilder();//空字符串存储
//2.生成5位验证码
System.out.println("输入验证码位数");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
//3.生成随机字符,数字,英文大小写
int type = r.nextInt(3);//0,1,2
switch(type){
case 0:
//大写字符(A65- Z65+25)
char ch = (char) (r.nextInt(26) + 65);
code.append(ch);
break;
case 1:
//小写字符(a97-z97+25)
char ch1 = (char) (r.nextInt(25) + 97);
code.append(ch1);
break;
case 2:
code.append(r.nextInt(10));//0-9
//数字
break;
}
}
return code.toString();
}
}
案例4
数组元素的复制
package moc;
public class model4 {
public static void main(String[] args) {
int[] arr1 = {11, 22, 33, 44};
int[] arr2 = new int[arr1.length];
copy(arr1, arr2);
pp(arr1);
pp(arr2);
}
public static void copy(int[] arr1, int[] arr2){
//完成元素复制
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
public static void pp(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1? arr[i] : arr[i] + ",");
}
System.out.println("]");
}
}
案例5
评委打分
package moc;
import java.util.Scanner;
public class model5 {
/*
6个人打分,1-100
去掉最高和最低,得出四人平均分
*/
public static void main(String[] args) {
int[] arr = new int[6];
//录入分数
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
System.out.println("输入第"+ (i + 1) + "个的分");
int score = sc.nextInt();
arr[i] += score;//分数存进去
}
//找最大值,最小值,总分
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
//统计总分
sum += arr[i];
}
//统计平均
double score = (sum - max -min) * 1.0 / (arr.length - 2);
System.out.println("最终得分" + score);
}
}
案例6
数字加密
package moc;
import java.util.Scanner;
public class model6 {
/*
* 先得到每位数字,对每位数字加5,对10求余,最后将所有数字反转,得到一串新数*/
public static void main(String[] args) {
//定义数组,存入需要加密的数据
Scanner sc = new Scanner(System.in);
System.out.println("输入要加密几位数字");
int num = sc.nextInt();
int[] arr = new int[num];
//录入需要加密的数组
for (int i = 0; i < arr.length; i++) {
System.out.println("输入要加密的第" + (i +1) + "个数字");
int number = sc.nextInt();
arr[i] = number;
}
//打印原数组内容
printp(arr);
//对数组中的数据进行加密
for (int i = 0; i < arr.length; i++) {
arr[i] = ( arr[i] +5 ) % 10;
}
//对数组中加密的数据反转
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
//交换位置的值
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
//打印加密后数组
printp(arr);
}
//打印数组
public static void printp(int[] arr){
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ", ");
}
System.out.println("]");
}
}
案例7
双色球
package moc;
import java.util.Random;
import java.util.Scanner;
public class model7 {
public static void main(String[] args) {
//生成
int[] lll = createLuckNumber();
//输入
int[] uuu = userInputNumber();
//对比
diff(lll, uuu);
}
//生成双色球号码
public static int[] createLuckNumber(){
//定义一个动态初始化数组,存储7个数字
int[] number = new int[7];
//遍历前6个,生成不重复的6个红球号码/1-33
Random r = new Random();
for (int i = 0; i < number.length - 1; i++) {
while (true) {//找到不重复的数据
int data = r.nextInt(33) + 1;//1-33
//判断随机的号码,是否重复
boolean flag = true;
for (int j = 0; j < i; j++) {
if(number[j] == data){
//数据之前出现过
flag = false;
break;
}
}
if(flag){
number[i] = data;
break;
}
}
}
//第⑦个数字
number[number.length - 1] = r.nextInt(16) + 1;
return number;
}
//用户录入7个号码
public static int[] userInputNumber(){
//定义数组存储7个号码
int[] numbers = new int[7];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < numbers.length - 1; i++) {
System.out.println("请输入第" + (i + 1) + "个红球号码(1-33,不重复)");
int data = sc.nextInt();
numbers[i] = data;
}
//单独第7位
System.out.println("输入蓝球号码1-16");
numbers[6] = sc.nextInt();
return numbers;
}
//判断中奖情况
public static void diff(int[] arr1,int[] arr2){
int sum = 0;//中红球的数量
for (int i = 0; i < arr1.length - 1; i++) {
for (int j = 0; j < arr2.length - 1; j++) {
if(arr1[i] == arr2[j]){
sum += 1;
break;//数组中不重复。判断到一个就可以退出
}
}
}
int lan = 0;
if(arr1[arr1.length - 1] == arr2[arr2.length - 1]){
lan += 1;
}
System.out.println("中奖的号码是" );
model6.printp(arr1);//生成
System.out.println("投注的是");
model6.printp(arr2);//输入
System.out.println("红球中了" + sum + "个,蓝球中了" + lan + "个");
//奖金
if(lan ==1 && sum < 3){
System.out.println("5$");
}else if((lan ==1 && sum == 3) | sum == 4 && lan == 0){
System.out.println("10$");
} else if((lan ==1 && sum == 4) | sum == 5 && lan == 0){
System.out.println("200$");
}else if(lan ==1 && sum == 5){
System.out.println("3000$");
}else if(lan ==0 && sum == 6){
System.out.println("500万$");
}else if(lan ==1 && sum == 6){
System.out.println("1000万$");
}else {
System.out.println("血亏");
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)