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

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

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);
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三元运算符是一种简洁的条件判断语句,它的语法是:条件表达式 ? 表达式1 : 表达式2。如果条件表达式为真,则执行表达式1;如果条件表达式为假,则执行表达式2。下面是几个关于三元运算符练习题: 1. 利用三元运算符求任意三个数中最大的数: ```java import java.util.Scanner; public class 求三个数中最大值 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入第一个数:"); int x = in.nextInt(); System.out.println("请输入第二个数:"); int y = in.nextInt(); System.out.println("请输入第三个数:"); int z = in.nextInt(); int max1 = (x > y) ? x : y; // 比较x和y的大小,并将较大的那个赋值给max1 int max = (max1 > z) ? max1 : z; // 比较max1和z的大小,并将较大的那个赋值给max System.out.println("三个数中最大的数是:" + max); } } ``` 2. 利用三元运算符将一个三位数拆分成个位、十位和百位: ```java import java.util.Scanner; public class 数值拆分 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入三位数:"); int num = in.nextInt(); int ge = num % 10; // 取个位 int shi = ((num - ge) / 10) % 10; // 取十位 int bai = (num - shi * 10 - ge) / 100; // 取百位 System.out.println("个位数是:" + ge); System.out.println("十位数是:" + shi); System.out.println("百位数是:" + bai); } } ``` 3. 判断任意两个数的和是否是3的倍数或其中一个数是否为3: ```java import java.util.Scanner; public class 判断和是否是3的倍数 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入第一个数字:"); int x = in.nextInt(); System.out.println("请输入第二个数字:"); int y = in.nextInt(); boolean result = ((x + y) % 3 == 0) || x == 3 || y == 3; System.out.println(result); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值