class AirTest{
public static void main(String[] args)
{
int num1= 12;
int num2 = 5;
int result1 = num1/num2;
System.out.println(result1);//2
int result2 = num1/num2*num2;
System.out.println(result2);//10
double result3 = num1/num2;
System.out.println(result3);//2.0
double result4 = num1/num2 +0.0;//2.0
double result5 = num1/(num2+0.0);//2.4
double result6 = (double)num1 +num2;//2.4
double result7 = (double)(num1 / num2);//2.0
System.out.println(result5);
//%:取余运算
//结果的符号与被模数的符号相同
//
int m1 = 12;
int n1 = 5;
System.out.println("m1 % n1 = "+m1 % n1);
int m2 = -12;
int n2 = 5;
System.out.println("m2 % n2 = "+m2 % n2);
int m3 = 12;
int n3 = -5;
System.out.println("m3 % n3 = "+m3 % n3);
int m4 = -12;
int n4 = -5;
System.out.println("m4 % n4 = "+m4 % n4);
//(前)++:先自增1,然后再运算
//(后)++:先运算,后自增1
int a1 = 10;
int b1 = ++a1;
System.out.println("a1="+a1+",b1="+b1);
int a2 = 10;
int b2 = a2++;
System.out.println("a2="+a2+",b2="+b2);
//注意点:
short s1 = 10;
//s1 = s1 +1;//编译失败
//s1 = (short)(s1+1);//正确的
s1++;//自增1不会改变本身变量的数据类型
System.out.println(s1);
//问题:
byte bb1=127;
bb1++;
System.out.println("bb1 ="+bb1);
//(前)--:先自减1,后运算
//(后)--:先运算,后自减1
int a4 = 10;
int b4 = --a4;
System.out.println("a4="+a4+",b4="+b4);
}
}
/*
class VariableTest2{
public static void main(String[] args){
byte b1 = 2;
int i1 = 12;
//编译不通过
//byte b2 = b1+i1;
int i2 = b1 + i1;
System.out.println(i2);
float f = b1+i1;
System.out.println(f);
//*************************
char c1 = 'a'; //97
int i3 = 10;
int i4 = c1+i3;
System.out.println(i4);
short s2 = 10;
//编译不通过
//short s3=c1 + s2;
byte b2 = 10;
//char c3 = c1+b2;//编译不通过
}
}
class VariableTest3
{
public static void main(String[] args)
{
double d1 = 12.3;
int i1 = (int)d1;//截断
System.out.println(i1);
//编译失败
//float f1 = 12.3
//整型常量,默认类型为int型
//浮点型常量,默认类型为double型
byte b = 12;
// byte b1 = b+1;//编译失败
//float f1 = b+12.3;//编译失败
}
}
*/
class StringTest{
public static void main(String[] args)
{
String s1 = "Hello World!";
System.out.println(s1);
String s2 = "a";
System.out.println(s2);
String s3 = "";
System.out.println(s3);
//char c='' 编译不通过
int number = 1001;
String numberStr = "学号: ";
String info = numberStr + number;//+ : 链接运算
System.out.println(info);
boolean b1 = true;
String info1 = info+b1;//+ 连接运算
System.out.println(info1);
//**************
//练习1
char c='a';//97 A65
int num = 10;
String str = "hello1";
System.out.println(c+num+str);//107hello1
System.out.println(c+str+num);//ahello110
System.out.println(c+(num+str));//a10hello1
System.out.println((c+num)+str);//107hello1
System.out.println(str+num+c);//hello110a
//练习2
//* *
System.out.println("* *");//输出 * *
System.out.println('*'+'\t'+'*');//输出 93
System.out.println('*'+"\t"+'*');//输出 * *
System.out.println('*'+'\t'+"*");//输出 51*
System.out.println('*'+('\t'+"*"));//输出 * *
byte b = 3;
byte b11 = (byte)(b+3);
System.out.println(b11);
}
}
class SetValueTest{
public static void main(String[] args){
short s1 = 10;
//s1=s1 + 2;//编译失败
s1+=2;// 不会改变变量本身的数据类型
System.out.println(s1);
int s2 = 11;
s2 *=0.1;//不会改变变量本身的数据类型
System.out.println(s2);
int n=10;
n += (n++)+(++n);//n=n+(n++)+(++n); 10+10 +12
System.out.println(n);
}
}
java学习
最新推荐文章于 2024-09-13 11:00:36 发布