最近又重新开始学习Java了,做了一些简单的题目,记录一下学习进程。废话不多说,直接上代码。
1. 输入一个正整数,判断是不是素数
public class IsPrime {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个正整数:");
Scanner scanner = new Scanner(System.in);// 创建Scanner对象
int prime = scanner.nextInt();// 获取从键盘输入的数据
int i = 2;
for (; i < prime; i++) {
if (prime % i == 0) {
System.out.println(prime + " is not a prime.");
break;
}
}
if (prime == i) {
System.out.println(prime + "is a prime.");
}
}
}
2. 找出1-1000以内的所有素数
public class SearchPrime {
public static void main(String args[]) {
// TODO Auto-generated method stub
int i, j;
for (i = 1; i <= 1000; i++) {
for (j = 2; j < i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
System.out.println(j + ",");
}
}
}
}
3. 输入两个数,计算最大公约数
public class MaxDivisor {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("Input the first number:");
int number1 = scanner.nextInt();
System.out.println("Input the second number:");
int number2 = scanner.nextInt();
int temp = number1 % number2;
while(temp != 0) {//欧几里得算法
number1 = number2;
number2 = temp;
temp = number1 % number2;
}
System.out.println(number2 + " is the max common divisor.");
}
}
4.计算1-100之间的偶数和
public class calculate {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int i = 0; i <= 100; i++) {
if(i % 2 == 0) {
sum += i;
}
}
System.out.println("The summary between 1 and 100 is:" + sum);
}
}
5.输入一个正整数,将其转换为二进制显示出来
public class Conversion {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Input a number:");
Scanner scanner = new Scanner(System.in);//创建Scanner对象
int number = scanner.nextInt();
String string = Integer.toBinaryString(number);//调用API函数
System.out.println(string);
}
}
6.找出1000以内的所有水仙花数
public class DaffodilNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b,c;//分别放置数值的个,十,百位上的数字
for(int i = 1; i <= 1000 ; i++) {
a = i / 100;//百位
b = i % 100 / 10;//十位
c = i % 10;//个位
if(a*a*a + b*b*b + c*c*c == i) {
System.out.println(i);
}
}
}
}
7.输入三个数,按从小到大排序
public class Sort {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Input three number:");
Scanner scanner = new Scanner(System.in);//创建Scanner对象
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if(a > b && a > c) {
if(b > c) {
System.out.println(c + "<" + b + "<" + a);
} else {
System.out.println(b + "<" + c + "<" + a);
}
}
if (b > a && b > c) {
if(a > c) {
System.out.println(c + "<" + a + "<" + b);
} else {
System.out.println(a + "<" + c + "<" + b);
}
}
if (c > a && c > b) {
if(a > b) {
System.out.println(b + "<" + a + "<" + c);
} else {
System.out.println(a + "<" + b + "<" + c);
}
}
}
}
8.打印等腰三角形
public class Triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Input the number of triangle layer:");// 输入要打印的等腰三角形的层数
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
for (int j = 0; j < num - i - 1; j++) {
System.out.print(" ");// 打印空格
}
for (int k = 0; k < (i * 2 + 1); k++) {
System.out.print("*");// 打印*号
}
System.out.println("");
}
}
}
9.输入年月日判断是当年的第几天
public class JudgeDate {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("Input the number of year:");//输入年份
int year = scanner.nextInt();
System.out.println("Input the number of month:");//输入月份
int month = scanner.nextInt();
System.out.println("Input the number of day:");//输入日期
int day = scanner.nextInt();
int num = 0;
int[] leapYear= {0,31,60,91,121,152,182,213,244,274,305,335,366};//闰年
int[] commonYear= {0,31,59,90,120,151,181,212,243,273,304,334,365};//平年
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {//年份为闰年
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if (day >= 1 && day <= 31) {
num = leapYear[month - 1] + day;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day >= 1 && day <= 31) {
num = leapYear[month - 1] + day;
}
}else {
if (day >= 1 && day <= 29) {
num = leapYear[month - 1] + day;
}
}
} else {
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
if (day >= 1 && day <= 31) {
num = commonYear[month - 1] + day;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day >= 1 && day <= 31) {
num = commonYear[month - 1] + day;
}
}else {
if (day >= 1 && day <= 29) {
num = commonYear[month - 1] + day;
}
}
}
System.out.println(year + "年" + month + "月" + day + "日是该年第" + num + "天。");
}
}
都是一些简单的题目,但是涉及了Java的基础语法,继续努力吧!