Java学习笔记(三)

String类型变量的使用

1.String属于引用数据类型,翻译为:字符串

2.声明String类型变量时,使用一对“”

3.String可以和8种基本数据类型变量做运算,且运算只能是连接运算:+

4.运算的结果仍然是String类型

用三元运算符输出三个数中最大的值

class SanYuanTest 
{
	public static void main(String[] args) 
	{
		int a=23,b=24,c=67;
		int max1 = (a>b)? a : b;
		int max2 = (max1>c)? max1 : c;
		System.out.println(max2);
		System.out.println("Hello World!");
	}
}

以下是输出结果:

在这里插入图片描述

交换两个值

class TransTest 
{
	public static void main(String[] args) 
	{
		int a = 34,b = 45;
		int term = a;
		a = b;
		b = term;
		System.out.println("a="+a+",b="+b);
		System.out.println("Hello World!");
	}
}

以下是输出结果:

在这里插入图片描述

用if判断两个数

class IfTest 
{
	public static void main(String[] args) 
	{
		double a1 = 7,b1 = 13,c1;
		if(a1>10&&b1<20){
		c1 = a1+b1;
		}else{
		c1 = a1*b1;
		}
		System.out.println(c1);
		System.out.println("Hello World!");
	}
}

输出:

做个测试

class OperatorTest {
	public static void main(String[] args) {
		boolean x = true;
		boolean y = false;
		short z = 40;
		if ((z++ == 40) && (y = true)) {
			z++;
			System.out.println("if1执行了");
		}
		if ((x = false) || (++z == 43)) {
			z++;
		}
		System.out.println("z = " + z);
	}
}

输出:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值