方法
- 方法是程序中最小的执行单元
- 重复的代码、具有独立功能的代码可以抽取到方法中
- 方法的好处:提高代码复用性、可维护性
tips:
- 方法不调用就不执行
- 方法与方法之间是平级关系,不能互相嵌套定义
- 方法的编写顺序与执行顺序无关
- 方法的返回值类型为void,表示该方法没有返回值,没有返回值的方法可以省略return语句不写,如果要编写return,后面不能跟具体的数据
- return语句下面,不能编写代码,因为永远执行不到,属于无效代码
圆的面积:
public class method {
public static void main(String[] args) {
int r ;//圆的面积
Scanner sc = new Scanner(System.in);
System.out.println("请输入圆的半径!");
r = sc.nextInt();
getCircleArea(r);
}
public static void getCircleArea(int i){
double area;
area = i*i* Math.PI;//圆的面积
System.out.println("圆的面积为"+area);
}
}
方法重载
-
在同一个类中,定义了多个同名的方法,这些同名的方法具有同种的功能
-
每个方法具有不同的参数类型或参数个数,这些同名的方法,就构成了重载关系
tips:同一个类中,方法名相同,参数不同的方法,与返回值无关。
参数不同:个数不同,类型不同,顺序不同(可以重载,但是不建议)
练习:
遍历数组+查找数组元素最大值+判断元素是否存在于数组+拷贝某段数组
public class MethodPractice {
public static void main(String[] args){
int [] array = new int[7];//创建一个含有4个元素的数组
int max;//定义最大值
boolean flag;//创建布尔常量
int e;//指定判断是否存在的数字
System.out.println("请输入需要遍历的数组:");
Scanner sc = new Scanner(System.in);
for (int i = 0 ; i < array.length; i++) {
array [i] = sc.nextInt();
}
throughArray(array);
max = getMax(array);
System.out.println("最大值为:" + max);
System.out.println("请输入需要判断数组是否存在的数值:");
e = sc.nextInt();
flag = whetherExists(array,e);
if (flag == true){
System.out.println(e+"存在于数组中");
}else {
System.out.println(e+"不存在于数组中");
}
System.out.println("请输入需要复制的数组从第几个到第几个:");
System.out.print("from:");
int f = sc.nextInt();
System.out.print("to:");
int t = sc.nextInt();
int [] newArray = copyArray(array,f,t);
System.out.println("拷贝后的新数组如下:" + "\n");
throughArray(newArray);
}
//遍历数组
public static void throughArray(int [] array){
System.out.print("[");
for (int i = 0; i < array.length - 1 ; i++) {
System.out.print(array[i] + ",");
}
System.out.println(array[array.length-1]+"]");
}
//取最大值
public static int getMax(int [] array){
int max = 0;
for (int i = 0; i < array.length; i++) {
if ( array[i] > max){
max = array[i];
}
}
return max;
}
//判断元素是否存在数组
public static boolean whetherExists(int [] a,int b){
for (int i = 0; i < a.length; i++) {
if (b == a [i]){
return true;
}
}
return false;
}
//拷贝数组
public static int[] copyArray(int a [],int from,int to){
int [] b = new int[ to - from +1];
int index = 0;
for (int i = from - 1; i < to; i++) {
b [index] = a[i];
index++;
}
return b;
}
}
java内存分配
方法区:字节码文件加载时进入的内存
栈内存:方法运行时所进入的内存
堆内存:new出来的东西会在这块内存中开辟空间并产生地址
栈:先进后出 队列:先进先出
基本数据类型:数据值是存储在自己的空间中。
特点:赋值给其他变量,也是赋的真实值
eg.
int a = 1;
引用数据类型(一般new出来的),变量中存储的是地址值,引用:使用了其他空间中的数据
int[] array1 = {1,2,3,4};
int[] array2 = array1;//array2和array1指向同一个地址,其中一个数据改变,另外一个数组的数据也会改变,(改变的是地址中的值)
方法传递基本数据类型的内存原理:
传递基本数据类型时,传递的是真实的数据。形参的改变,不影响实际参数的值
public static void main (String[] args){
int number = 1;//基本数据类型int
System.out.println("调用update方法前:" + number);
update(number);
System.out.println("调用update方法后:" + number);
}
public static void update(number){
number = 1;
}//答案都为1
方法传递引用数据类型的内存原理
传递引用数据类型时,传递的是地址值。形参的改变,影响实际参数的值
public static void main (String[] args){
int [] array = {1,2,3};//基本数据类型int
System.out.println("调用update方法前:" + array[1]);
update(array);
System.out.println("调用update方法后:" + array[1]);
}
public static void update(int[] array){
array[1] = 10
}//2
//10
计算票价:
public class BookTickets {
public static void main(String[] args) {
int ticket, month, seat;
Scanner sc = new Scanner(System.in);
//计算票价
System.out.println("请输入车票原价:");
ticket = sc.nextInt();//获取车票价格
System.out.println("请输入当前月份:");
month = sc.nextInt();//获取当前月份
System.out.println("请选择经济舱还是头等舱(0为经济舱场,1为头等舱):");
seat = sc.nextInt();//获取座位
if (month >= 5 && month <= 10) {//旺季
ticket = getTicket(seat, ticket, 0.85, 0.9);
} else if (month >= 11 && month <= 12 || month >= 1 && month <= 4) {//淡季
ticket = getTicket(seat, ticket, 0.7, 0.65);
} else {
System.out.println("输入不合法!");
}
System.out.println("你的车票价格为:" + ticket);
}
private static int getTicket(int seat, int ticket, double x, double x1) {
if (seat == 0) {//经济舱
ticket = (int) (ticket * x);
} else if (seat == 1) {//头等舱
ticket = (int) (ticket * x1);
} else {
System.out.println("输入不合法!");
}
return ticket;
}
}
判断素数:
质数(又称素数),是指在大于1的自然数中,除了1和它本身外,不能被其他自然数整除(除0以外)的数称之为素数(质数)。 比1大但不是素数的数称为合数,1和0既非素数也非合数。
public class WhetherPrime {
public static void main(String[] args) {
int from, to, all = 0;//需要判断素数的范围
boolean flag = true;//是素数
Scanner sc = new Scanner(System.in);
System.out.println("请输入判断有多少个素数的范围:");
System.out.println("from:");
from = sc.nextInt();
System.out.println("to:");
to = sc.nextInt();
for (int i = from; i <= to; i++) {//遍历判断素数的范围
flag = whetherPrime(i);
if (flag) {
System.out.println(i + "是素数");
all++;
} else {
System.out.println(i + "不是素数");
}
}
System.out.println(from + "到" + to + "之间有" + all + "个素数!");
}
public static boolean whetherPrime(int prime) {
boolean flag = true;
if (prime <= 1) {
Scanner sc = new Scanner(System.in);
System.out.println("输入素数有误!请重新输入:");
} else {
for (int i = 2; i < prime; i++) {
if (prime % i == 0) {
flag = false;
break;
} else {
flag = true;
}
}
}
return flag;
}
}
验证码练习:
public class IdentifyCode {
public static void main(String[] args) {
String result = "";//定义一个结果
char[] c = new char[52];//定义一个存放52个大小写英文字母的数组
for (int i = 0; i < c.length; i++) {
if (i < 26) {//小写字母
c[i] = (char) (97 + i);
} else {//存放大写字母
c[i] = (char) (65 + i - 26);
}
}
Random r = new Random();
for (int i = 0; i < 4; i++) {
int randomIndex = r.nextInt(c.length);
result = result + c[randomIndex];
}
int randomNumber = r.nextInt(10);
result = result + randomNumber;
System.out.println("生成的验证码为:" + result);
}
}
评委打分:
public class GetScore {
public static void main(String[] args) {
int[] score = new int[6];
int[] newscore = new int[4];
int max, min;
double result = 0;
System.out.println("请输入出六位评委的打分:");
for (int i = 0; i < score.length; i++) {
Random s = new Random();
score[i] = s.nextInt(101);
}
max = score[1];
min = score[1];
System.out.println("评委1:" + score[0] + "评委2:" + score[1] + "评委3:" + score[2] + "评委4:" + score[3] + "评委5:" + score[4] + "评委6:" + score[5]);
for (int i = 1; i < score.length; i++) {
if (max < score[i]) {//找出最高分
max = score[i];
} else if (min > score[i]) {//找出最低分
min = score[i];
}
}
System.out.println("最高分为:" + max + "最低分为:" + min);
int j = 0;
for (int i = 0; i < score.length; i++) {
if (score[i] != max && score[i] != min) {
newscore[j] = score[i];
j++;
}
}
for (int i = 0; i < newscore.length; i++) {
result += newscore[i];
}
System.out.println("四位评委总分:" + result);
result = result / 4;
System.out.println("该选手最后得分为:" + result);
}
}
加密解密:
public class SecretNumber {
public static void main(String[] args) {
System.out.println("请输入4位需要加密的数字:");
Scanner sc = new Scanner(System.in);
int[] array = new int[4];
for (int i = 0; i < array.length; i++) {
array[i] = sc.nextInt();//写入需要加密的数字
}
for (int i = 0; i < array.length; i++) {
array[i] = array[i] + 5;//每位加5
}
for (int i = 0; i < array.length; i++) {
array[i] = array[i] % 10;//每位对10取余
}
for (int i = 0, j = array.length - 1; i < j; i++, j--) {
//反转所有数字
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
//拼接每一位数字
int number = 0;
for (int i = 0; i < array.length; i++) {
number = number * 10 + array[i];
}
System.out.println("加密后的数字为:" + number);
System.out.println("需要解密的数字为:");
for (int i = 0; i < array.length; i++) {
array[i] = sc.nextInt();//写入需要解密的数字
}
for (int i = 0, j = array.length - 1; i < j; i++, j--) {
//反转所有数字
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
for (int i = 0; i < array.length; i++) {
if (array[i] >= 0 && array[i] <= 4) {
array[i] = array[i] + 10;//根据+5取余操作,实现反解
}
}
for (int i = 0; i < array.length; i++) {
array[i] = array[i] - 5;
}
number = 0;
for (int i = 0; i < array.length; i++) {
number = number * 10 + array[i];
}
System.out.println("解密后的数字为:" + number);
}
}
模拟双色球:
public class DoubleColorBall {
public static void main(String[] args) {
int[] luckyNumber = createLuckyNumber();
int[] userLuckyNumber = userLuckyNumber();
System.out.println("彩票号码为:");
iterateThrough(luckyNumber);
System.out.println("");
System.out.println("您的号码为:");
iterateThrough(userLuckyNumber);
System.out.println("");
judgeLuckyNumber(luckyNumber, userLuckyNumber);
}
public static void judgeLuckyNumber(int[] luckyNumber, int[] userLuckyNumber) {
int redHitNumber = 0;
int blueHitNumber = 0;
for (int i = 0; i < userLuckyNumber.length - 1; i++) {//判断红球命中多少个
for (int j = 0; j < luckyNumber.length - 1; j++) {
if (userLuckyNumber[i] == luckyNumber[j]) {
redHitNumber++;
break;
}
}
}
blueHitNumber = luckyNumber[6] == userLuckyNumber[6] ? 1 : 0;//判断蓝球命中多少个
if (blueHitNumber == 1 && redHitNumber < 3) {
System.out.println("恭喜您,您中奖了5元");
} else if (blueHitNumber == 1 && redHitNumber == 3 || blueHitNumber == 0 && redHitNumber == 4) {
System.out.println("恭喜您,您中奖了10元");
} else if (blueHitNumber == 0 && redHitNumber == 5 || blueHitNumber == 1 && redHitNumber == 4) {
System.out.println("恭喜您,您中奖了200元");
} else if (blueHitNumber == 1 && redHitNumber == 5 || blueHitNumber == 0 && redHitNumber == 6) {
System.out.println("恭喜您,您中奖了3000元");
} else if (blueHitNumber == 1 && redHitNumber == 6) {
System.out.println("恭喜您,您中奖了10000元");
} else {
System.out.println("很遗憾,你未中奖!");
}
}
//生成6个红球(红球号码不重复,范围是1-33)加一个蓝球(1-16)
public static int[] createLuckyNumber() {
Random r = new Random();
int[] number = new int[7];
for (int i = 0; i < number.length - 1; i++) {//获取前6个随机数
while (true) {//定义死循环
int data = r.nextInt(33) + 1;//生成随机数
boolean flag = true;//flag标记无重复
for (int j = 0; j < i; j++) {
if (number[j] == data) {
flag = false;
break;
}
}
if (flag) {
number[i] = data;//将第i个位置赋值为data
break;//跳出while循环
}
}
}
number[number.length - 1] = r.nextInt(16) + 1;//在第7个号码上生成一位1-16中的随机数
return number;//返回一个含有六个不重复的随机数的数组
}
public static int[] userLuckyNumber() {
int[] number = new int[7];
Scanner sc = new Scanner(System.in);
System.out.println("请输入6个红球号码和1个蓝球号码!");
for (int i = 0; i < number.length - 1; i++) {
number[i] = sc.nextInt();//依次获取6个红球号码
}
number[number.length - 1] = sc.nextInt();
return number;
}
public static void iterateThrough(int[] number) {//遍历方法
for (int i = 0; i < number.length; i++) {
System.out.print(number[i] + "\t");
}
}
}