Java作业2

本文展示了四个Java编程题目,涉及Scanner类的使用、字符串到整数数组的转换、数组操作(计数、排序与合并)、动态数组初始化以及条件判断。每个代码片段展示了基础的控制结构和数据处理技巧。
摘要由CSDN通过智能技术生成

题目一

 代码

import java.util.*;

public class Main {
	public static void main(String[] args){
		Scanner input=new Scanner(System.in);
		String str=input.nextLine();//nextLine()方法遇到回车结束输入,next()遇到空格
		int[]a=new int[10];//用来计数
		a[0]=-1;
		for(int i=0;i<str.length();i++){
			if(str.charAt( i )=='0')
				a[0]++;
			else if(str.charAt( i )=='1')//charAt(i)获取字符方法
				a[1]++;
			else if(str.charAt( i )=='2')
				a[2]++;
			else if(str.charAt( i )=='3')
				a[3]++;
			else if(str.charAt( i )=='4')
				a[4]++;
			else if(str.charAt( i )=='5')
				a[5]++;
			else if(str.charAt( i )=='6')
				a[6]++;
			else if(str.charAt( i )=='7')
				a[7]++;
			else if(str.charAt( i )=='8')
				a[8]++;
			else if(str.charAt( i )=='9')
				a[9]++;
			else
				;			
		}
		for(int i=0;i<10;i++){
			System.out.printf("%-2d",i);
		}
		System.out.println();
		for(int i=0;i<10;i++){
			System.out.printf("%-2d",a[i]);
		}
	}
}

输入输出

1 23 53 87 90 12 15 89 0
0 1 2 3 4 5 6 7 8 9
1 3 2 2 0 2 0 1 2 2

题目二

代码 

import java.util.*;

public class Main {
	public static void main(String[] args){
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		int locks[]=new int[n];//动态初始化
		 // 进行n轮循环
		for (int i = 1; i <= n; i++) {
			// 对每隔i把锁进行操作
			for (int j = i - 1; j < n; j += i) {
				// 如果锁的状态为关闭,则将其状态改为打开
				if (locks[j] == 0) {
					locks[j] = 1;
				}
				// 如果锁的状态为打开,则将其状态改为关闭
				else {
					locks[j] = 0;
				}
			}
		}
		int cnt=0;//记打开锁的个数
		for (int i = 0; i < n; i++) {
			if (locks[i] == 1) {
				cnt++;	
			}
		}
		for (int i = 0; i < n; i++) {
			if (locks[i] == 1) {
				System.out.print(i+1);
				cnt--;
				if(cnt>0)
					System.out.printf(" ");
			}
		}
	}
}

 输入输出

8
1 4

题目三

代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] arr = new int[n];	
        for (int i = 0; i < n; i++) {
            arr[i] = input.nextInt();
        }
        if (isAscendingOrder(arr)) {
            System.out.println("The list is already sorted.");
        } else {
			System.out.println("The list is not sorted");
            Arrays.sort(arr);//排序方法参考的课件
            for(int i=0;i<n;i++){
				System.out.print(arr[i]);
				if(i<n-1)
					System.out.printf(" ");
			}
            System.out.println();
        }
    }
    public static boolean isAscendingOrder(int[] arr) {//函数声明方法参考网络
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < arr[i - 1]) {
                return false;
            }
        }
        return true;
    }
}

输入输出

8 10 1 5 16 61 9 11 1
The list is not sorted
1 1 5 9 10 11 16 61

题目四

代码

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
		//数组1
        int n = input.nextInt();
        int[] arrN = new int[n];	
        for (int i = 0; i < n; i++) {
            arrN[i] = input.nextInt();
        }
		//数组2
		int m = input.nextInt();
        int[] arrM = new int[m];	
        for (int i = 0; i < m; i++) {
            arrM[i] = input.nextInt();
        }
		//合并到数组3
		int x = n + m;
		int[] arr = new int[x];
		//合并
		System.arraycopy(arrN, 0, arr, 0, n);//方法参考课件+CSDN
		System.arraycopy(arrM, 0, arr, n, m);
		//排序
		Arrays.sort(arr);
		//打印
		System.out.print("The merged list is ");
		for(int i = 0; i < x; i++){
			System.out.print(arr[i]);
			if(i<x-1)
				System.out.printf(" ");
		}
	}	
}

输入输出

5 1 5 16 61 111 4 2 4 5 6
The merged list is 1 2 4 5 5 6 16 61 111

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值