Acacia made plum blossoms overnight, and suddenly came to the window and suspected to be a monarch.
Overview
Java
It is IT
one of the most popular programming languages widely used in the industry. It is simple, robust, and helps us reuse code. In this article, let's look at some Java
applications that understand the basics.
Getting started with Java programs
Computer program
Write a Java
program to perform basic calculator operations.
When you consider using a calculator, you will think of operations such as addition, subtraction, multiplication, and division. Let us use the following procedure to achieve basic calculator operations.
package com.niocoder;
import java.util.Scanner;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _01Calculator {
public static void main(String[] args) {
Scanner param = new Scanner(System.in);
System.out.print("请输入第一个数字:");
double first = param.nextDouble();
System.out.print("请输入第二个数字:");
double second = param.nextDouble();
System.out.print("请输入运算符 (+, -, *, /): ");
char operator = param.next().charAt(0);
double result;
//switch case for each of the operations
switch (operator) {
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
default:
// operator doesn't match any case constant (+, -, *, /)default:
System.out.println("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}
复制代码
When the above program is executed, the output is as follows:
请输入第一个数字:10
请输入第二个数字:10
请输入运算符 (+, -, *, /): +
10.0 + 10.0 = 20.0
复制代码
Use recursive factorial procedures
Write a Java
program to calculate the factorial of a number.
The factorial of a number is the product of all positive numbers less than or equal to the number. n
The factorial of is n!
represented by . Now, let us write a program and use recursion to find the factorial of a number.
package com.niocoder;
import java.util.Scanner;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _02Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字:");
//Stored the entered value in variable
int num = scanner.nextInt();
//Called the user defined function fact
int factorial = fact(num);
System.out.println("输入数字的阶乘是: " + factorial);
}
static int fact(int number) {
if (number == 1) {
return 1;
}
return number * fact(number - 1);
}
}
复制代码
When executing the above program, you will get a factorial of a number as follows:
请输入一个数字:12
输入数字的阶乘是: 479001600
复制代码
Fibonacci sequence procedure
Write a Java
program to calculate the Fibonacci sequence up to n
a number.
It is a series, and the next item is the sum of the first two items. For example: 0 1 1 2 3 5 8 13 ... Let us write a Java
program to calculate the Fibonacci sequence.
package com.niocoder;
import java.util.Scanner;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _03Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个数字:");
//Stored the entered value in variable
int num = scanner.nextInt();
int first = 0, second = 1;
System.out.print(num + ":");
while (first < num) {
System.out.print(first + "+");
int sum = first + second;
first = second;
second = sum;
}
}
}
复制代码
After executing the above code, the output is as follows:
请输入一个数字:100
100:0+1+1+2+3+5+8+13+21+34+55+89+
复制代码
Palindrome program
Write a Java
program to find out if a given string is a palindrome.
A palindrome is a number, string, or sequence, even if you reverse the order, they are the same. For example, RACECAR
if you spell backwards the RACECAR
same as.
package com.niocoder;
import java.util.Scanner;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _04Palindrome {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入一个字符串: ");
String str = sc.nextLine();
checkPalindrome(str);
}
private static void checkPalindrome(String str) {
boolean flag = true;
int length = str.length();
for (int i = 0; i <= length / 2; i++) {
if (str.charAt(i) != str.charAt(length - i - 1)) {
flag = false;
break;
}
}
System.out.println(str + " 是否回文 = " + flag);
}
}
复制代码
When running the code, it will check whether the given string is a palindrome, as shown below:
请输入一个字符串: abab
abab 是否回文 = false
请输入一个字符串: abba
abba 是否回文 = true
复制代码
Pattern program
Java
Print a diamond pattern with a written program.
Here, the for
loop is used to print the diamond pattern.
package com.niocoder;
import java.util.Scanner;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _05DiamondPattern {
public static void main(String[] args) {
int n, i, j, space = 1;
System.out.print("请输入行数: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= n - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
}
复制代码
Output
请输入行数: 5
*
***
*****
*******
*********
*******
*****
***
*
复制代码
String reversal program
Write a Java program to reverse the letters in a given string.
This Java
program will reverse the letters in the string entered by the user. For example, " Hello People
" will be called " olleH elpoeP
". Let's use Java
to achieve the same function.
package com.niocoder;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _06Stringreverse {
public static void main(String[] args) {
String str = "Welcome To niocoder";
String[] strArray = str.split(" ");
for (String temp : strArray) {
System.out.println(temp);
}
for (int i = 0; i < 3; i++) {
char[] s1 = strArray[i].toCharArray();
for (int j = s1.length - 1; j >= 0; j--) {
System.out.print(s1[j]);
}
System.out.print(" ");
}
}
}
复制代码
The output of the above program is as follows:
Welcome
To
niocoder
emocleW oT redocoin
复制代码
Mirror program
Write a Java program to check whether the given array is a mirrored array.
package com.niocoder;
/**
* Created by zhenglongfei on 2020/4/21
*
* @VERSION 1.0
*/
public class _07MirrorInverse {
public static void main(String[] args) {
int arr[] = {3,4,2,0,1};
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
static boolean isMirrorInverse(int arr[]) {
for (int i = 0; i < arr.length; i++) {
if (arr[arr[i]] != i)
return false;
}
return true;
}
}
复制代码
Output
Yes
复制代码