不知道在哪写程序,请看项目的创建和输出
-
** 八种基本数据类型:byte、short、int、long、float、double、boolean、char**
数据类型由小到大依次为:byte <short <int <long <float <double <char
1.byte : 字节类型,占用1个字节,取值范围为-128~127。
2.shot : 短整型,占用2个字节,取值范围为-32768~32767。
3.int : 整型,占用4个字节,取值范围为-2147483648~2147483647。
4.1ong: 长整型,占用8个字节,取值范围为-9223372036854775808~9223372036854775807。
5.f1oat: 单精度浮点型,占用4个字节,取值范围为3.40282347E+38F~1.4E-45F。
6.double: 双精度浮点型,占用8个字节,取值范围为1.7976931348623157E+308~4.9E-324。
7.char : 字符型,占用2个字节,取值范围为0~65535。
8.boolean: 布尔型,占用1个字节,取值范围为true或false。 -
类型间的转换:
自动/隐式类型转换:小类型转为大类型
强制类型转换: 大类型转为小类型
整数直接量可以直接赋值给byte,short,char,但不能超出范围
byte,short,char型数据参与运算时,系统会一律先将其转为int再运算 -
变量的使用
int a; //声明了一个整型的变量,名为a int b,c,d; //声明了三个整型的变量,名为b,c,d //int a; //编译错误,变量不能同名 //长整型的直接量需在数字后加L或l long a = 125L; // 长整型 //小数直接量默认为double型,若想表示float,需在数字后加F或f float b = 3.15F;// 单精度浮点型 double a = 3.15; //3.14为小数,默认为double类型 //boolean:布尔型,1个字节,只能存储true或false boolean a = true; boolean b = false; //char:字符型,2个字节 条件:单引号,有字符,必须单个字符 char c1 = '中'; //字符中
-
运算符:
算术:+、-、*、/、%、++、–
关系:>、<、>=、<=、==、!=
逻辑:&&、||、!
赋值:=、+=、-=、*=、/=、%=
字符串连接:+
条件/三目:boolean?数1:数2
int a,b;
a=4;
b=5;
int c = a+b;
int c1 = a-b;
int c11 = a*b;
int c111 = a/b;
int c1111 = a%b;
System.out.println(c1111); //1,商2余1
// ++i和i++
//单独使用和被使用
//单独使用
int a=5,b=5;
a++; //相当于a=a+1
++b; //相当于b=b+1
System.out.println(a); //6
System.out.println(b); //6
//被使用 ++在前先加后赋值,++在后先赋值后相加
int a=5,b=5;
int c = a++;
int d = ++b;
System.out.println(a); //6
System.out.println(b); //6
System.out.println(c); //5
System.out.println(d); //6
//先弄会这些,其他自行查找
-
if分支结构(上):
if结构:1条路
1)语法: if(boolean){ 语句块--------------基于条件执行的语句 } 2)执行过程: 判断boolean的值: 若为true,则执行语句块(整个结束) 若为false,则直接结束
//1)大于0的判断: int num = 5; //num/2是否大于0 if(num/2>0){ System.out.println("是"); }
if…else结构:2条路(必走其中之一)
//1)大于0的判断: int num = 5; //num/2是否大于0 if(num/2>0){ System.out.println("是"); }else{ System.out.println("不是"); } //没有案例后期用到就明白了
if…else if结构
1)语法: if(条件1){ 语句块1 }else if(条件2){ 语句块2 }else if(条件3){ 语句块3 }else{ 语句块4 } 2)执行过程: 判断条件1,若为true则执行语句块1(结束),若为false则 再判断条件2,若为true则执行语句块2(结束),若为false则 再判断条件3,若为true则执行语句块3(结束),若为false则执行语句块4(结束) 3)说明: 语句块1/2/3/4,必走其中之一-------------多选1
-
Scanner接收用户输入的数据
Scanner scan = new Scanner(System.in);
System.out.println("请输入成绩:");
double score = scan.nextDouble();
System.out.println("输入成绩是:"+score);
-
switch…case结构(break:跳出switch)
-
while循环(continue:跳过循环体中剩余语句而进入下一次循环)
while(boolean){ 语句块/循环体------------反复执行的代码 }
do - while循环
-
break:跳出循环
continue:跳过循环体中剩余语句而进入下一次循环
Math.random() //0到1随机生成一个数 -
数组
//声明整型数组arr,包含10个元素,每个元素都是int类型,默认值为0 int[] arr = new int[10]; //其他写法,直接存数 int[] arr = {2,5,8}; //2,5,8 int[] arr = new int[]{2,5,8}; //2,5,8
-
方法
形参:形式参数,定义方法时的参数叫做形参
实参:实际参数,调用方法时的参数叫做实参修饰词 返回值类型 方法名(参数列表) { 方法体 }
//无参无返回值 public static void say(){ System.out.println("大家好,我叫明明,今年19岁了"); } //有参无返回值 public static void sayHi(String name){ //------------------形参 System.out.println("大家好,我叫"+name+",今年19岁了"); } //有参无返回值 public static void sayHello(String name,int age){ //------------------形参 if(age>=50){ //在某种特定条件下,提前结束方法 return; //结束方法的执行 } System.out.println("大家好,我叫"+name+",今年"+age+"岁了"); } //无参有返回值 public static double getNum(){ //若方法有返回值,则在方法中必须出现return关键字 //并且return后跟的数的类型还必须与返回值类型匹配 //return "abc"; //编译错误,返回值类型必须匹配 return 8.88; //1)结束方法的执行 2)返回结果给调用方 } //有参有返回值 public static int plus(int num1,int num2){ //------------------形参 int num = num1+num2; return num; //返回的是num里面的数 //return num1+num2; //返回的是num1与num2的和 } //无参有返回值 public static int[] testArray(){ int[] arr = new int[10]; for(int i=0;i<arr.length;i++){ arr[i] = (int)(Math.random()*100); } return arr; }