百度
360搜索
搜狗搜索

java基础代码大全详细介绍

以下是一些Java基础代码示例:

一、Hello World程序

这是最基本的Java程序,用于在控制台输出“Hello, World!”。

public class HelloWorld {

public static void main(String[] args) {

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

}

}

 

二、变量与数据类型

1. 定义和使用变量

public class VariableExample {

public static void main(String[] args) {

// 定义整数变量

int age = 20;

System.out.println("年龄是: " + age);

// 定义双精度浮点数变量

double height = 1.75;

System.out.println("身高是: " + height);

// 定义字符变量

char gender = 'M';

System.out.println("性别是: " + gender);

// 定义布尔变量

boolean isStudent = true;

System.out.println("是否是学生: " + isStudent);

}

}

 

2. 变量的类型转换

public class TypeConversion {

public static void main(String[] args) {

int num1 = 10;

double num2 = (double) num1; // 把int类型转换为double类型

System.out.println(num2);

double num3 = 3.14;

int num4 = (int) num3; // 把double类型转换为int类型,会丢失小数部分

System.out.println(num4);

}

}

 

三、运算符

1. 算术运算符

public class ArithmeticOperators {

public static void main(String[] args) {

int a = 10;

int b = 3;

System.out.println("加法: " + (a + b));

System.out.println("减法: " + (a - b));

System.out.println("乘法: " + (a * b));

System.out.println("除法: " + (a / b));

System.out.println("取余: " + (a % b));

}

}

 

2. 关系运算符和逻辑运算符

public class OperatorsExample {

public static void main(String[] args) {

int x = 5;

int y = 3;

boolean result1 = (x > y);

System.out.println("x大于y: " + result1);

boolean result2 = (x == y);

System.out.println("x等于y: " + result2);

boolean result3 = (x > 2 && y < 4);

System.out.println("x大于2并且y小于4: " + result3);

boolean result4 = (x < 2 || y > 4);

System.out.println("x小于2或者y大于4: " + result4);

}

}

 

四、控制结构

1. if - else语句

public class IfElseExample {

public static void main(String[] args) {

int score = 70;

if (score >= 60) {

System.out.println("及格");

} else {

System.out.println("不及格");

}

}

}

 

2. switch - case语句

public class SwitchCaseExample {

public static void main(String[] args) {

int day = 3;

switch (day) {

case 1:

System.out.println("星期一");

break;

case 2:

System.out.println("星期二");

break;

case 3:

System.out.println("星期三");

break;

default:

System.out.println("其他日期");

}

}

}

 

3. for循环

public class ForLoopExample {

public static void main(String[] args) {

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

System.out.println(i);

}

}

}

 

阅读更多 >>>  javase面试题,JAVA多久学完

4. while循环

public class WhileLoopExample {

public static void main(String[] args) {

int j = 0;

while (j < 3) {

System.out.println(j);

j++;

}

}

 

5. do - while循环

public class DoWhileLoopExample {

public static void main(String[] args) {

int k = 0;

do {

System.out.println(k);

k++;

} while (k < 3);

}

}

 

五、方法(函数)

public class MethodExample {

// 定义一个方法,用于计算两个整数的和

public static int add(int a, int b) {

return a + b;

}

public static void main(String[] args) {

int result = add(3, 5);

System.out.println("3和5的和是: " + result);

}

}

 

六、数组

1. 数组的定义和访问

public class ArrayExample {

public static void main(String[] args) {

// 定义一个整数数组

int[] numbers = {1, 2, 3, 4, 5};

System.out.println("第一个元素: " + numbers[0]);

// 定义一个字符串数组

String[] names = new String[3];

names[0] = "张三";

names[1] = "李四";

names[2] = "王五";

System.out.println("第二个名字: " + names[1]);

}

}

 

2. 数组的遍历

public class ArrayTraversal {

public static void main(String[] args) {

int[] array = {1, 2, 3, 4, 5};

// 使用for循环遍历数组

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

System.out.println(array[i]);

}

// 使用增强型for循环(foreach)遍历数组

for (int element : array) {

System.out.println(element);

}

}

}

← 上一篇: 下一篇:

网站数据信息

"java基础代码大全"浏览人数已经达到20次,如你需要查询该站的相关权重信息,可以点击进入"Chinaz数据" 查询。更多网站价值评估因素如:java基础代码大全的访问速度、搜索引擎收录以及索引量、用户体验等。 要评估一个站的价值,最主要还是需要根据您自身的需求,如网站IP、PV、跳出率等!