德州网站开发定制-欧美精品福利-小程序开发制作-欧美精品二区三区四区免费看视频-APP软件开发--「两山开发」

10個(gè)簡單的java代碼教程演練

發(fā)布時(shí)間:2023-04-25 14:37:00 欄目:行業(yè)動(dòng)態(tài) 作者:趙麗君 點(diǎn)贊:

Java代碼不會(huì)?這個(gè)問題可能很多人都遇到過,因?yàn)閖ava語言本身就是一個(gè)開源的語言,所以很多人都覺得自己學(xué)不會(huì),其實(shí)只要掌握了基本的知識(shí),就可以輕松上手了。下面是幾個(gè)簡單的Java代碼演練,涵蓋了變量、條件語句、循環(huán)語句等核心概念,希望對(duì)大家有所幫助。如果你想學(xué)習(xí)java,可以參考一下這篇文章。


10個(gè)簡單的java代碼教程演練(圖1)

1. 變量和基本輸出

```java

public class HelloWorld {

    public static void main(String[] args) {

        // 定義字符串和整數(shù)變量

        String name = "Alice";

        int age = 25;

        

        // 輸出變量到控制臺(tái)

        System.out.println("Hello, " + name + "!");

        System.out.println("You are " + age + " years old.");

    }

}

```


輸出結(jié)果:


```

Hello, Alice!

You are 25 years old.

```

2. 條件語句

```java

public class Grade {

    public static void main(String[] args) {

        // 定義變量作為成績

        int score = 85;

        

        // 根據(jù)成績輸出不同的等級(jí)

        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");

        }

    }

}

```


輸出結(jié)果:


```

B

```

3. 循環(huán)語句

```java

public class Count {

    public static void main(String[] args) {

        // 從1到10累加

        int sum = 0;

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

            sum += i;

        }

        

        // 輸出累加結(jié)果

        System.out.println("1 + 2 + ... + 10 = " + sum);

    }

}

```


輸出結(jié)果:


```

1 + 2 + ... + 10 = 55

```

4. 數(shù)組和循環(huán)

```java

public class ArraySum {

    public static void main(String[] args) {

        // 定義數(shù)組并初始化

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

        

        // 遍歷數(shù)組并求和

        int sum = 0;

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

            sum += arr[i];

        }

        

        // 輸出數(shù)組元素和

        System.out.println("The sum of array elements is " + sum);

    }

}

```


輸出結(jié)果:


```

The sum of array elements is 15

```


5. 方法調(diào)用

```java

public class Calculator {

    public static void main(String[] args) {

        // 調(diào)用add方法

        int num1 = 10;

        int num2 = 5;

        int sum = add(num1, num2);

        

        // 輸出結(jié)果

        System.out.println(num1 + " + " + num2 + " = " + sum);

    }

    

    // 定義add方法

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

        return a + b;

    }

}

```


輸出結(jié)果:


```

10 + 5 = 15

```

6. 類和對(duì)象

```java

public class Rectangle {

    // 定義長和寬變量

    int length;

    int width;


    // 定義計(jì)算面積方法

    public int getArea() {

        return length * width;

    }


    public static void main(String[] args) {

        // 創(chuàng)建Rectangle對(duì)象并設(shè)置長和寬

        Rectangle rect = new Rectangle();

        rect.length = 5;

        rect.width = 3;

        

        // 輸出面積

        System.out.println("The area of rectangle is " + rect.getArea());

    }

}

```


輸出結(jié)果:


```

The area of rectangle is 15

```

7. 繼承和多態(tài)

```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");

    }

}


// 測(cè)試類

public class AnimalSound {

    public static void main(String[] args) {

        // 創(chuàng)建Animal、Dog、Cat對(duì)象并調(diào)用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"

        

        // 多態(tài):Animal類型的變量可以引用Dog、Cat對(duì)象并調(diào)用sound方法

        Animal ani1 = new Dog();

        Animal ani2 = new Cat();

        

        ani1.sound();   // 輸出 "The dog says woof"

        ani2.sound();   // 輸出 "The cat says meow"

    }

}

```


輸出結(jié)果:


```

The animal makes a sound

The dog says woof

The cat says meow

The dog says woof

The cat says meow

```

8. 接口和實(shí)現(xiàn)

