Here are three concise Java code examples for different purposes:
1. Hello World Program
Copy the codepublic class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. Simple Calculator (Addition)
Copy the codeimport java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
int sum = num1 + num2;
System.out.println("The sum is: " + sum);
}
}
3. Check if a Number is Even or Odd
Copy the codeimport java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
Feel free to adapt these examples to your needs! Let me know if you'd like help with a
specific type of Java program.