21xrx.com
2025-03-29 05:27:08 Saturday
文章检索 我的文章 写文章
关键词:Java代码、简单、好玩
2023-06-15 15:32:27 深夜i     6     0

玩转Java:10个简单好玩的代码示例

Java作为一门成熟的编程语言,其应用早已渗透到我们生活的各个领域中。而作为Java开发者,怎么才能把这个语言玩得更好呢?下面就为大家介绍十段简单好玩的Java代码示例,让你的Java语言之旅更加有趣。

1. 输出Hello World!

public class HelloWorld {
 public static void main(String[] args) {
  System.out.println("Hello World!");
 }
}

2. 实现加、减、乘、除的简单计算器

import java.util.Scanner;
public class Calculator {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入两个数:");
  double num1 = sc.nextDouble();
  double num2 = sc.nextDouble();
  System.out.println("请选择要进行的运算(+、-、*、/):");
  String operation = sc.next();
  double result = 0;
  switch (operation) {
   case "+":
    result = num1 + num2;
    break;
   case "-":
    result = num1 - num2;
    break;
   case "*":
    result = num1 * num2;
    break;
   case "/":
    result = num1 / num2;
    break;
   default:
    System.out.println("无效的运算符!");
    break;
  }
  System.out.println("计算结果为:" + result);
 }
}

3. 实现猜数字小游戏

import java.util.Scanner;
public class GuessNumber {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  int target = (int) (Math.random() * 100) + 1;
  int guess = 0;
  while (guess != target) {
   System.out.println("请输入你猜的数字(1~100):");
   guess = sc.nextInt();
   if (guess < target) {
    System.out.println("太小了,再试一次!");
   } else if (guess > target) {
    System.out.println("太大了,再试一次!");
   }
  }
  System.out.println("恭喜你猜对了!");
 }
}

4. 实现字符串反转

import java.util.Scanner;
public class StringReverse {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入一个字符串:");
  String str = sc.nextLine();
  StringBuilder sb = new StringBuilder(str);
  String reversedStr = sb.reverse().toString();
  System.out.println("反转后的字符串为:" + reversedStr);
 }
}

5. 实现斐波那契数列

import java.util.Scanner;
public class Fibonacci {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入要生成的斐波那契数列个数:");
  int count = sc.nextInt();
  int[] fibArr = new int[count];
  fibArr[0] = 0;
  fibArr[1] = 1;
  for (int i = 2; i < count; i++) {
   fibArr[i] = fibArr[i - 1] + fibArr[i - 2];
  }
  System.out.println("生成的斐波那契数列为:");
  for (int fib : fibArr) {
   System.out.print(fib + " ");
  }
 }
}

6. 实现冒泡排序

import java.util.Arrays;
public class BubbleSort {
 public static void main(String[] args) {
  int[] arr = 2 ;
  System.out.println("排序前的数组:" + Arrays.toString(arr));
  for (int i = arr.length - 1; i > 0; i--) {
   for (int j = 0; j < i; j++) {
    if (arr[j] > arr[j + 1]) {
     int temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
    }
   }
  }
  System.out.println("排序后的数组:" + Arrays.toString(arr));
 }
}

7. 实现求阶乘

import java.util.Scanner;
public class Factorial {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入一个自然数:");
  int num = sc.nextInt();
  System.out.println(num + "的阶乘为:" + factorial(num));
 }
 private static int factorial(int num) {
  if (num <= 1)
   return 1;
   else {
   return num * factorial(num - 1);
  }
 }
}

8. 实现判断回文数

import java.util.Scanner;
public class PalindromeNumber {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入一个数字:");
  int num = sc.nextInt();
  boolean isPalindrome = true;
  String str = String.valueOf(num);
  int left = 0;
  int right = str.length() - 1;
  while (left < right) {
   if (str.charAt(left) != str.charAt(right))
    isPalindrome = false;
    break;
   
   left++;
   right--;
  }
  if (isPalindrome) {
   System.out.println(num + "是回文数");
  } else {
   System.out.println(num + "不是回文数");
  }
 }
}

9. 实现模拟五子棋

