JAVA全栈--实操型--笔记
基础实操 记录
环境
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<arrlist.length;i++){
for (j=1;j<i;j++){
if (arrlist[i]>arrlist[j]){
temp=arrlist[j];
arrlist[j]=arrlist[i];
arrlist[i]=temp;
}
}
}
for (int o=0;o<arrlist.length;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<number;i++){
int index=r.nextInt(namelist.length); //随机数0-7
int dex=0,min;
random:
for (int j=0;j<recordnum.length;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();
}
}
/*
写入操作案例
try {
FileWriter write=new FileWriter("C:\\Users\\ASUS1\\Desktop\\newfile.txt");
BufferedWriter bw=new BufferedWriter(write);
bw.write("hello\n");
bw.write("world\n");
bw.close();
write.close();
}catch(IOException e){
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));
}
}