// 定义基础数据类型
// 不可被更改的常量: final int number = 10
fianl int number = 10;
int number1 = 10;
byte number0 = -128;
byte number00 = 127;
double number2 = 10.55;
char str = '3';
String str2 = "hello";
//1. length()方法
String s1 = "four";
String s2 = "five";
int s1_number = s1.length(); //4
int s2_number = s2.length(); //4
//2.equal方法
String s = "java";
String S = "Java";
boolean cp = s.equals(S);
//忽略大小写时:
boolean CP = s.equalsIgnoreCase(S);
//3.字符串的连接
String s1 = "java";
String s2 = "good";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
//4.相关查询
//查询某字符 or 字符串第一次出现的位置
String str = "abcdear";
int a = str.indexOf("a");
int b = str.indexOf("bc");
int c = str.lastIndexOf("a");//最后一次出现
String d = str.substring(2, 5);//不包括最后一个
int e = str.charAt(3);//按照索引获得对应的字符
//StringBuilder创建一个带有16容量的出存容器
StringBuilder s1 = new StringBuilder();//容量为16
StringBuilder s2 = new StringBuilder(5); //此时容量为5
StringBuilder s3 = new StringBuilder
("hello"); //此时容量为 16 + 5 = 21
//基本方法:
// capacity(); 返回当前容器的容量
int number = s1.capacity();
//toString();返回数据的string格式?
int number2 = 33;
Integer.toString(number2);
//deleteCharAt(i); 删除对应下标的字符
String a = "avsdas"
StringBuilder a_new = StringBuilder(a);
a.deleteCharAt(3);
//两个都为真的时候为真
a & b
//两个中有一个为真的时候就为真
a | b
//真则结果为假 假则结果为真
!a
//如果两个逻辑一样则为真
a^b
//方法修饰符 返回值类型 方法名(参数列表){方法主体}
//方法修饰符: public(表示可以被变量所使用)、protected、private、
//返回值类型:如果没有返回值则为void;如果有返回值则为对应的类型,并在主体中return;
public class method_test(){
public static void mian(String[] args){
method;
}
//编写了一个方法method()
//静态方法只能用静态方法调用(?)
public static void method(){
System.out.println("life")
}
}
----------------------------
if(条件){
代码块
}
else if(条件){
代码块
}
else{
代码块
}
----------------------------
if(条件){
if(条件){
}else{
}
}
else{
}
switch(表达式){
case1(表达式计算的结果):
代码块1
break;
case2:
代码块2
break;
···
default:
默认执行的代码
}
1.while循环(先判断再做)
while(条件){
}
2.do-while循环(先运行再判断)
do{
}while(条件)
for(循环变量初始化;循环条件;循环变量值操作){
循环主体
}
for (int a = 0; a < 100; a++){
if(a % 3 == 0){
System.out.println(a);
}
}
//定义一个数组
数据类型[] 数组名
int[] ages = {1,45,4,3,3,2}
String[] name = new String[30]//是个数还是长度
//遍历数组中的数据
for(int num = 0;num < ages.length;num++){
System.out.println(ages[num]);
}
//方法2
for(int num2:ages){
System.out.println(ages[num])
}
//将一个数组赋值给另一个数组
int[] ages = {1,45,4,3,3,2}
int[] ages2;
ages2 = ages;
//此时ages2的值会随着ages的变化而变化
//二维数组需要被定义
int[][] site = {{1,7,4}{3,6,5}} //两行三列
1 | 7 | 4 |
---|---|---|
3 | 6 | 5 |
import java.unit.Scanner;
public class ScannerDemo{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String s = in.nextLine();
while(!in.hasNext("exit")){
System.out.println(in.nextLine());
}
in.colse();
}
}
本文章使用limfx的vscode插件快速发布