Classic examples of Java entry (1)
1. According to age, to print out the current age of people is juvenile (below 18), youth (19-28), middle-aged (29-55), old (above 56).
Upload code
import java.util.Scanner;
public class TextDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要判断的年龄");
int year = scanner.nextInt();
if(year < 18){
System.out.println("你目前是少年");
}
if(year > 19 && year <28){
System.out.println("你目前是青年");
}
if(year > 29 && year <55) {
System.out.println("你目前是中年");
}
if(year > 56){
System.out.println("你目前是老年");
}
}
}
The results are as follows
2. Determine whether a number is a prime number.
Upload code
import java.util.Scanner;
public class TextDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入需要判断的数字");
int a = scanner.nextInt();
if(a == 1 || a == 2 ){
System.out.println("还数字是素数");
}else {
int i = 2 ;
while(i<a){
if (a % i == 0 ){
System.out.println("该数字不是素数");
break;
}
i++;
}
if( a == i ){
System.out.println("该数字是素数");
}
}
}
}
The results are as follows
3. Print all prime numbers between 1 and 100.
Upload code
public class TextDemo {
public static void main(String[] args) {
System.out.println("1~100之间素数为");
for(int a = 2; a <=100 ; a++ ){
int b = 2;
while ( a % b != 0){
b++;
}
if ( a == b){
System.out.printf("%d\t",a);
}
}
}
}
The results are as follows
4. Output all leap years from 1980 to 2020
public class TextDemo {
public static void main(String[] args) {
System.out.println("闰年有");
int year = 1980;
for (year = 1980; year <= 2020; year++){
if (year % 100 == 0){
if (year % 400 == 0);{
System.out.printf("%d\t", year );
}
}else{
if (year % 4 == 0 ){
System.out.printf("%d\t", year );
}
}
}
}
}
The results are as follows
5. Output the multiplication formula table and
directly upload the code
public class TextDemo {
public static void main(String[] args) {
int num = 1;
for (int i = 1; i < 10;i++){
for (int j = 1; j <= i ;j++){
num = i * j ;
System.out.print(j + "*" + i + "=" + num + " ");
//此处的‘+’,并非加法运算,而是拼接。
}
System.out.println();
}
}
}
The results are as follows
6. Find the greatest common divisor of two positive integers.
This is the most classic question and must be mastered.
import java.util.Scanner;
public class TextDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入两个正整数");
int a = scanner.nextInt();
int b = scanner.nextInt();
for (int i =Math.min( a , b ); i >0; i--){
if (a % i == 0 && b % i == 0){
System.out.println("这两个正整数的最大公约数为" + i);
break;
}
}
}
The results are as follows
7. Calculate the value of 1/1-1/2+1/3-1/4+1/5... + 1/99-1/100.
This question is very simple as long as the thinking is clear. Find the rule, odd-numbered items are positive, and even-numbered items are negative. Directly on the code below.
public class TextDemo {
public static void main(String[] args) {
double a = 0.0;
double b = 0.0;
double sum = 0.0;
for (double i = 1;i <= 100;i += 2){
a += 1 / i;
}
for (double i = 2;i <= 100;i += 2){
b += 1 / i;
}
sum = a - b ;
System.out.println(sum);
}
}
The running results are as follows.
This article is mainly about some simple Java introductory sample questions. The next article is a little more complicated, but it is still java introductory sample questions. Ladies and gentlemen, see you next time.