B:+运算符的作用有哪些?
short s = 1;
s = s + 1; //short类型转——int类型转——short类型(损失精度)
short s = 1;
s += 1; //强制转换(s =(s的数据类型)(s+1))
C:|和||的区别是什么?
C:||:结果和|是一样的,只不过有短路效果。左边是true,右边不执行。//方式1:使用第三方变量(开发中用的)
int c = a;
a = b;
b = c;
System.out.println("a:"+a+",b:"+b);
System.out.println("------------");
//方式2:用位异或实现(面试用)
//左边:a,b,a
//右边:a ^ b
a = a ^ b;
b = a ^ b; //a ^ b ^ b = a
a = a ^ b; //a ^ b ^ a = b
System.out.println("a:"+a+",b:"+b);
//方式2:用位异或实现(面试用)
//左边:a,b,a
//右边:a ^ b
a = a ^ b;
b = a ^ b; //a ^ b ^ b = a
a = a ^ b; //a ^ b ^ a = b
System.out.println("a:"+a+",b:"+b);
//方式2:用位异或实现(面试用)
//左边:a,b,a
//右边:a ^ b
a = a ^ b;
b = a ^ b; //a ^ b ^ b = a
a = a ^ b; //a ^ b ^ a = b
System.out.println("a:"+a+",b:"+b);
A:比较两个数是否相等
int x = 100;
int y = 200;
int z = ((x == y)? x: y);
B:获取两个数中最大值
int x = 100;
int y = 200;
int z = ((x > y)? x: y);
C:获取三个数中最大值
int a = 10;
int b = 30;
int c = 20;
//方法一,分两步:
//A:先比较a,b的最大值
//B:拿a,b的最大值在和c进行比较
int temp = ((a > b)? a: b);
int max1 = ((temp > c)? temp: c);
System.out.println("max1:"+max1);
//方法二,一步搞定
//int max2 = (a > b)?((a > c)? a: c):((b > c)? b: c);
int x = 1,y = 1;
if(x++==2 & ++y==2) //++在操作符后面,x不等于2,然后在++x=2,前面为false;++在操作符前面,++y=2,后面为true;
{ //与运算一false全false
x =7; //不执行
}
System.out.println("x="+x+",y="+y); //x = 2,y = 2
第二题
int x = 1,y = 1;
if(x++==2 && ++y==2) //&&:结果和&是一样的,只不过有短路效果。左边是false,右边不执行。
{
x =7;
}
System.out.println("x="+x+",y="+y); //x=2,y=1
第三题
int x = 1,y = 1;
if(x++==1 | ++y==1) //前面为false,后面为true
{ //或运算一true全true
x =7; //执行
}
System.out.println("x="+x+",y="+y); //x=7,y=2
第四题
int x = 1,y = 1;
if(x++==1 || ++y==1) //前面为false,后面不执行
{
x =7; //执行
}
System.out.println("x="+x+",y="+y); //x=7,y=1
第五题
boolean b = true;
if(b==false) //false
System.out.println("a");
else if(b) //true 循环结束
System.out.println("b");
else if(!b)
System.out.println("c");
else
System.out.println("d");
//b
/*
*===============================================================================
* Program: Grades
* Keyboard entry a result, and the level of output results.
* 90-100 excellent
* 80-90 good
* 70-80 fine
* 60-70 pass
* 0-60 fail
*
*
* About:
* Date:2017/06/02
* @author: Bruce_You [email protected]
* @version: 1.0
*===============================================================================
*/
package text;
import java.util.Scanner;
public class Grades {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//
System.out.println("Please enter your grades。");
// create a keyboard entry object
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if(score<0 || score>100) {
System.out.println("Your input is wrong。");
}else if(score>=90 && score<=100) {
System.out.println("excellent");
}else if(score>=80 && score<90) {
System.out.println("good");
}else if(score>=70 && score<80) {
System.out.println("fine");
}else if(score>=60 && score<70) {
System.out.println("pass");
}else {
System.out.println("fail");
}
System.out.println();
System.out.println("The author:");
System.out.println("Bruce_You");
}
}
/*
* ##################################################################
* ########################## End Of Grades #########################
* ##################################################################
*/