【Java基础】2.19 2.21 2.29 2.30

2.19

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args)
    {
        //Scanner sc=new Scanner(System.in);//构建一个数据,输入对象
        //Random random=new Random();
        //System.out.println("平局");
        printRectangle(3,5);
        printRectangle(2,4);
        printRectangle(6,10);
    }
    public static void printRectangle(int h,int w)
    {
        for(int i=0;i<h;i++) {
            for(int j=0;j<w;j++) {
                System.out.printf("*");
            }
            System.out.printf("\n");
        }
        System.out.printf("\n");
    }
}

2.21

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args)
    {
        int sum1=add(1,2);
        double sum2=add(1.0,2.2);
        int sum3=add(1,2,3);
        System.out.println("sum1="+sum1);
        System.out.println("sum2="+sum2);
        System.out.println("sum3="+sum3);
    }
    public static int add(int a,int b)
    {
        return a+b;
    }
    public static double add(double a,double b)
    {
        return a+b;
    }
    public static int add(int a,int b,int c)
    {
        return a+b+c;
    }
}

2.29

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args)
    {
        //冒泡排序
        int[] arr={9,8,3,5,2};
        bubblesort(arr);
        printfarr(arr);
    }
    public static void bubblesort(int[] arr)
    {
        for(int i=0;i<arr.length-1;i++)
        {
            for(int j=0;j<arr.length-i-1;j++)
            {
                if(arr[j]>arr[j+1])
                {
                    int tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                }
            }
        }
    }
    public static void printfarr(int[] arr)
    {
        for(int i=0;i<arr.length;i++)
            System.out.print(arr[i]+" ");
    }
}

2.30

import java.util.Scanner;
import java.util.Random;
public class Main {
    public static void main(String[] args)
    {
        int[][] arr=new int[3][];
        arr[0]=new int[]{11,12};
        arr[1]=new int[]{21,22,23};
        arr[2]=new int[]{31,32,33,34};
        int sum=0;
        for(int i=0;i<arr.length;i++)
        {
            int flag=0;
            for(int j=0;j<arr[i].length;j++)
            {
                flag=flag+arr[i][j];
            }
            sum=sum+flag;
            System.out.println("第"+(i+1)+"小组的总和为"+flag);
        }
        System.out.println("总销售额为"+sum);
    }
}
posted @ 2022-09-23 14:35  Vijurria  阅读(18)  评论(0编辑  收藏  举报