本篇章是记录学习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();
}
}
}
转载:https://blog.csdn.net/a531747384/article/details/101774939
查看评论