Java程序设计例题代码

本篇章是记录学习JAVA程序设计时的课堂以及课后作业代码
不定时持续更新中

HomeWork1

求某范围质数(只能被1和自身整除的数)
a.开始范围和结束范围自行录入
b.每行输出10个
c.输出质数的数量以及质数和

/**
 * @author Se7en
 *
 */
public class HomeWork1 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入开始范围:");
        int start = scanner.nextInt();
        System.out.print("请输入结束范围:");
        int end = scanner.nextInt();
        int n = 0;
        int count = 0;
        int sum = 0;
        for(int i = start; i <= end; i++) {
            boolean a = true;
            if(i < 2) {
                a = false;
            }
            for(int j = 2; j < i; j++) {
                if(i%j == 0) {
                    a = false;
                    break;
                }
            }
            if(a) {
                System.out.print(i+" ");
                n++;
                count++;
                sum += i;
            }
            if(n == 10) {
                System.out.println();
                n=0;
            }
        }
        System.out.println("\n质数数量:"+count);
        System.out.println("质数和:"+sum);
        
    }

}

HomeWork2

输出N*N乘法表
输入:3

1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
/**
 * @author Se7en
 *
 */
public class HomeWork2 {

    /**
     * @param args
     */
    public static void main(String[] args) {    //输出N*N乘法表
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入一个数字:");
        int n = scanner.nextInt();
        int mul = 0;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j<=i; j++) {
                mul = i * j;
                System.out.print(i+"*"+j+"="+mul+"    ");
            }
            System.out.println();
        }

    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值