```java

// 接口

interface Shape {

    public void draw();

    public double getArea();

}


// 實(shí)現(xiàn)類

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;

    }

}


// 實(shí)現(xiàn)類

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;

    }

}


// 測(cè)試類

public class ShapeTest {

    public static void main(String[] args) {

        // 創(chuàng)建Rectangle、Circle對(duì)象并調(diào)用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"

    }

}

```


輸出結(jié)果:


```

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();

        }

    }

}

```


輸出結(jié)果:


```

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 {

            // 創(chuàng)建或打開文件

            File file = new File("output.txt");

            FileWriter writer = new FileWriter(file);


            // 寫入字符串

            writer.write("Hello world!\n");

            writer.write("This is a test file.\n");


            // 關(guān)閉文件

            writer.close();

        } catch (IOException e) {

            System.out.println("Error writing file!");

        }


        // 讀取文件

        try {

            // 打開文件

            File file = new File("output.txt");

            Scanner scanner = new Scanner(file);


            // 逐行讀取并輸出到控制臺(tái)

            while (scanner.hasNextLine()) {

                System.out.println(scanner.nextLine());

            }


            // 關(guān)閉文件

            scanner.close();

        } catch (IOException e) {

            System.out.println("Error reading file!");

        }

    }

}

```


輸出結(jié)果:


```

Hello world!

This is a test file.

```

聯(lián)系二維碼
德州兩山軟件開發(fā)

軟件開發(fā)定制報(bào)價(jià):13173436190

網(wǎng)站建設(shè)開發(fā)/小程序定制開發(fā)/APP軟件開發(fā)

本文鏈接:http://www.kmly315.cn/news1/1079.html

文章TAG: #java代碼教程演練 #系統(tǒng)開發(fā) #軟件開發(fā)

版權(quán)聲明:

本站所有原創(chuàng)作品,其版權(quán)屬于兩開發(fā)技( http://www.kmly315.cn )所有。任何媒體、網(wǎng)站或個(gè)人轉(zhuǎn)載須注明此文章來源URL。被本站授權(quán)使用單位,不應(yīng)超越授權(quán)范圍。本站部分文章來源于網(wǎng)絡(luò),如侵犯到您的權(quán)利請(qǐng)聯(lián)系我們,我們將立即刪除。
主站蜘蛛池模板: 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | PVC快速门-硬质快速门-洁净室快速门品牌厂家-苏州西朗门业 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 浙江工业冷却塔-菱电冷却塔厂家 - 浙江菱电冷却设备有限公司 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 北京乾茂兴业科技发展有限公司 | 动库网动库商城-体育用品专卖店:羽毛球,乒乓球拍,网球,户外装备,运动鞋,运动包,运动服饰专卖店-正品运动品网上商城动库商城网 - 动库商城 | 东莞动力锂电池保护板_BMS智能软件保护板_锂电池主动均衡保护板-东莞市倡芯电子科技有限公司 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 微妙网,专业的动画师、特效师、CG模型设计师网站! - wmiao.com 超声波电磁流量计-液位计-孔板流量计-料位计-江苏信仪自动化仪表有限公司 | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 中国在职研究生招生信息网| 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 大通天成企业资质代办_承装修试电力设施许可证_增值电信业务经营许可证_无人机运营合格证_广播电视节目制作许可证 | 烟气在线监测系统_烟气在线监测仪_扬尘检测仪_空气质量监测站「山东风途物联网」 | 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 能量回馈_制动单元_电梯节能_能耗制动_深圳市合兴加能科技有限公司 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 色油机-色母机-失重|称重式混料机-称重机-米重机-拌料机-[东莞同锐机械]精密计量科技制造商 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 安徽免检低氮锅炉_合肥燃油锅炉_安徽蒸汽发生器_合肥燃气锅炉-合肥扬诺锅炉有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 深圳市万色印象美业有限公司| 谈股票-今日股票行情走势分析-牛股推荐排行榜 | 振动时效_振动时效仪_超声波冲击设备-济南驰奥机电设备有限公司 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 离子色谱自动进样器-青岛艾力析实验科技有限公司 | 水厂自动化|污水处理中控系统|水利信息化|智慧水务|智慧农业-山东德艾自动化科技有限公司 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 大型低温冷却液循环泵-低温水槽冷阱「厂家品牌」京华仪器_京华仪器 | 样品瓶(色谱样品瓶)百科-浙江哈迈科技有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 作文导航网_作文之家_满分作文_优秀作文_作文大全_作文素材_最新作文分享发布平台 | 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] | 防潮防水通风密闭门源头实力厂家 - 北京酷思帝克门窗 |