您的位置:首页 > 编程语言 > Java开发

java学习之路之基本语法-运算符练习题

2016-07-22 22:43 489 查看
public class Exer2 {
//字符串的连接
//声明两个double形的变量并初始化赋值进行除运算,结果显示成如333/2344=xxx的形式
public static void main(String[] args) {
double var1 = 333;
double var2 = 2344;

String str = var1 + " / " + var2 + " = ";

var2 = var1 / var2;

str = str + var2;

System.out.println(str);
}
}

public class OperatorTest {
//前加加,后加加、前减减,后减减的练习
public static void main(String[] args) {
int a = 9;
a++; // 自增, 后++
//a = a + 1;
++a; // 前++
//a = a + 1;
// a:11
int b = a++; // 先用后加
int c = ++a; // 先加后用
// 13
b = --a; // b:12
c = a--; // c:12 , a:11
}
}

public class BinaryTest {
//int型数据进&、|、^、~逻辑运算符的练习
public static void main(String[] args) {
int a = 0x000000A9;
// 0000 0000 0000 0000 0000 0000 1010 1001
int b = 0x000000C7;
// 0000 0000 0000 0000 0000 0000 1100 0111
//分别进行按位与,或,异或, 再分别对两个数进行按位取反,推导过程, 必须先全部转成int型二进制
System.out.println(a & b);
System.out.println(a | b);
System.out.println(a ^ b);
System.out.println(~a);
System.out.println(~b);
}
}

public class StringTest {
//字符串连接练习
public static void main(String[] args) {
String str = "abc你好吗";
str = str + 100; // 字符串可以和任意数据类型连接, 结果是一个新的字符串
str = str + ' ';
str = str + 3.58;
str = str + false;
str = str + "哈哈哈";
System.out.println(str);

String str2 = null; // 地址为空, 没有字符串
String str3 = ""; // 有字符串,但是内容为空

String str4 = "8393";
int i1 = Integer.parseInt(str4); // "8393" -> 8393
System.out.println(i1);

int i2 = 3922; // -> "3922"
String str5 = "" + i2; // 字符串连接, 结果是字符串
System.out.println(str5);
}
}

class StringTest2 {
//double型数据与float型数据间的转换,double型到float型是大范围到小范围需要进行强转
public static void main(String[] args) {
float f1 = 3.89f;
double d1 = 2.33;
float f2 = (float)(f1 + d1);//强制转换
System.out.println("f1:" + f1);
System.out.println("f2:" + f2);
System.out.println("d1:" + d1);
}
}

public class OperatorTest {
//+=赋值运算符的使用
public static void main(String[] args) {
short a = 32767; // 0111 1111 1111 1111
a += 5; // 累加 不会使数据类型发生变量
//a = a + 5; // 任意两个整数运算, 结果都是int型
a *= 3; // a = a * 3;
a /= 0.2; // a = a / 2;
System.out.println("a:" + a);
}

}

class OperatorTest2 {
//输出两个数中的较大数(三元运算符的使用)
public static void main(String[] args) {
int num1 = Integer.parseInt(args[0]); // 把命令行参数的第1个字符串转化为真的整数
int num2 = Integer.parseInt(args[1]); // 把命令行参数的第2个字符串转化为真的整数
int max = num1 > num2 ? num1 : num2;
System.out.println("max:" + max);
}

}

class OperatorTest3 {

public static void main(String[] args) {
// 获取三个数中的较大者
int num1 = Integer.parseInt(args[0]); // 把命令行参数的第1个字符串转化为真的整数
int num2 = Integer.parseInt(args[1]); // 把命令行参数的第2个字符串转化为真的整数
int num3 = Integer.parseInt(args[2]); // 把命令行参数的第3个字符串转化为真的整数
int max = num1 > num2 ? num1 : num2;
max = max > num3 ? max : num3;
System.out.println("max:" + max);
}

}

class OperatorTest4 {
// 变量的交换
public static void main(String[] args) {
int a = 20;
int b = 30;

// 临时变量的类型和原变量的类型一定要一致
int tmp = a; // 先保存a的值,防止丢失
a = b; // 此时a和b的值一样
b = tmp; // 把tmp中原来的a的值再写入b

System.out.println("a:" + a);
System.out.println("b:" + b);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: