逻辑运算
1.’!’(非)
2.与(多个条件一起满足)
Java中&&和&都是表示与的逻辑运算符,都表示逻辑运输符and,当两边的表达式都为true的时候,整个运算结果才为true,否则为false。
’&&'的短路功能,当第一个表达式的值为false的时候,则不再计算第二个表达式;
’&'则两个表达式都执行。
public class Test2 {
public static void main(String args[]) {
//if((1==2)&(10/0)==0){//(10/0)==0出现错误
// System.out.println("Hello World");
//}
//解决办法,使用&&,因为(1==2)是false,所以不再判断后面的((10/0)==0),故编译器不会报错
if ((1 == 2) && (10 / 0) == 0) {
System.out.println("Hello World");
}
}
}
'&'可以用作位运算符,当&两边的表达式不是Boolean类型的时候,&表示按位操作。
3.或(多个条件有一个满足)
Java中’||‘和’|'都是表示与的逻辑运算符,都表示逻辑运输符or,当两边的表达式都为false的时候,整个运算结果才为false,否则为true。
|(或)与||(短路或)
(1)相同之处:在程序中,当有其中一个表达式为true时,结果都为true;
(2)不同之处:
a. ‘|’ 当遇到类型为true的表达式时,程序还会继续向后走, 最后结果为true。
b. '||'当遇到类型为true的表达式时,程序不会向下走,但是最终结果还是true。
public class Test3 {
public static void main(String args[]){
//if((1==1)|(10/0)==0){//程序继续往下运行,遇到((10/0)==0)时报错
// System.out.println("Hello World");
//}
//解决方法
if((1==1)||(10/0)==0){//程序遇到第一个为判断结果为true即停止,不再继续往下运行
System.out.println("Hello World");
}
}
}
位运算
位运算在java中有:’&’(与)、’|’(或)、’^’(异或)、’~’、’>>’、’<<’、’>>>’
注意:’&&’、’||'并不能进行位运算
public class Test4 {
public static void main(String args[]){
int num1 = 9 ;
int num2 = 11 ;
//与
System.out.println(num1&num2);
//分析
// 9:00000000 00000000 00000000 00001001
// 11:00000000 00000000 00000000 00001011
//'&'结果:00000000 00000000 00000000 00001001
//故结果为9
//或
System.out.println(num1|num2);
//分析
// 9:00000000 00000000 00000000 00001001
// 11:00000000 00000000 00000000 00001011
//'|'结果:00000000 00000000 00000000 00001011
//故结果为11
}
}
public class Test5 {
//快速计算2的3次方
public static void main(String args[]){
int num = 2 ;
System.out.println(num<<2);//向左移两位
//即由2:00000000 00000000 00000000 00000010
//变为8:00000000 00000000 00000000 00001000
}
}
选择结构
1.if-else结构
2.switch结构
java中switch的用法基本和C语言一样
public class Test6 {
public static void main(String args[]){
int num = 1 ;
switch(num){
case 1:{
System.out.println("该数字为1");
break;
}
case 2:{
System.out.println("该数字为2");
break;
}
case 3:{
System.out.println("该数字为3");
break;
}
default:{
System.out.println("没有匹配的内容");
break;
}
}
}
}
Java的switch结构与C语言最大的区别就是java可以进行字符串判断,这是C语言没有的
public class Test6 {
public static void main(String args[]){
String str = "Hello World" ;
switch(str){
case "hello":{
System.out.println("hello");
break;
}
case "Hello World":{
System.out.println("Hello World");
break;
}
default:{
System.out.println("无匹配的内容");
break;
}
}
}
}
3.循环结构
while循环和do…while循环
while(循环判断){
循环语句;
修改循环结束条件;
}
do{
循环语句;
修改循环结束条件;
}while(循环判断);
由此可以看出while循环是先判断再执行,do…while循环是先执行再判断,在条件相同的情况下,do…while循环至少执行一次,而while循环一次都不会执行。
for循环
java中的for循环与C语言的for循环用法一样
public class Test7 {
public static void main(String args[]){
int num = 0 ;
for(int n = 1 ;n<=100;n++){
num+=n;
}
System.out.println(num);
}
}
方法(函数)的定义与使用
public static 返回值类型 方法名称(参数类型 参数变量,…){
方法体(本方法要执行的若干操作);
[return[返回值];]
}
public class Test8 {
public static void main(String args[]){
printf();
}
public static void printf(){
System.out.println("*************");
System.out.println("*Hello World*");
System.out.println("*************");
}
}
public class Test9 {
public static void main(String args[]){
set(100);
set(3);
}
public static void set(int x){
if(x==3){
return;//遇到return方法直接结束,后面的步骤不再执行
}
System.out.println("x="+x);
}
}
方法的重载
方法的重载简单地来说就是可以定义多个相同名称的方法进行不同的操作
public class Test1_1 {
public static void main(String args[]){
System.out.println("两个整型参数:"+add(10,20));
System.out.println("三个整型参数:"+add(10,20,30));
System.out.println("两个浮点型参数:"+add(10.2,20.3));
}
public static int add(int x,int y){
return x+y;
}
public static int add(int x,int y,int z){
return x+y+z;
}
public static double add(double x,double y){
return x+y;
}
}
注意:
1.虽然方法的调用名称相同,但是编译器会根据其声明的参数个数或类型执行不同的方法体。
2.方法重载的时候重点是根据参数类型及个数来区分不同的方法,而不是依靠返回值的不同来确定的。
之前使用过的System.out.println()和System.out.print()也属于方法的重载
方法的递归调用
public class Test1_2 {
public static void main(String args[]){
System.out.println(sum(100));
}
public static int sum(int num){
if(num==1){
return 1;
}
return num+sum(num-1);
}
}