Day3
Java运算符
首先我们讲了Java的关系运算符(==、!=、>、<、>=、<=)和逻辑运算符(&&、||、!)
大致代码如下:
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a =12;
int b=80;
System.out.println("a>b结果="+(a>b));
System.out.println("a==b结果="+(a==b));
System.out.println("a<b结果="+(a<b));
System.out.println("a>=b结果="+(a>=b));
System.out.println("a<=b结果="+(a<=b));
System.out.println("a!=b结果="+(a!=b));
//关系运算
boolean b1 =true;
boolean b2 =false;
boolean b3 =true;
System.out.println("b1且b2结果="+(b1&&b2));
System.out.println("b1且b3结果="+(b1&&b3));
//对于且,只有两边都是真,结果才是真
System.out.println("b1或b2结果="+(b1||b2));
//对于或,只有两边都是假,结果才是假
System.out.println("b1反过来="+(!b1));
System.out.println("b2反过来="+(!b2));
//把一个boolean类型的值反过来,需要一个感叹号
}
}
流程控制
接着又讲了流程控制,讲到了四种流程控制的语句,分别是if(){}else{}、switch 语句格式(switch需要注意添加break)、while 语句(写while语句需要当心,不然容易写出死循环的程序导致服务器崩溃)、do…while
大致代码如下:
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean f1=true;
boolean f2=false;
if(f1){
System.out.println("f1是真的");
}
//简单情况,一次判断
if(f2){
System.out.println("f2是真的");
}else{
System.out.println("f2是假的");
}
//两种情况 if{}else{}
if(f1&&f2){
System.out.println("f1和f2都是真的");
}else if(f1||f2){
System.out.println("f1和f2中只有一个是真的");
}else{
System.out.println("f1和f2都是假的");
}
//三种情况 if(){}else if(){}else(){}
}
}
public class Test3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=20;
switch(a){
case 10:
System.out.println("a的值是10");
break;
case 20:
System.out.println("a的值是20");
// break;
case 30:
System.out.println(“a的值是30”);
// break;
//如果忘记写break,会一直执行下去,直到碰到一个break停止
}
}
}
public class Test4 {
public static void main(String[] args){
int a=10;
while(a>0){
System.out.println(“a=”+a);
a=a-1;
//相等于a–
}
int b=8;
do{
System.out.println("b="+b);
b--;//相当于a--
}while(b>10);
}
}
Java String
String类的常用方法
大致代码如下(注释在代码中)
public class StringTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = " ajfhkas hfikw ";
String b="ss";
String c="ss";
System.out.println(a);
System.out.println(a.lastIndexOf("w"));
//从末位开始查找
System.out.println(a.indexOf("f"));
System.out.println(a.indexOf("hkas"));
System.out.println(a.indexOf("aa"));
//返回字符串的下标值,如有多个,返回第一个的下标值,如果字符串不存在,返回-1
System.out.println(a.contains("a"));
//检测字符串中是否包含,包含即是真,写两个必须是连续的
System.out.println(a.charAt(1));
System.out.println(a.charAt(11));
//返回下标对应的字符
System.out.println(a.equals(b));
System.out.println(c.equals(b));
//判断两个字符串是否相同,用equals方法
System.out.println(a.trim());
//消除前后的空格
System.out.println(a.endsWith(" "));
System.out.println(a.endsWith("w"));
//endwith 最后一个字符是否相同,空格也算一个字符
System.out.println(a.substring(0, 3));
System.out.println(a.length());
//字符串的长度
System.out.println(a.substring(a.length()-3,a.length()-1));
//截取字符串
}
}
Array
最后讲了数组
写数组的三种方法:
int[] a=new int[3];
a[0]=23;
a[1]=123;
a[2]=236;
int[] b=new int[]{123,234,456,1234,453};
int[] c={123,2354,1235};
遍历数组:
public class ArrayTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a=new int[3];
a[0]=23;
a[1]=123;
a[2]=236;
int i=0;
do{
System.out.println(a[i]);
i++;
}while(i<=2);
}
}
Print和Println的区别
Println是为后一个输出换行,并不是当前输出换行
大致代码如下:
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(" ");
System.out.print(" ");
System.out.print("△");
System.out.print(" ");
System.out.println(" ");
System.out.print(" ");
System.out.print("△△△");
System.out.println(" ");
System.out.println("△△△△△");
System.out.print("A");
System.out.println("B");
System.out.print("C");
}
}