java练习题2

练习题:求数字的绝对值

绝对值是指一个数在数轴上所对应点到原点的距离,用“| |”来表示。负数的绝对值是他去掉负号以后的值,而非负数(0和正数)的绝对值是他本身。请定义一个方法,接收用户输入的数字,输出绝对值结果。

package cn.cxy.exec;

import java.util.Scanner;

public class TestNum {
    public static void main(String[] args) {
        //1.接收用户输入的要测试的数字
        System.out.println("请输入你要判断的数字:");
        double input = new Scanner(System.in).nextDouble();
        //2.调用求绝对值的方法并拿到方法执行后的结果(返回值)
        double result = getAbsNum(input);
        //3.打印求出的结果
        System.out.println(input+"的绝对值是:"+result);
    }

    private static double getAbsNum(double input) {
        if(input >=0){//如果是非负数
            return input;//返回本身的值
        }else{//如果是负数
            return -input;//则值取反
        }
    }

}
 

 

练习题:输入数字1~7,输出对应星期几 

package cn.cxy.exec;

import java.util.Scanner;

public class TestDay {
    public static void main(String[] args) {
        //1.提示并接收用户输入的数据
        System.out.println("请输入数据:");
        int input = new Scanner(System.in).nextInt();
        //2.判断用户输入的数据
        if(input == 1) {
            System.out.println("星期一");
        }else if(input == 2){
            System.out.println("星期二");
        }else if(input == 3){
            System.out.println("星期三");
        }else if(input == 4){
            System.out.println("星期四");
        }else if(input == 5){
            System.out.println("星期五");
        }else if(input == 6){
            System.out.println("星期六");
        }else if(input == 7){
            System.out.println("星期日");
        }
    }
}
 

 

练习题:接收用户输入的数据,判断是奇数还是偶数 

package cn.cxy.exec;

import java.util.Scanner;
public class TestNum {
    public static void main(String[] args) {
        //1.提示并接收用户输入的数据
        System.out.println("请输入您要判断的数据");
        int input = new Scanner(System.in).nextInt();
        //2.判断用户输入的数据
        if(input % 2 == 0) {
            System.out.println(input+"是偶数");
        }else {
            System.out.println(input+"是奇数");
        }
    }
}
 

 

练习题:生成一个顺序数组,将这个数组的元素打乱顺序后输出 

 

package cn.cxy.exec;

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

public class ShuffleArray {
    public static void main(String[] args) {
        //调用f()方法,从方法获取一个int[]数组
        int[] a = f();
        //遍历打印数组数据
        for(int i=0; i<a.length; i++) {
            System.out.println(a[i]);
        }
        System.out.println("\n\n----------------");

        //把a数组,传递到 shuffle() 方法打乱顺序
        shuffle(a);
        //打印乱序后的数组
        System.out.println(Arrays.toString(a));
    }

    public static int[] f() {
        //新建int[]数组,长度5
        //再把它的内存地址存到变量 a
        int[] a = new int[5];
        //遍历访问5个位置,填入1,2,3,4,5
        for(int i=0; i<a.length; i++) {
            a[i] = i+1;
        }
        //返回数组,把数组返回到调用位置
        //本质是把数组地址返回去
        return a;
    }

    public static void shuffle(int[] a) {
        /*
         *        j
         * [4, 2, 3, 1, 5]
         *     i
         *
         * *) i循环遍历数组
         * *) 随机定位下标j与i交换
         */
        for (int i = 0; i < a.length; i++) {
            //随机下标j,范围:[0, a.length)
            int j = new Random().nextInt(a.length);
            int t = a[i];
            a[i] = a[j];
            a[j] = t;
        }
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值