java 练习题
2021-01-16 19:14
                         标签:tar   length   赋值   方式   ati   创建   案例   演示   tin    需求:输入星期数[1,7],显示今天的减肥活动  实现步骤:     需求: 实现步骤:   需求:   需求: 要求: 实现步骤:   需求(不定义方法): 实现步骤:   案例需求(定义方法):  实现步骤:定义方法,获取一个int数据,在int数组中第一次出现的索引值,如果没有出现返回-1  三要素:  getIndex方法的实现步骤:  main方法的实现步骤:   图解: 需求:交换两个int变量的值 步骤:借助于第三个int变量   图解: 需求:交换int数组两个元素的值  思考:如何定义一个方法,实现一个int数组中,两个位置的元素进行交换呢?  三要素:   图解: 案例需求(不定义方法): 图解: 代码实现(不定义方法):   java 练习题 标签:tar   length   赋值   方式   ati   创建   案例   演示   tin    原文地址:https://www.cnblogs.com/boyw/p/12922583.html减肥计划if版本
public class IfJF {
    public static void main(String[] args) {
        //1.创建键盘录入Scanner类的对象
        Scanner sc = new Scanner(System.in);
        //2.获取键盘录入的1-7的整数数字(代表星期数),保存到int变量week中
        System.out.println("请输入一个1-7的整数数字(代表星期数):");
        int week = sc.nextInt();
        //3.因为week中的数字有7+1种情况,所以使用if语句的第三种格式对week中的数据进行判断,并输出不同的结果
        //先判断无效数字
        if (week  7) {
            System.out.println("您输入的数字有误...............");
        } else if (week == 1) {
            System.out.println("跑步");
        } else if (week == 2) {
            System.out.println("游泳");
        } else if (week == 3) {
            System.out.println("慢走");
        } else if (week == 4) {
            System.out.println("动感单车");
        } else if (week == 5) {
            System.out.println("拳击");
        } else if (week == 6) {
            System.out.println("爬山");
        } else   // if (week == 7)
            System.out.println("好好吃一顿");
        }
        System.out.println("main....end...");
    }
}
减肥计划switch版本
public class SwitchJF {
    public static void main(String[] args) {
        //1.创建键盘录入Scanner类的对象
        Scanner sc = new Scanner(System.in);
        //2.获取键盘录入的1-7的整数数字(代表星期数),保存到int变量week中
        System.out.println("请输入一个1-7的整数数字(代表星期数):");
        int week = sc.nextInt();
        //3.因为week中的数字有7+1种情况,所以使用switch语句对week中的数据进行判断,并输出不同的结果
        switch (week) {//5
            case 1:
                System.out.println("跑步");
                break;
            case 2:
                System.out.println("游泳");
                break;
            case 3:
                System.out.println("慢走");
                break;
            case 4:
                System.out.println("动感单车");
                break;
            case 5:
                System.out.println("拳击");
                break;
            case 6:
                System.out.println("爬山");
                break;
            case 7:
                System.out.println("好好吃一顿");
                break;
            default:
                System.out.println("您输入的数字有误.....");
                break;
        }
        System.out.println("main...end....");
    }
}
逢七跳过
public class FQG {
    public static void main(String[] args) {
        //定义int变量count,初始值0,用来统计满足逢七过规则的数据
        int count = 0;
        //使用for循环获取1-100之间的数字,循环变量int类型num
        for (int num = 1; num ) {
            //计算当前num中数字的个位和十位,分别保存到int变量ge和shi中
            int ge = num % 10;//个位
            int shi = num / 10 % 10;//十位
            //如果 个位 等于 7 或者 十位 等于 7 或者 num中的数字是7的倍数
            if ((ge==7 || shi==7) || (num%7==0)) {
                //打印num中的数字
                System.out.println(num);
                //计数器count的值增加1
                count++;
            }
        }
        //循环结束打印count的值
        System.out.println("符合逢七过的数字总共有: "+count+" 个");
    }
}
数组元素求和
实现步骤:
public class Array {
    public static void main(String[] args) {
        int count = 0;
        int[] array = {68,27,95,88,171,996,51,210};
        //3.使用for循环遍历数组
        for (int i = 0; i ) {
            //计算当前元素的个位和十位,分别保存到int变量ge和shi中
            int ge = array[i]%10;//个位
            int shi = array[i] / 10 % 10;//十位
            //判断如果 个位 不是 7 并且 十位 不是 7  并且 当前元素是偶数
            if ((ge != 7 && shi != 7) && (array[i] % 2 == 0)) {
                //打印当前元素值
                System.out.println(array[i]);
                //计数器count的值增加1
                count++;
            }
        }
        //循环结束后,打印count的值
        System.out.println("满足条件的元素数量: "+count);
    }
}
数组统计学生成绩
public class Score {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        //获取键盘录入的整数数字(代表班级人数), 保存到int变量size中
        System.out.println("请输入班级人数(整数数字):");
        int size = sc.nextInt();
        //创建长度为size的int数组array,只能采用动态初始化方式创建数组
        int[] array = new int[size];
        //按照要求产生size个随机数字,存储到数组array
        for (int i = 0; i ) {
            array[i] = r.nextInt(101);
        }
        //打印数组array中的元素(目的: 看数组中有没有存储对应的随机数字)
        for (int i = 0; i ) {
            System.out.println(array[i]);
        }
        System.out.println("---------------");
        //定义int变量count,初始值0,用来统计不及格人数
        int count = 0;
        //定义int变量sum,初始值0,用来统计所有学生的成绩之和
        int sum = 0;
        //使用for循环遍历数组
        for (int i = 0; i ) {
            //把当前元素累加到求和变量sum中
            sum += array[i];
            //如果当前元素值 小于 60 计数器count的值增加1
            if (array[i] ) {
                count++;
            }
        }
        //for循环结束后,根据总分sum计算出平均分,保存到int变量avg中
        int avg = sum / (array.length);
        //打印count和avg的值
        System.out.println("不及格人数:" + count);
        System.out.println("班级平均分:" + avg);
    }
}
查找元素在数组中第一次出现的索引位置
public class Demo01GetValue {
    public static void main(String[] args) {
        int[] array = {19, 28,37, 46, 50};
        Scanner sc = new Scanner(System.in);
        // 获取键盘录入的整数数字(要查找的数字),保存到int变量value中
        System.out.println("请输入您要查找的元素(整数数字): ");
        int value = sc.nextInt();
        // 定义int变量index,初始值-1,作用保存找到的元素的索引值
        int index = -1;
        // 使用for循环遍历数组
        for (int i = 0; i ) {
            // 如果当前元素值 等于 要查找的元素value
            if (array[i] == value) {
                // 把当前元素的索引值 赋值 给 index 结束循环break
                index = i;
                break;
            }
        }
        // 循环结束直接打印变量index的值
        System.out.println("元素值"+value+"在数组中的索引值: "+index);
    }
}
public class Demo02GetValue {
    public static void main(String[] args) {
        int[] array = {19, 28,37, 46, 50};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要查找的元素(整数数字): ");
        int value = sc.nextInt();
        // 调用getIndex方法,传递数组array和要查找的元素value,获取索引值保存到int变量index中
        int index = getIndex2(value, array); 
        System.out.println("元素值"+value+"在数组中的索引值: "+index);
    }
    //定义方法,获取一个int数据,在int数组中第一次出现的索引值,如果没有出现返回-1
    public static int getIndex(int value, int[] array) {
        //定义int变量index,初始值-1,作用保存找到的元素的索引值
        int index = -1;
        //使用for循环遍历数组
        for (int i = 0; i ) {
            // 如果当前元素值 等于 要查找的元素value
            if (array[i] == value) {
                // 把当前元素的索引值 赋值 给 index 结束循环break
                index = i;
                break;
            }
        }
        // 循环结束返回index的值
        return index;
    }
    //定义方法,获取一个int数据,在int数组中第一次出现的索引值,如果没有出现返回-1
    public static int getIndex2(int value, int[] array) {
       // 使用for循环遍历数组
        for (int i = 0; i ) {
            // 如果当前元素值 等于 要查找的元素value
            if (array[i] == value) {
                // 直接返回当前元素的索引值
                return i;
            }
        }
        //执行到这里,说明数组中是没有value这个元素的,直接返回-1
        return -1;
    }
}

两个int变量值的交换
public class Demo01SwapVar {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("a="+a);
        System.out.println("b="+b);
        //把变量a的值,(赋值)复制一份保存到int变量temp中
        int temp = a;
        //把变量b的值,(赋值)复制一份保存到int变量a中
        a = b;
        //把变量temp的值,(赋值)复制给变量b
        b = temp;
        System.out.println("a="+a);
        System.out.println("b="+b);
    }
}        

数组元素的交换
public class Demo02SwapArray {
    public static void main(String[] args) {
        int[] arr = {10,20};
        System.out.println("arr[0]="+arr[0]);
        System.out.println("arr[1]="+arr[1]);
       //把数组arr中索引0的元素值,赋值给int变量temp
        int temp = arr[0];
        //把数组arr中索引1的元素值,赋值给数组arr中索引为0的元素
        arr[0] = arr[1];
        //把temp中的值,赋值给数组arr中索引为1的元素
        arr[1] = temp;
        System.out.println("arr[0]="+arr[0]);
        System.out.println("arr[1]="+arr[1]);
    }
    //定义一个方法,实现一个int数组中,两个位置的元素进行交换
    public static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
}            

数组反转分析
实现步骤:

上一篇:java常量池