Bootstrap

Java学习Day5

方法

2.1 什么是方法?

  • 功能
  • 方法
    • 最简单的格式

2.2 定义方法

  • add方法
  • m1
  • 代码

2.3 方法的调用

  • 赋值调用(方法必须要有返回值)
    • 变量 = 方法名();
  • 直接调用:没有返回值
    • 方法名()

2.4 方法练习

编写方法三点!
1.我要干什么---决定方法体!
2.need什么东西才能干成---决定形式参数!
3.是否需要返回调用处---决定返回值及其类型!

例1:

import java.util.Scanner;

public class ten {
    public static void main(String[] args) {
        int list[] = extracted();
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i]);
        }
        int mac = extracted1(list);
        int min = extracted2(list);
        System.out.println("mac-xz=" + mac);
        System.out.println("min=" + min);
        for (int i = 0; i < list.length; i++) {
            if (list[i] == mac | list[i] == min) {
                list[i] = 0;
            }
        }
        int num = 0;
        int sum = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i] != 0) {
                System.out.println("有效分数为:" + list[i]);
                num++;
            }
            sum += list[i];
        }
        System.out.println("平均分是" + (sum / num));
    }
     static int[] extracted() {
        int [] numlist =new int[6];
        System.out.println("输入6位评委打分!");
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < numlist.length; ) {//数组名称.for i;不要i++因为错误数据会使循环跳过空位
            int num = sc.nextInt();
            if (num>0&&num<=100){
                numlist[i]=num;
                System.out.println("第"+(i+1)+"位评委分数是"+numlist[i]);
                i++;
            }
            if (num<0||num>100){
                System.out.println("错误数据!当前的i为"+i+"请重新输入:");
            }
        }
        return numlist;
    }
    static int extracted1(int [] list) {
        int ma = list[0];
        for (int i = 1; i < list.length; i++) {
           if (ma<list[i]){
               ma=list[i];
           }
        }
        return ma;
    }
    static int extracted2(int [] list) {
        int mi = list[0];
        for (int i = 1; i < list.length; i++) {
            if (mi>list[i]){
                mi=list[i];
            }
        }
        return mi;
    }
}

例2:

附带解密!

import java.util.Scanner;
public class ele {
    public static void main(String[] args) {
        //1.取每一位
        System.out.println("请输入需要加密的密码:");
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        int miwen = inword(number);
        System.out.println(miwen);
        System.out.println("是否将加密的密码解密?( 1. Y )( 2. N ):");
        int number1 = sc.nextInt();
        if (number1==1){
            //调用outword
            int yuanwen =jiemi(miwen);
            System.out.println(yuanwen);
        }
        if (number1==2){
            System.out.println("加秘完成,当前为密文!");
        }
    }
    private static int jiemi(int miwen) {//解密模块
        int count = 0;
        int temp = miwen;
        int [] list2 =getLIST(miwen,count,temp);
        reverse(list2);
        ciecleoutput(list2);
        for (int i = 0; i < list2.length; i++) {//反解密%10操作
            if (list2[i]<=4){
                list2[i]+=10;
            }
            if (list2[i]>4){

            }
        }
        ciecleoutput(list2);
        for (int i = 0; i <list2.length ; i++) {//-5
            list2[i]-=5;
        }
        ciecleoutput(list2);
        int num1 = getNum(list2);
        System.out.println(num1);
        return num1;
    }

    private static int inword(int number) {
        int count =0;
        int temp= number;
        int[] list = getLIST(number, count, temp);
        for (int i = 0; i <list.length ; i++) {//+5
            list[i]+=5;
        }
        for (int i = 0; i < list.length; i++) {//%10
            list[i]%=10;
        }
        reverse(list);
        ciecleoutput(list);
        int num = getNum(list);
        return num;
    }

    private static int getNum(int[] list) {
        int num=0;
        for (int i = 0; i < list.length; i++) {//拼接
            num=num*10+ list[i];
        }
        return num;
    }

    private static void ciecleoutput(int[] list) {//Step:遍历数组
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i]);
        }
    }

    private static int[] getLIST(int number, int count, int temp) {//Step:把数字放入数组
        while (number !=0){//利用number/10找出数字位数,number=0
            number /=10;
            count++;
        }
        System.out.println(count);
        int [] list = new int[count];
        int inde = list.length-1 ;
        while (temp !=0){//利用temp实现求出每一位的数字,并录入list,但是顺序是倒序
            int out = temp % 10;
            list[inde]=out;
            temp = temp /10;
            inde--;
        }
        return list;
    }

    private static void reverse(int[] list) {//Step:反转倒序数组
        for (int i = 0, j = list.length-1; i < j; i++,j--) {
            int q= list[j];
            list[j]= list[i];
            list[i]=q;
        }
    }
}

例3:

