栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【7.11-7.14 JAVA全栈--实操型--笔记】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【7.11-7.14 JAVA全栈--实操型--笔记】

JAVA全栈--实操型--笔记
  • 基础实操 记录
    • 环境
      • 入门程序(Helloworld)
      • 通过冒泡法排序
      • 随机点名器(可同时点名多个不重复)
      • 记录一串字符串中一个字母出现的次数
      • (进阶)记录每个数字出现的次数
      • 读取文件
      • 删除文件操作
      • 创建文件
      • 两个值互相交换(重点)
      • BigDecimal(扩展)

基础实操 记录 环境

IDEA && JDK 1.8.0

入门程序(Helloworld)
package com.cn;

public class Helloworld {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

print及println区别:

package com.cn;

public class Helloworld {
    public static void main(String[] args) {
        System.out.print("Hello world!"); //print
        System.out.println("你好世界!");  //println
    }
}

通过冒泡法排序
package com.cn;

public class BubbleSort {
    public static void main(String[] args) {
        //从大到小排序
        byte [] arrlist = {127,12,56,87,21,98,-12,-32,65}; //9
        int i,j;
        byte temp;
        for (i=0;i
            for (j=1;j
                if (arrlist[i]>arrlist[j]){
                    temp=arrlist[j];
                    arrlist[j]=arrlist[i];
                    arrlist[i]=temp;
                }
            }

        }
        for (int o=0;o
            System.out.print(arrlist[o]+"  ");
        }

    }
}

随机点名器(可同时点名多个不重复)
package com.cn;

import java.util.Random;
import java.util.Scanner;

public class Rollcall {
    public static void main(String[] args) {
        String [] namelist={"小明","小红","小七","小四","小五","小丽","小昝","小张"}; // 输入八个人的信息
        Random r= new Random();
        Scanner in = new Scanner(System.in);//定义scanner,等待输入
        System.out.println("请输入需要抽取的人数:");
        int number = in.nextInt();//字符类型的输入方式
        int [] recordnum =new int[number];
        for (int i=0;i
            int index=r.nextInt(namelist.length); //随机数0-7
            int dex=0,min;
            random:
            for (int j=0;j
                if (index==recordnum[j]){
                    min=r.nextInt(8); //随机数0-7
                    index=min;
                    continue random;
                }else {
                    recordnum[dex]=index;
                }
            }
            System.out.println(namelist[index]);
        }
    }
}

记录一串字符串中一个字母出现的次数
package com.cn;

public class StatisticalAlphabet {
    public static void main(String[] args) {
        String str = "abcdddsasgfff";
        //将字符串转化为数组,方便后面的比较记录
        String[] strtem = str.split("");
        int times = 0;
        for (int i = 0; i < strtem.length; i++) {
            if (strtem[i].equals("a")) {
                times++;
            }
        }
        System.out.println("字母a出现的次数为:" + times);
    }
}

(进阶)记录每个数字出现的次数
package com.cn;

import java.util.Random;
import java.util.ArrayList;

public class NumSum {
    public static void main(String[] args) {
        Random r = new Random();
        int number = 0;
        //产生1000W个0-10的随机数
        int[] numbers = new int[1000_0000];
        for (int p = 0; p < 1000_0000; p++) {
            number = r.nextInt(11);
            numbers[p] = number;
        }
        int n, temp, sumindex = 0, l;
        //定义记录数组的长度
        int[] arrssum = new int[numbers.length];
        long start = System.currentTimeMillis();
        // for1 记录数字 9
        outer:
        for (n = 0; n < numbers.length; n++) {
            //减少多次重复输出同一数字记录
            temp = numbers[n];
            int i, times = 0;
            for (l = 0; l < arrssum.length; l++) {
                if (temp == arrssum[l]) {
                    continue outer;
                }
            }
            //for2 记录次数
            for (i = 0; i < numbers.length; i++) {
                if (numbers[i] == temp) {
                    times++;
                }
            }
            //输出记录的数字出现的次数
            System.out.println("数字:" + temp + "次数:" + times);
            arrssum[sumindex] = temp;
            sumindex++;
        }
        long end = System.currentTimeMillis();
        long sum = end - start;
        System.out.println("所耗时:" + sum + "ms");
    }
}

读取文件
package com.cn;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class FileRead {
    public static void main(String[] args) {
        String namefilepath = "D:/JAVATEST/NameList.txt";
        File file = new File(namefilepath);
        try {
            InputStreamReader input = new InputStreamReader(new FileInputStream(file));
            BufferedReader bufferdata = new BufferedReader(input);

            String line = null;
            line = bufferdata.readLine();

            while (line != null) {
                System.out.print(line);
                line = bufferdata.readLine();
            }

        } catch (Exception e) {
            System.out.println("读取失败!");
            e.printStackTrace();
        }
    }
    
}

删除文件操作
package com.cn;

import java.io.File;

public class FIlesDelete {
    public static void main(String[] args) {
        String filepath = "D:/JAVATEST/testfile.txt";
        File myFilepath = new File(filepath);
        try {
            if (myFilepath.exists()) {
                myFilepath.delete();
                System.out.println("文件删除成功!");
            } else {
                System.out.println("删除的文件不存在!");
            }


        } catch (Exception e) {
            System.out.println("文件删除失败!");
            e.printStackTrace();
        }
    }
}

创建文件
package com.cn;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

public class FilesModify {
    public static void main(String[] args) throws Exception {
        String filepath = "D:/JAVATEST/testfile.txt";  //文件的路径
        File myfilepath = new File(filepath);
        try {
            //判断文件路径中的文件是否存在
            if (!myfilepath.exists()) {
                //创建新的文件
                myfilepath.createNewFile();
            }

            FileWriter resulFile = new FileWriter(myfilepath);
            PrintWriter myFile = new PrintWriter(resulFile);
            resulFile.close();
            System.out.println("新建文件成功!");
        } catch (Exception e) {
            System.out.println("新建文件失败!");
            e.printStackTrace();
        }
    }
}

两个值互相交换(重点)
package com.etime01;

public class Exchange {
    public static void main(String[] args) {
        int x = 3;
        int y = 4;
        int temp;
        System.out.println("交换前的X:" + x + "Y:" + y);
        //同理有加减乘除及异或也可用
        x = x + y;
        y = x - y;
        x = x - y;
        System.out.println("交换后的X:" + x + "Y:" + y);

        x = x ^ y; //x=x^y
        y = y ^ x; // y = y^x^y
        x = x ^ x; // x = x^x^y
        System.out.println("交换后的X:" + x + "Y:" + y);

    }
}

BigDecimal(扩展)
package com.etime01;

import java.math.BigDecimal;
import java.util.Scanner;

public class DecimalTest {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);//定义scanner,等待输入
        System.out.println("请输入数字a:");
        double a = in.nextDouble();//字符类型的输入方式
        System.out.println("请输入数字b:");
        double b = in.nextDouble();//字符类型的输入方式
        BigDecimal intStr = BigDecimal.valueOf(a);
        BigDecimal doubleStr = new BigDecimal(Double.toString(b));

        System.out.println(intStr);
        System.out.println(doubleStr);

        //BigDecimal运算
        BigDecimal sum, mul;
        sum = intStr.add(doubleStr);
        mul = intStr.multiply(doubleStr);
        System.out.println(intStr + "+" + doubleStr + "=" + sum);
        System.out.println(intStr + "*" + doubleStr + "=" + mul);
        System.out.println("四舍五入,保留两位小数:" + intStr + "/" + doubleStr + "=" + intStr.divide(doubleStr, 2, BigDecimal.ROUND_HALF_UP));
    }


}

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017524.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号