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);
}
}
输出: