知识屋:更实用的电脑技术知识网站
所在位置:首页 > 教育

适合初学者入门Java程序

发表时间:2022-03-26来源:网络

相思一夜梅花发,忽到窗前疑是君。

概述

Java是在IT行业广泛使用的最流行的编程语言之一。它简单,健壮,可帮助我们重用代码。在本文中,让我们看一些了解Java基础的应用程序。

入门的Java程序

计算机程序

编写一个Java程序来执行基本的计算器操作。

当你考虑使用计算器时,就会想到加,减,乘,除等运算。让我们借助以下程序来实现基本的计算器操作。

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); } }

执行上述程序时,输出如下所示:

请输入第一个数字:10 请输入第二个数字:10 请输入运算符 (+, -, *, /): + 10.0 + 10.0 = 20.0

使用递归的阶乘程序

编写一个Java程序来计算一个数字的阶乘。

数字的阶乘是所有小于或等于该数字的正数的乘积。n的阶乘由n!表示。现在,让我们编写一个程序,并使用递归查找数字的阶乘。

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); } }

执行上面的程序时,您将获得一个数字的阶乘,如下所示:

请输入一个数字:12 输入数字的阶乘是: 479001600

斐波纳契数列的程序

编写一个Java程序来计算斐波那契数列直到n个数字。

它是一个级数,其中下一项是前两项之和。例如:0 1 1 2 3 5 8 13……让我们编写一个Java程序来计算斐波那契数列。

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 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 flag = false; break; } } System.out.println(str + " 是否回文 = " + flag); } }

运行代码时,它将检查给定的字符串是否是回文,如下所示:

请输入一个字符串: abab abab 是否回文 = false 请输入一个字符串: abba abba 是否回文 = true

图案程序

用Java编写程序打印菱形图案。

在这里,使用for循环来打印菱形图案。

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 System.out.print(" "); } space--; for (i = 1; i for (i = 1; i System.out.print("*"); } System.out.println(""); } } }

输出

请输入行数: 5 * *** ***** ******* ********* ******* ***** *** *

字符串反转程序

编写一个Java程序来反转给定字符串中的字母。

这个Java程序会反转用户输入的字符串中存在的字母。例如,“ Hello People”将被称为“ olleH elpoeP”。让我们使用Java来实现相同的功能。

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 System.out.print(s1[j]); } System.out.print(" "); } } }

上面程序的输出如下所示:

Welcome To niocoder emocleW oT redocoin

镜像程序

编写一个Java程序来检查给定的数组是否为镜像数组。

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
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