使用方法的好处是?
package com.ith.create;
public class methoddemo1 {
public static void main(String[] args) {
//张工
int c1 = sum(30,10);
System.out.println(c1);
//徐工
int c2 = sum(20,30);
System.out.println(c2);
}
public static int sum(int a,int b){
int c = a+b;
return c;
}
}
package com.ith.demo;
public class test1 {
public static void main(String[] args) {
System.out.println("1-100的和为:" + sum(100));
System.out.println("1-5的和为:" + sum(5));
}
public static int sum(int n){
int sum = 0;
for (int i = 0; i <= n; i++) {
sum+=i;
}
return sum;
}
}
//1-100的和为:5050
//1-5的和为:15
package com.ith.demo;
public class test2 {
public static void main(String[] args) {
// 判断数的奇偶
check(9);
System.out.println("----------");
check(100);
}
public static void check(int number){
if(number%2 == 0){
System.out.println(number + "是偶数");
}else{
System.out.println(number + "是奇数");
}
}
}
/*
9是奇数
----------
100是偶数
*/
需求:
把找出数组的最大值案例,改造成方法,可以支持返回任意整型数组的最大值数据。
package com.ith.demo;
public class test3 {
public static void main(String[] args) {
int[] ages = {23,19,45,67,86,32};
int max = getArrayMaxData(ages);
System.out.println("最大值数据是:" + max);
}
public static int getArrayMaxData(int[] arr){
//找出数组最大值
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
}
/*
最大值数据是:86
*/
在传输实参给方法的形参的时候,并不是传输实参变量本身,而是传输实参变量中存储的值,这就是值传递。
package com.ith.param;
public class demo1 {
public static void main(String[] args) {
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
}
}
1.形参和实参各指什么?
2.Java的参数传递机制是什么样的?
package com.ith.param;
public class demo2 {
public static void main(String[] args) {
int[] arrs = {10,20,30};
change(arrs);
System.out.println(arrs[1]);///222
}
public static void change(int[] arrs){
System.out.println("方法内部:"+arrs[1]);//20
arrs[1] = 222;
System.out.println("方法内部:"+arrs[1]);//222
}
}
1、基本类型和引用类型的参数在传递的时候有什么不同?
package com.ith.param;
public class test3 {
public static void main(String[] args) {
int[] ages = {10,20,30,40};
printArray(ages);
System.out.println("----------");
int[] numbers = {1,2,3,8,30};
printArray(numbers);
System.out.println("----------");
int[] numbers1 = null;
printArray(numbers1);
}
public static void printArray(int[] arr){
if(arr != null && arr.length>0){
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(("]"));
}
}
}
/*
[10,20,30,40]
----------
[1,2,3,8,30]
----------
*/
package com.ith.param;
public class test4 {
public static void main(String[] args) {
int [] arr = {11,22,33,44,55,77};
int index = searchIndex(arr,77);
System.out.println("您查询数据的索引是:" + index);
System.out.println("-------------");
index = searchIndex(arr,17);
System.out.println("您查询数据的索引是:" + index);
}
public static int searchIndex(int[] arr,int data){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == data){
return i;
}
}
return -1;
}
}
/*
您查询数据的索引是:5
-------------
您查询数据的索引是:-1
*/
package com.ith.param;
public class test5 {
public static void main(String[] args) {
int[] arr1 = {10,20,30};
int[] arr2 = {10,20,30};
System.out.println(compate(arr1, arr2));//true
}
public static boolean compate(int[] arr1,int[] arr2){
if(arr1.length == arr2.length){
for (int i = 0; i < arr1.length; i++) {
if(arr1[i] != arr2[i]){
return false;
}
}
return true;
}else{
return false;
}
}
}
方法重载:同一个类中,出现多个方法名称相同,但是形参列表是不同的,那么这些方法就是重载方法。
package com.ith.overload;
public class demo1 {
public static void main(String[] args) {
fire();
fire("小日子");
fire("小日子",5);
}
public static void fire(){
System.out.println("默认发射武器给米国");
}
public static void fire(String location){
System.out.println("默认发射武器给"+location);
}
public static void fire(String location,int num){
System.out.println("默认发射"+num+"枚武器给"+location);
}
}
/*
默认发射武器给米国
默认发射武器给小日子
默认发射5枚武器给小日子
*/
1.方法重载是什么样的?
2.使用方法重载的好处?
3.方法重载的关键要求是什么样的?
4.形参列表不同指的是什么?
需求:
分析:
代码:
package com.ith.demo;
import java.util.Scanner;
//机票优惠价格
public class test1 {
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));
}
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;
break;
}
}
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;
break;
}
}
else{
System.out.println("您输入的月份错误!!!");
money = -1;
}
return money;
}
}
/*输出
请输入机票原价:
1000
请输入机票的月份(1-12):
12
请您选择仓位类型:
头等舱
机票优惠后的价格是:700.0
*/
需求:
代码:
package com.ith.demo;
public class test2 {
public static void main(String[] args) {
for (int i = 101; i < 200; i++) {
//信号位:标记
boolean flag = true;
for (int j = 2; j < i/2; j++) {
if(i%j==0){
flag = false;
break;
}
}
if(flag){
System.out.print (i+"\t" );
}
}
}
}
/*输出
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
*/
需求:
分析:
代码:
package com.ith.demo;
import java.util.Random;
public class test3 {
public static void main(String[] args) {
String code = createCode(5);
System.out.println("随机验证码:" + code);
}
public static String createCode(int n){
String code = " ";
Random r = new Random();
for(int i=0;i<n;i++){
int type = r.nextInt(3);
switch (type){
case 0:
//大写字符
char ch = (char)(r.nextInt(26)+65);
code += ch;
break;
case 1:
//小写字符
char ch1 = (char)(r.nextInt(26)+97);
code += ch1;
break;
case 2:
//数字
code += r.nextInt(10);
break;
}
}
return code;
}
}
/*输出
随机验证码: QWfBk
*/
需求:
分析:
代码:
package com.ith.demo;
public class test4 {
public static void main(String[] args) {
int[] arr1 = {11,22,33,44};
int[] arr2 = new int[arr1.length];
copy(arr1,arr2);
printArray(arr1);
printArray(arr2);
}
public static void printArray(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("]");
}
public static void copy(int[] arr1,int[] arr2){
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
}
}
/*输出
[11, 22, 33, 44]
[11, 22, 33, 44]
*/
需求∶
分析:
代码:
package com.ith.demo;
import java.util.Scanner;
public class test5 {
public static void main(String[] args) {
int[] scores = new int[6];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < scores.length; i++) {
System.out.println("请输入第" + (i + 1) + "个评委的打分:");
int score = sc.nextInt();
scores[i] = score;
}
int max = scores[0];
int min = scores[0];
int sum=0;
for (int i = 0; i < scores.length; i++) {
if(scores[i] > max){
max = scores[i];
}
if(scores[i] < max){
max = scores[i];
}
sum+=scores[i];
}
double result = 1.0*(sum-max-min)/(scores.length-2);
System.out.println("选手最终得分:" + result);
}
}
/*输出
请输入第1个评委的打分:
100
请输入第2个评委的打分:
90
请输入第3个评委的打分:
77
请输入第4个评委的打分:
45
请输入第5个评委的打分:
23
请输入第6个评委的打分:
12
选手最终得分:58.75
*/
需求:
分析:
代码:
package com.ith.demo;
import java.util.Scanner;
public class test6 {
public static void main(String[] args) {
System.out.println("请输入加密数字个数:");
Scanner sc = new Scanner(System.in);
int length = sc.nextInt();
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入加密的第" + (i + 1) + "个数字:");
int number = sc.nextInt();
arr[i] = number;
}
System.out.print("加密之前:");
printArray(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 temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
System.out.print("加密之前:");
printArray(arr);
}
public static void printArray(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("]");
}
}
/*
请输入加密数字个数:
4
请输入加密的第1个数字:
1
请输入加密的第2个数字:
9
请输入加密的第3个数字:
8
请输入加密的第4个数字:
3
加密之前:[1, 9, 8, 3]
加密之前:[8, 3, 4, 6]
Process finished with exit code 0
*/
投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1—33中选择;蓝色球号码从1—16中选择。
package com.ith.demo;
import java.util.Random;
import java.util.Scanner;
public class test7 {
public static void main(String[] args) {
//创造幸运号码
int[] luckNumber = createLuckNumber();
//printArray(luckNumber);
//用户选号
int[] userNumbers = userInputNumbers();
//printArray(userNumbers);
judge(luckNumber,userNumbers);
}
//随机生成中奖号码
public static int[] createLuckNumber(){
int[] numbers = new int[7];
Random r=new Random();
//以下是六个红球,1-33
for (int i = 0; i < numbers.length-1; i++) {
while(true){
int data = r.nextInt(33)+1;
boolean flag = true;
//以下查重
for(int j=0;j<i;j++){
if(data == numbers[j]){
flag = false;
break;
}
}
if(flag){
numbers[i] = data;
break;
}
}
}
//以下是一个蓝球1-16
numbers[numbers.length-1] = r.nextInt(16) + 1;
//返回
return numbers;
}
//用户输入的号码
public static int[] userInputNumbers(){
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;
}
System.out.println("请输入蓝球号码(1-16):");
numbers[6] = sc.nextInt();
//返回
return numbers;
}
//打印数组
public static void printArray(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("]");
}
//判断是否中奖
public static void judge(int[] luckNumbers,int[] userNumbers){
//红球中奖个数
int redHitNumbers = 0;
//蓝球中奖个数
int blueHitNumbers = 0;
//红球中奖情况
for (int i = 0; i < userNumbers.length-1; i++) {
for (int j = 0; j < luckNumbers.length-1; j++) {
if(luckNumbers[j] == userNumbers[i]){
redHitNumbers++;
break;
}
}
}
//蓝球中奖情况
blueHitNumbers = luckNumbers[luckNumbers.length-1] == userNumbers[userNumbers.length-1] ? 1:0;
System.out.println("中奖号码是:");
printArray(luckNumbers);
System.out.println("您投注的号码是:");
printArray(userNumbers);
System.out.println("红球中奖个数:" + redHitNumbers);
System.out.println("蓝球"+((blueHitNumbers == 1)?"中奖了":"没中奖"));
//计算中奖情况
if(redHitNumbers < 3 && blueHitNumbers == 1){
System.out.println("中奖5元!!!");
}else if(redHitNumbers==3 && blueHitNumbers == 1 || redHitNumbers==4 && blueHitNumbers == 0 ){
System.out.println("中奖10元!!!");
}else if(redHitNumbers==4 && blueHitNumbers == 1 || redHitNumbers==5 && blueHitNumbers == 0 ){
System.out.println("中奖200元!!!");
}else if(redHitNumbers==5 && blueHitNumbers == 1){
System.out.println("中奖3000元!!!");
}else if(redHitNumbers==6 && blueHitNumbers == 0 ){
System.out.println("中奖500万元!!!");
}else if(redHitNumbers==6 && blueHitNumbers == 1 ){
System.out.println("中奖1000万元!!!");
}else{
System.out.println("感谢你为福利事业做出的贡献!!");
}
}
}
/*
请输入第1个红球号码(1-33,要求不能重复):
1
请输入第2个红球号码(1-33,要求不能重复):
2
请输入第3个红球号码(1-33,要求不能重复):
3
请输入第4个红球号码(1-33,要求不能重复):
4
请输入第5个红球号码(1-33,要求不能重复):
5
请输入第6个红球号码(1-33,要求不能重复):
6
请输入蓝球号码(1-16):
7
中奖号码是:
[17, 28, 7, 5, 25, 20, 9]
您投注的号码是:
[1, 2, 3, 4, 5, 6, 7]
红球中奖个数:1
蓝球没中奖
感谢你为福利事业做出的贡献!!
*/