import java.util.Random;
import java.util.Scanner;
public class twl {
    public static void main(String[] args) {
        System.out.println("欢迎抽奖!");
        Scanner sc = new Scanner(System.in);
        int arr[] ={2,288,388,588,988};
        boolean[] arr1 = new boolean[arr.length];
        a:for(;;){
            System.out.println("按“1”抽奖!,按“2”退出!");
            int num = sc.nextInt();
            if (num==1){
                Random randomint = new Random();
                int rnum =randomint.nextInt(arr.length);
                    for (int i = 0; i < arr1.length; i++) {
                        if (arr1[rnum]){
                            System.out.println("奖品"+arr[rnum]+"元现金已被抽出!");
                            break ;
                        }else {
                            arr1[rnum] = true; // 标记奖品为已抽中
                            System.out.println("您抽到的是"+rnum+"号奖品,奖品为"+arr[rnum]+"元现金!");
                            break ;
                        }
                        }
            }
            if (num==2){
                break;
            }
        }
    }
}

例4:

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class threeteen {
    public static void main(String[] args) {
        int [] arr1 =createNumber();
        System.out.println("********************");//打印中奖号码
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i]+" ");
        }
        System.out.println();
        System.out.println("********************");
        int [] arry2 =userInput();
//        for (int i = 0; i < arry2.length; i++) {
//            System.out.print(arry2[i]+" ");
//        }
        int redconut = 0;
        int bluecount = 0;
        for (int i = 0; i < arry2.length-1; i++) {
            int red = arry2[i];
            for (int j = 0; j < arr1.length-1; j++) {
                if (red==arr1[j]){
                    redconut++;
                    break;
                }
            }
        }
        for (int i = 0; i < arr1.length; i++) {
            int blue = arry2[arry2.length-1];
            if (blue==arr1[arr1.length-1]){
                bluecount++;
                break;
            }
        }
        System.out.println(redconut);
        System.out.println(bluecount);
        if (redconut==6&&bluecount==1){
            System.out.println("特等奖!1000万!");
        }
        if (redconut==6&&bluecount==0){
            System.out.println("一等奖!500万!");
        }
        if (redconut==5&&bluecount==1){
            System.out.println("3000!");
        }
        if ((redconut==5&&bluecount==0)||redconut==4&&bluecount==1){
            System.out.println("200!");
        }
        if ((redconut==4&&bluecount==0)||(redconut==3&&bluecount==1)){
            System.out.println("10!");
        }
        if ((redconut==2&&bluecount==1)||(redconut==1&&bluecount==1)||(redconut==0&&bluecount==1)){
            System.out.println("5!");
        }
    }
    public static int[] userInput(){//用户键入红球号码
        Scanner sc = new Scanner(System.in);
        int arry[] = new int[7];
        for (int i = 0; i < 6; ) {
            System.out.println("请输入红球第"+i+"个号码!");
            int output1 = sc.nextInt();
            if (output1>0&&output1<34){
                boolean flag = contains(arry,output1);
                if (!flag){
                    arry[i]=output1;
                    i++;
                }else {
                    System.out.println("号码存在!");
                }
            }else {
                System.out.println("不在范围内!");
            }
        }
        System.out.println("请输入蓝球号码!");
        int output2 = sc.nextInt();
        if (output2>0&&output2<17){//键入蓝球号码
            arry[arry.length-1]=output2;
        }else {
            System.out.println("不在范围内!");
        }return arry;
    }
    //生成中奖红球号码
    public static int[] createNumber(){
        int arry[] = new int[7];
        Random r =new Random();//随机生成号码添加到数组中
        for (int i = 0; i < 6; ) {
            int number1=r.nextInt(33)+1;
            boolean flag = contains(arry,number1);
            if (!flag){
                arry[i]=number1;
                i++;
            }
        }
        int number2 =r.nextInt(16)+1;
        arry[arry.length-1]=number2;
        return arry;
    }
    //生成蓝球号码添加到数组中
    public static boolean contains(int arry[],int number) {
        for (int i = 0; i <arry.length ; i++) {
            if (arry[i]==number){
                return true;
            }
        }return false;
    }
    //遍历返回01来确定是否添加
}

总结

1.for注意:

此种情况可以将i++下放,实现输入不重复,重复的话下标i并不会移动。

for (int i = 0; i < 6; ) {
        int number1=r.nextInt(33)+1;
        boolean flag = contains(arry,number1);
        if (!flag){
            arry[i]=number1;
            i++;
        }
    }
    int number2 =r.nextInt(16)+1;
    arry[arry.length-1]=number2;
    return arry;
}

1.布尔:

boolean[] arr1 = new boolean[arr.length];

for (int i = 0; i < arr1.length; i++) {
    if (arr1[rnum]){
        System.out.println("奖品"+arr[rnum]+"元现金已被抽出!");
        break ;
    }else {
        arr1[rnum] = true; // 标记奖品为已抽中
        System.out.println("您抽到的是"+rnum+"号奖品,奖品为"+arr[rnum]+"元现金!");
        break ;
    }
    }

可使用布尔数组boolean[] arr1来实现标记功能,也可以实现对比。

;