自己的Java学习之路(三)

本文提供四个Java编程实战案例,包括字符排序与输出、积分管理系统、图形打印及九九乘法表的实现。通过这些练习,读者可以加深对Java语言的理解,并提高实际编程能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目一:

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

  

基本代码:

/**
 * @筮堆
 */

package com.etc.july30.ui;
import java.util.Arrays;
public class Homework1 {
    public static void main(String[] args) {
        System.out.print("原始字符序列:");
        int i = 0;
        String[] n = new String[]{"a","b","r","t","u","e","i"};    
            for (int j = 0; j < n.length; j++) {
            System.out.print(n[j]+" ");
        }
            Arrays.sort(n);
            System.out.println();
            System.out.print("升序字符序列:");
            for ( i = 0; i < n.length ; i++) {    
            System.out.print(n[i]+" ");    
            }            
            System.out.println();
            System.out.print("降序字符序列 :");
            for (int k =n.length ; k > 0; k--) {
            System.out.print(n[k-1]+" ");    
            }
    }
}
运行截图:

题目二: 

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

 

基本代码:

/**
 * @筮堆
 */
package com.etc.july30.ui;
import java.util.Scanner;
public class Homework2 {
    public static void main(String[] args) {
        int[] a = new int[6];
        System.out.println("请输入5位会员的积分");
        int i = 0;
        Scanner input = new Scanner(System.in);
        for ( i = 1; i <= 5; i++) {
            System.out.print("请输入第" + i + "位会员积分:");
             a[i] = input.nextInt();
        }
        System.out.println("序号\t\t历史积分\t\t新年积分");
        for ( i = 1; i <= 5; i++) {
            System.out.println(i+"\t\t" + a[i] +"\t\t" + (a[i]+500));    
        }
    }
}
运行截图:

 题目三:

训练要点: 复杂图形分步打印的思想 复杂的二重循环 需求说明: 如果用户输入的行数为奇数,则打印出菱形;否则提示用户输入奇数 实现思路: 1、while循环判断是否奇数 2、分步打印 难点指导: 打印菱形下半部分。

基本代码:

Homework3 类:

/**
 * @筮堆
 */
package com.etc.july30.ui;
import java.util.Scanner;
import com.etc.july30.util.Number_class;
public class Homework3 {
    public static void main(String[] args) {
        boolean f = true;
        Scanner input = new Scanner(System.in);
        int a = 0;
        System.out.print("输入菱形行数:");
        // 调用工具类中的数字判断的类
        Number_class zheng = new Number_class();
        while (f) {
            a = zheng.Zhengshu();
            int ji = a % 2;
            if ((ji == 0)) {
                System.err.println("请输入奇数:");
            } else {
                f = false;
            }
        }
        for (int i = 1; i < a / 2 + 2; i++) {
            for (int j = a / 2; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i * 2 - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        for (int i = 0; i < a / 2 + 1; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" ");
            }
            for (int j = a / 2; j >= i * 2 - 1; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Number_class 类:

/**
 * @筮堆
 */
package com.etc.july30.util;
import java.util.Scanner;
public class Number_class {
    public int Zhengshu(){
        System.out.println("(输入一个整数)");
        int a = 0 ;
        while(true){
            Scanner input = new Scanner(System.in) ;         
            if(input.hasNextInt()){
                a = input.nextInt() ;
                break ;
            }else{
                System.err.println("请输入数字!!!");
            }
        }
        return a ;
    }

运行截图:

题目四:

实现九九乘法表。

基本代码:

/**
 * @筮堆
 */
package com.etc.july30.ui;
public class Homework4 {
    public static void main(String[] args) {
        
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i +"*"+ j +"="+ i*j +"\t");    
            }System.out.println();    
        }
    }
}

运行截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值