欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java每日一练(5)

程序员文章站 2022-07-12 08:56:26
...

1.字符逆序输出

需求说明: 将 一组乱序的字符进行排序、进行升序、逆序输出  

package com.etc.five.day;

import java.util.Arrays;

public class Sort {

	public static void main(String[] args) {
		String[] Letter={"Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M"};
		System.out.println("原字符序列");
		for(int i=0;i<Letter.length;i++){
			System.out.print(Letter[i]+" ");
		}
		Arrays.sort(Letter);
		System.out.println("\n升序排序后:");
		for(int i=0;i<Letter.length;i++){
			System.out.print(Letter[i]+" ");
		}
		System.out.println("\n降序排序后:");
		for(int i=Letter.length-1;i>=0;i--){
			System.out.print(Letter[i]+" ");
		}
	}

}

2.更新会员积分

需求说明: 将原有积分进行备份,然后赠送每位会员500积分,编写程序输出积分情况

package com.etc.five.day;

import com.etc.test.util.JudgeNumber;

public class Points {

	public static void main(String[] args) {
		int[] number = new int[999999];
		int[] number2 = new int[999999];
		int number3 = 0;
		JudgeNumber jn = new JudgeNumber();
		System.out.print("请输入会员数目:");
		number3 = jn.judge();
		for (int i = 1; i <= number3; i++) {
			System.out.print("第" + i + "会员积分:");
			number[i] = jn.judge();
		}
		System.out.println("\n序列" + "\t\t" + "历史积分" + "\t\t" + "新年积分");
		for (int i = 1; i <= number3; i++) {
			System.out.println(i + "\t\t" + number[i] + "\t\t" + (number[i] + 500));
		}
	}
}

3.打印菱形

练要点: 复杂图形分步打印的思想;复杂的二重循环

需求说明: 如果用户输入的行数为奇数,则打印出菱形;否则提示用户输入奇数

实现思路: 1、while循环判断是否奇数 2、分步打印

难点指导: 打印菱形下半部分

package com.etc.five.day;

import com.etc.test.util.JudgeNumber;

public class Diamond {

	public static void main(String[] args) {
		int number = 0;
		int number2 = 0;
		JudgeNumber jn = new JudgeNumber();
		System.out.println("请输入菱形行数");
		number = jn.odd();
		for (int i = 0; i <= number / 2; i++) {
			for (int k = number / 2 - 1; k >= i; k--) {
				System.out.print(" ");
			}
			for (int j = 1; j <= 2 * i - 1; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
		for (int i = 0; i <= number / 2; i++) {
			for (int j = 0; j <= i; j++) {
				System.out.print(" ");
			}
			for (int k = number/2-2; k >= (i*2-number/2)+2; k--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

4.实现九九乘法表

package com.etc.five.day;

public class Table {

	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(j + "*" + i + "=" + (i * j) + "\t");
			}
			System.out.println();
		}
	}
}

Java每日一练(5)

package com.etc.test.util;

import java.util.Scanner;

public class JudgeNumber {
	public int judge() {
		int number = 0;
		while (true) {
			Scanner input = new Scanner(System.in);
			if (input.hasNextInt()) {
				number = input.nextInt();
				break;
			} else {
				System.out.print("请输入数字:");
			}
		}
		return number;
	}

	public int odd() {
		int number = 0;
		while (true) {
			Scanner input = new Scanner(System.in);
			if (input.hasNextInt()) {
				number = input.nextInt();
			} else {
				System.out.print("请输入数字:");
			}
			if (number % 2 == 1) {
				break;
			} else {
				System.out.print("请输入奇数:");
			}
		}
		return number;
	}
	public String name(){
		String name="";
		while(true){
			Scanner input =new Scanner(System.in);
			if(input.hasNext()){
				name =input.next();
				break;
			}
		}
		return name;
	}
}