10个简单的java代码教程演练
Java代码不会?这个问题可能很多人都遇到过,因为java语言本身就是一个开源的语言,所以很多人都觉得自己学不会,其实只要掌握了基本的知识,就可以轻松上手了。下面是几个简单的Java代码演练,涵盖了变量、条件语句、循环语句等核心概念,希望对大家有所帮助。如果你想学习java,可以参考一下这篇文章。
1. 变量和基本输出
```java
public class HelloWorld {
public static void main(String[] args) {
// 定义字符串和整数变量
String name = "Alice";
int age = 25;
// 输出变量到控制台
System.out.println("Hello, " + name + "!");
System.out.println("You are " + age + " years old.");
}
}
```
输出结果:
```
Hello, Alice!
You are 25 years old.
```
2. 条件语句
```java
public class Grade {
public static void main(String[] args) {
// 定义变量作为成绩
int score = 85;
// 根据成绩输出不同的等级
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 70) {
System.out.println("C");
} else if (score >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
}
}
```
输出结果:
```
B
```
3. 循环语句
```java
public class Count {
public static void main(String[] args) {
// 从1到10累加
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
// 输出累加结果
System.out.println("1 + 2 + ... + 10 = " + sum);
}
}
```
输出结果:
```
1 + 2 + ... + 10 = 55
```
4. 数组和循环
```java
public class ArraySum {
public static void main(String[] args) {
// 定义数组并初始化
int[] arr = {1, 2, 3, 4, 5};
// 遍历数组并求和
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
// 输出数组元素和
System.out.println("The sum of array elements is " + sum);
}
}
```
输出结果:
```
The sum of array elements is 15
```
5. 方法调用
```java
public class Calculator {
public static void main(String[] args) {
// 调用add方法
int num1 = 10;
int num2 = 5;
int sum = add(num1, num2);
// 输出结果
System.out.println(num1 + " + " + num2 + " = " + sum);
}
// 定义add方法
public static int add(int a, int b) {
return a + b;
}
}
```
输出结果:
```
10 + 5 = 15
```
6. 类和对象
```java
public class Rectangle {
// 定义长和宽变量
int length;
int width;
// 定义计算面积方法
public int getArea() {
return length * width;
}
public static void main(String[] args) {
// 创建Rectangle对象并设置长和宽
Rectangle rect = new Rectangle();
rect.length = 5;
rect.width = 3;
// 输出面积
System.out.println("The area of rectangle is " + rect.getArea());
}
}
```
输出结果:
```
The area of rectangle is 15
```
7. 继承和多态
```java
// 父类
class Animal {
public void sound() {
System.out.println("The animal makes a sound");
}
}
// 子类
class Dog extends Animal {
public void sound() {
System.out.println("The dog says woof");
}
}
// 子类
class Cat extends Animal {
public void sound() {
System.out.println("The cat says meow");
}
}
// 测试类
public class AnimalSound {
public static void main(String[] args) {
// 创建Animal、Dog、Cat对象并调用sound方法
Animal animal = new Animal();
Dog dog = new Dog();
Cat cat = new Cat();
animal.sound(); // 输出 "The animal makes a sound"
dog.sound(); // 输出 "The dog says woof"
cat.sound(); // 输出 "The cat says meow"
// 多态:Animal类型的变量可以引用Dog、Cat对象并调用sound方法
Animal ani1 = new Dog();
Animal ani2 = new Cat();
ani1.sound(); // 输出 "The dog says woof"
ani2.sound(); // 输出 "The cat says meow"
}
}
```
输出结果:
```
The animal makes a sound
The dog says woof
The cat says meow
The dog says woof
The cat says meow
```
8. 接口和实现
```java
// 接口
interface Shape {
public void draw();
public double getArea();
}
// 实现类
class Rectangle implements Shape {
private int length;
private int width;
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
public void draw() {
System.out.println("Drawing a rectangle");
}
public double getArea() {
return length * width;
}
}
// 实现类
class Circle implements Shape {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public void draw() {
System.out.println("Drawing a circle");
}
public double getArea() {
return Math.PI * radius * radius;
}
}
// 测试类
public class ShapeTest {
public static void main(String[] args) {
// 创建Rectangle、Circle对象并调用draw、getArea方法
Rectangle rect = new Rectangle(5, 3);
Circle circle = new Circle(2);
rect.draw(); // 输出 "Drawing a rectangle"
System.out.println("The area of the rectangle is " + rect.getArea()); // 输出 "The area of the rectangle is 15.0"
circle.draw(); // 输出 "Drawing a circle"
System.out.println("The area of the circle is " + circle.getArea()); // 输出 "The area of the circle is 12.566370614359172"
}
}
```
输出结果:
```
Drawing a rectangle
The area of the rectangle is 15.0
Drawing a circle
The area of the circle is 12.566370614359172
```
9. 异常处理
```java
import java.util.Scanner;
public class Divide {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the dividend: ");
int dividend = scanner.nextInt();
System.out.print("Enter the divisor: ");
int divisor = scanner.nextInt();
try {
int quotient = dividend / divisor;
System.out.println(dividend + " / " + divisor + " = " + quotient);
} catch (ArithmeticException e) {
System.out.println("Error: division by zero!");
} finally {
scanner.close();
}
}
}
```
输出结果:
```
Enter the dividend: 10
Enter the divisor: 2
10 / 2 = 5
```
或者
```
Enter the dividend: 10
Enter the divisor: 0
Error: division by zero!
```
10. 文件读写
```java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileInOut {
public static void main(String[] args) {
// 写入文件
try {
// 创建或打开文件
File file = new File("output.txt");
FileWriter writer = new FileWriter(file);
// 写入字符串
writer.write("Hello world!\n");
writer.write("This is a test file.\n");
// 关闭文件
writer.close();
} catch (IOException e) {
System.out.println("Error writing file!");
}
// 读取文件
try {
// 打开文件
File file = new File("output.txt");
Scanner scanner = new Scanner(file);
// 逐行读取并输出到控制台
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
// 关闭文件
scanner.close();
} catch (IOException e) {
System.out.println("Error reading file!");
}
}
}
```
输出结果:
```
Hello world!
This is a test file.
```
德州两山软件开发
软件开发定制报价:13173436190
网站建设开发/小程序定制开发/APP软件开发
本文链接:http://www.dzkaifa.cn/news1/1079.html
文章TAG: #java代码教程演练 #系统开发 #软件开发
版权声明:
本站所有原创作品,其版权属于两开发技( http://www.dzkaifa.cn )所有。任何媒体、网站或个人转载须注明此文章来源URL。被本站授权使用单位,不应超越授权范围。本站部分文章来源于网络,如侵犯到您的权利请联系我们,我们将立即删除。