import java.util.Scanner;
public class Gobang {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String[][] chessboard = new String[15][15];
  System.out.println("欢迎来到五子棋游戏!");
  boolean isBlack = true;
  boolean isGameOver = false;
  while (!isGameOver) {
   int row, col;
   while (true) {
    System.out.println("请输入下棋的坐标(如:3 5):");
    row = sc.nextInt();
    col = sc.nextInt();
    if (chessboard[row][col] == null)
     break;
    
    System.out.println("此处已经有棋子,请重新输入!");
   }
   if (isBlack) {
    chessboard[row][col] = "●";
    isBlack = false;
   } else {
    chessboard[row][col] = "○";
    isBlack = true;
   }
   printChessboard(chessboard);
   if (checkWin(chessboard, row, col)) {
    if (isBlack) {
     System.out.println("黑方获胜!");
    } else {
     System.out.println("白方获胜!");
    }
    isGameOver = true;
   }
  }
 }
 private static void printChessboard(String[][] chessboard) {
  System.out.println(" 0 1 2 3 4 5 6 7 8 9 a b c d e");
  for (int i = 0; i < 15; i++) {
   System.out.print(Integer.toHexString(i) + " ");
   for (int j = 0; j < 15; j++) {
    if (chessboard[i][j] == null) {
     System.out.print("+ ");
    } else {
     System.out.print(chessboard[i][j] + " ");
    }
   }
   System.out.println();
  }
 }
 private static boolean checkWin(String[][] chessboard, int row, int col) {
  String color = chessboard[row][col];
  int count = 0;
  for (int i = 0; i < 5; i++) {
   if (col - i >= 0 && col + 4 - i < 15 && chessboard[row][col - i] != null && chessboard[row][col - i].equals(color)
     && chessboard[row][col + 1 - i] != null && chessboard[row][col + 1 - i].equals(color)
     && chessboard[row][col + 2 - i] != null && chessboard[row][col + 2 - i].equals(color)
     && chessboard[row][col + 3 - i] != null && chessboard[row][col + 3 - i].equals(color)
     && chessboard[row][col + 4 - i] != null && chessboard[row][col + 4 - i].equals(color)) {
    count++;
   }
  }
  if (count > 0)
   return true;
  
  count = 0;
  for (int i = 0; i < 5; i++) {
   if (row - i >= 0 && row + 4 - i < 15 && chessboard[row - i][col] != null && chessboard[row - i][col].equals(color)
     && chessboard[row + 1 - i][col] != null && chessboard[row + 1 - i][col].equals(color)
     && chessboard[row + 2 - i][col] != null && chessboard[row + 2 - i][col].equals(color)
     && chessboard[row + 3 - i][col] != null && chessboard[row + 3 - i][col].equals(color)
     && chessboard[row + 4 - i][col] != null && chessboard[row + 4 - i][col].equals(color)) {
    count++;
   }
  }
  if (count > 0)
   return true;
  
  count = 0;
  for (int i = 0; i < 5; i++) {
   if (row - i >= 0 && row + 4 - i < 15 && col - i >= 0 && col + 4 - i < 15 && chessboard[row - i][col - i] != null && chessboard[row - i][col - i].equals(color)
     && chessboard[row + 1 - i][col + 1 - i] != null && chessboard[row + 1 - i][col + 1 - i].equals(color)
     && chessboard[row + 2 - i][col + 2 - i] != null && chessboard[row + 2 - i][col + 2 - i].equals(color)
     && chessboard[row + 3 - i][col + 3 - i] != null && chessboard[row + 3 - i][col + 3 - i].equals(color)
     && chessboard[row + 4 - i][col + 4 - i] != null && chessboard[row + 4 - i][col + 4 - i].equals(color)) {
    count++;
   }
  }
  if (count > 0)
   return true;
  
  count = 0;
  for (int i = 0; i < 5; i++) {
   if (row - i >= 0 && row + 4 - i < 15 && col + i >= 0 && col - 4 + i < 15 && chessboard[row - i][col + i] != null && chessboard[row - i][col + i].equals(color)
     && chessboard[row + 1 - i][col - 1 + i] != null && chessboard[row + 1 - i][col - 1 + i].equals(color)
     && chessboard[row + 2 - i][col - 2 + i] != null && chessboard[row + 2 - i][col - 2 + i].equals(color)
     && chessboard[row + 3 - i][col - 3 + i] != null && chessboard[row + 3 - i][col - 3 + i].equals(color)
     && chessboard[row + 4 - i][col - 4 + i] != null && chessboard[row + 4 - i][col - 4 + i].equals(color)) {
    count++;
   }
  }
  if (count > 0)
   return true;
  
  return false;
 }
}

10. 实现抽奖程序

import java.util.Scanner;
public class Lottery {
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入抽奖人数:");
  int count = sc.nextInt();
  System.out.println("请输入奖品数:");
  int prizeCount = sc.nextInt();
  boolean[] selected = new boolean[count];
  int[] prizes = new int[prizeCount];
  for (int i = 0; i < prizeCount; i++) {
   int prize = (int) (Math.random() * count);
   while (selected[prize]) {
    prize = (prize + 1) % count;
   }
   selected[prize] = true;
   prizes[i] = prize;
  }
  System.out.println("中奖的抽奖号码为:");
  for (int prize : prizes) {
   System.out.print(prize + " ");
  }
 }
}

以上就是十个简单好玩的Java代码示例,它们涉及到了不同的领域,从传统的输出“Hello World!”到实现五子棋游戏,从简单的字符串反转到复杂的冒泡排序和递归求阶乘,每个代码示例都能帮助你更好地理解和掌握Java语言,同时增加你的编程乐趣。

  
  

评论区