Arrays和Math类(牛刀小试)

题目1:随机输入一个字符串,将其升序排序后倒序输出。

import java.util.Scanner;
import java.util.Arrays;
public class Demo4 {
    public static void main(String[] args){

        //1.动态输入一个字符串
        System.out.println("请输入一个字符串:");
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();

        //2.将字符串转换为字符数组
        char[] transomStr = str.toCharArray();

        //3.调用升序方法
        Arrays.sort(transomStr);

        //4.倒序输出该字符数组的字符\
        System.out.println("升序之后倒序输出的结果为:");
        for(int i = transomStr.length - 1; i >= 0 ; i --){
            System.out.print(transomStr[i]);
        }
    }
}

题目2:用一个Math类方法,计算出-10.8~5.9之间绝对值小于2.1或者大于6的整数有哪些?这样的整数一个有多少个?

public class Demo3 {
    public static void main(String[] args){

        //1.在草稿纸上计算出这些整数在哪个具体区间-->[-2.1,2.1]U[-10.8,-6)

        //2.调用方法使2.1和-10.8取整数为2和10
        //public static long round(double a):返回最接近参数的long。(相当于四舍五入方法)
        double min1 = Math.round(-2.1);
        double min2 = Math.round(2.1);

        //public static double floor(double a):返回小于等于参数最大的整数
        double max = Math.floor(-10.8) + 1;

        //3.通过循环语句求出[-10.8,5.9]区间的这些整数:[-2.1,2.1]U[-10.8,-6)
        int count = 0;
        System.out.print("这些数分别是:");
        for(double num = Math.round(-10.8); num <= Math.round(5.9); num ++){
            if((num >= min1 && num <= min2) || (num >= max && num < -6)){
                System.out.print(num + "\t");
                count ++;
            }
        }
        System.out.println();
        System.out.println("一共有" + count + "个数。");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值