21xrx.com
2024-06-03 07:05:49 Monday
登录
文章检索 我的文章 写文章
Java基础入门:10个简单实用的代码案例分享
2023-06-15 00:52:24 深夜i     --     --
Java 代码案例 基础入门

Java作为一种经典的面向对象编程语言,一直备受程序员们的喜爱。它不仅具有良好的可移植性和安全性,而且还可以应用于多种不同的场景下。不过,如果你是一位初学者,可能会在学习过程中遇到一些难题。本文将为大家分享10个简单实用的Java代码案例,帮助大家更好地掌握Java的基础知识。

1. 如何打印“Hello World!”?

这是Java程序员们必须要学会的第一个任务。下面是实现该任务的代码示例:


public class HelloWorld {

  public static void main(String[] args) {

    System.out.println("Hello World!");

  }

}

2. 如何计算两个数的和?

这个案例将演示如何使用Java实现简单的算术运算。下面是实现该任务的代码示例:


public class AddTwoNumbers {

  public static void main(String[] args) {

    int num1 = 5, num2 = 10, sum;

    sum = num1 + num2;

    System.out.println("Sum of " + num1 + " and " + num2 + " is " + sum);

  }

}

3. 如何查找一个数是否为素数?

素数是指只能被1和它本身整除的自然数。下面是实现该任务的代码示例:


public class CheckPrimeNumber {

  public static void main(String[] args) {

    int num = 29;

    boolean isPrime = true;

    for (int i = 2; i <= num / 2; i++) {

      if (num % i == 0)

        isPrime = false;

        break;

      

    }

    if (isPrime) {

      System.out.println(num + " is a prime number");

    } else {

      System.out.println(num + " is not a prime number");

    }

  }

}

4. 如何反转一个字符串?

下面是实现该任务的代码示例:


public class ReverseString {

  public static void main(String[] args) {

    String str = "Hello World!";

    String reversedStr = "";

    for (int i = str.length() - 1; i >= 0; i--) {

      reversedStr += str.charAt(i);

    }

    System.out.println("Original string: " + str);

    System.out.println("Reversed string: " + reversedStr);

  }

}

5. 如何查找一个数组中的最大元素?

下面是实现该任务的代码示例:


public class FindMaxElement {

  public static void main(String[] args) {

    int[] arr = 12;

    int max = arr[0];

    for (int i = 1; i < arr.length; i++) {

      if (arr[i] > max) {

        max = arr[i];

      }

    }

    System.out.println("Maximum element in the array: " + max);

  }

}

6. 如何创建包含指定数量元素的数组?

下面是实现该任务的代码示例:


public class CreateArray {

  public static void main(String[] args) {

    int length = 5;

    int[] arr = new int[length];

    for (int i = 0; i < length; i++) {

      arr[i] = i + 1;

    }

    System.out.println("Array elements: ");

    for (int i = 0; i < length; i++) {

      System.out.print(arr[i] + " ");

    }

  }

}

7. 如何自定义异常类?

下面是实现该任务的代码示例:


class CustomException extends Exception {

  CustomException(String message) {

    super(message);

  }

}

public class CustomExceptionDemo {

  static void validateAge(int age) throws CustomException {

    if (age < 18) {

      throw new CustomException("Age must be greater than 18");

    } else {

      System.out.println("Valid age: " + age);

    }

  }

  public static void main(String[] args) {

    try {

      validateAge(15);

    } catch (CustomException e) {

      System.out.println("Exception caught: " + e.getMessage());

    }

  }

}

8. 如何使用Java中的接口?

下面是实现该任务的代码示例:


interface Vehicle {

  void start();

  void stop();

}

class Car implements Vehicle {

  public void start() {

    System.out.println("Car Started");

  }

  public void stop() {

    System.out.println("Car Stopped");

  }

}

public class InterfaceDemo {

  public static void main(String[] args) {

    Vehicle car = new Car();

    car.start();

    car.stop();

  }

}

9. 如何使用Java中的多线程?

下面是实现该任务的代码示例:


class PrintNumbers extends Thread {

  public void run() {

    for (int i = 1; i <= 10; i++) {

      System.out.println(i);

      try {

        Thread.sleep(1000);

      } catch (InterruptedException e) {

        e.printStackTrace();

      }

    }

  }

}

public class MultithreadingDemo {

  public static void main(String[] args) {

    PrintNumbers t1 = new PrintNumbers();

    t1.start();

  }

}

10. 如何使用Java中的集合?

下面是实现该任务的代码示例:


import java.util.ArrayList;

public class ArrayListDemo {

  public static void main(String[] args) {

    ArrayList colors = new ArrayList<>();

    colors.add("Red");

    colors.add("Green");

    colors.add("Blue");

    colors.add("Yellow");

    System.out.println(colors);

  }

}

以上10个Java代码案例可以帮助你更好地入门Java,并为你提供学习的极佳起点。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复