Java作业——方法和字符串

本文介绍了Java编程中的基础概念和实用技巧,包括使用for循环打印倒直角三角形,判断字符串对称性,计算阶乘,求解偶数之和,找出能被7整除的数字数量,去除字符串中的空格,计算平均数,以及输出100到1000之间的素数。内容覆盖了循环控制、字符串操作、条件判断及算法实现等核心知识点。
摘要由CSDN通过智能技术生成

1.打印倒直角三角形
*****
****
***
**
*

public static void main(String[] args) {
        for(int i=0;i<5;i++){
            for(int j=0;j<5-i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

2. 控制台输入一个字符串,判断这个字符串是否对称 abcba算对称 abccba也算对称

public static void main(String[] args) {
        while(true) {//循环输入
            System.out.print("请输入字符串:");
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();//键盘输入字符串

            int number = str.length();
            
            int a = 0;
            //比较第i个字符和倒数第i个值是否相同
            for (int i = 0; i <= number / 2 - 1; i++) {

                if (str.charAt(i) == str.charAt(number - 1 - i)) {
                    a++;//记录相同的组数的个数

                }
            }
            System.out.println("该字符串有" + a + "组字符对称");
            if (a == number / 2) {//字符相同的组数的个数等于字符串的总组数
                System.out.println("字符串对称");
            } else {
                System.out.println("字符串不对称");

            }
        }
    }

3. 一、键盘输入一个正整数,求这个数的阶乘
二、求 1!+2!+3!+4!+5!

//键盘输入一个正整数,求这个数的阶乘
public class pro3 {
    public static void main(String[] args) {
        while (true) {
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();

            System.out.println(a+"的阶乘是"+jiecheng(a));//调用jiecheng方法,输入参数是a;

        }
    }
    public static int jiecheng(int a){//定义求阶乘的方法
        int result=1;
        for(int i=1;i<=a;i++){
            result =result*i;

        }
        return result;
    }
}

public class pro0301 {
    public static void main(String[] args) {
        while(true){
            Scanner sc=new Scanner(System.in);
            int x=sc.nextInt();
            int result=0;
            for(int i=1;i<=x;i++){//循环调用阶乘方法计算,1~x之间所有数的阶乘和
                result=result+jiecheng(i);

            }
            System.out.println("1~"+x+"之间的数的阶乘和为:"+result);
        }
    }
    public static int jiecheng(int x){//定义计算一个整数的阶乘方法
        int result=1;
        for(int i=1;i<=x;i++){

            result=result*i;
        }
        return result;
    }
}

4.使用while循环完成计算1到100之间偶数之和

public static void main(String[] args) {
        int i=0;
        int sum=0;
        while(i<=100){

            if(i%2==0){
                sum=sum+i;
            }
            i++;
        }
        System.out.println("1~100之间偶数之和为"+sum);

    }

5.使用while循环,统计1-100范围内,有多少数字可以被7整除

 public static void main(String[] args) {


        int i=1;
        int number=0;
        while(i<=100){
            if(i%7==0){
                number++;
            }
            i++;
        }
        System.out.println(number);
    }

6.String str= "abc god 中国 java"去掉字符串中的空格。
结果:String str2 = "abcgod中国java"

 public static void main(String[] args) {
        String str="abc  god     中国                java";

        for(int i=0;i<str.length();i++){

            if(str.charAt(i)!=' '){
                System.out.print(str.charAt(i));
            }

        }

    }

7.定义一个求平均数的方法 avg 传入三个int类型参数 返回这三个数的平均数

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

        System.out.println(avg(11,2,3));
    }
    public static double avg(int a,int b,int c){
        double sum=0;
        sum=(a+b+c)/3.0;
        return sum;
    }
}

8.输出100 到 1000之间的所有素数

public class pro8 {
    public static void main(String[] args) {
        for (int i = 100; i <= 1000; i++) {
            sushu(i);
        }

    }

    public static void sushu(int x) {

        boolean flag=true;
        for (int i = 2; i < x; i++) {
            if (x % i == 0) {
                flag=false;//不是素数的时候,flag=flase
                break;
            }
        }
        if(flag==true){
            System.out.println(x+" ");
        }

    }
}

9.String str= “abc god 中国 java” 反转每个单词
"cba dog 国中 avaj"

String str="abc  god     中国                java";
        str=str+" ";//字符串结尾加上空格,方便最后一个单词的反转打印
        String temp="";//存放反转的数组元素
        String result="";//存放不断打印的结果数组
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)!=' '){//元素不是空格的时候,单词反转,用temp记录
                temp=str.charAt(i)+temp;
            }
            else//元素是空格的时候,temp和空格打印在一起,同时清空temp,因为不清空会被重复加上
            {
                result=result+temp+" ";
                temp="";
            }
        }
        result.trim();//trim方法,清除字符串前后的空格
        System.out.println(result);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值