Java基础知识
1.数据类型
数据类型 | 定义 |
---|---|
char(字符型) | 储存单个字符如:门‘开’、‘关’ |
int(整型) | 不包含小数部分的数值型数据 |
double(双精度) | 存储小数如:1.8元 |
string(字符串) | 存储一串字符如:”我是小明“ |
2.变量
定义:一个数据的存储空间。
使用方法:
步骤 | 代码实现 |
---|---|
第一步,声明变量(根据数据类型在内存申请空间) | int a; |
第二步,赋值(将数据存储到对应的内存空间) | a=1; |
合并即 int a = 1;
3.实例
运算符求均值
public class Hello02 {
public static void main(String[] args) {
//创建对象
Scanner sc=new Scanner(System.in);
System.out.println("java的成绩是:");
int java=sc.nextInt();//获取用户控制台输入的整数
System.out.println("mysql的成绩是:");
int mysql=sc.nextInt();
System.out.println("javaweb的成绩是:");
int javaweb=sc.nextInt();
System.out.println("三门课程平均分:"+(java+mysql+javaweb)/3);
}
}
4.选择控制语句
1 . if 结构
根据条件做出判断
结构:
if(判断条件){
//代码块
}
例子1.如果考试超过90分可以得到1000元奖励
public class ooo {
public static void main(String[] args) {
Scanner score = new Scanner(System.in);
System.out.println("分数为:");
int score1 = score.nextInt();
if(score1>90) {
System.out.print("优秀,奖励1000元!");
}
}
}
2. if…else if…else 结构
例子2:
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("张三的成绩是:");
int score=sc.nextInt();
if(score>=90) {
System.out.print("非常好,奖励一个mp4!");
}else if(score>=60&&score<90) {
System.out.print("优秀,再接再励");
}else if(score>=50&&score<60) {
System.out.print("不及格,请努力!");
}else {
System.out.print("叫家长!!!");
}
}
}
3.嵌套if语句
就是在另一个if或者else if语句中使用if或者else if语句
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("张三的成绩是:");
int score=sc.nextInt();
if(score>=90) {
if(score>=95) {
System.out.print("奖励1000元!");
}
System.out.print("非常好,奖励一个mp4!");
}else if(score>=60&&score<90) {
System.out.print("优秀,再接再励");
}else if(score>=50&&score<60) {
System.out.print("不及格,请努力!");
}else {
System.out.print("叫家长!!!");
}
}
}
5.switch case
判断一个变量与一系列值中某个值是否相等,每个之值称为一个分支。
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
switch case 语句语法格式如下:
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
表达式expression为整型或字符型。
switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。
例如:根据考试成绩等级来输出信息
public static void main(String[] args) {
String score="A";
switch (score) {
case "A":
System.out.print("成绩优秀!");
break;
case "B":
System.out.print("成绩良好");
break;
case "C":
System.out.print("成绩合格");
break;
default:
System.out.print("叫家长");
break;
}
}
}
如果 case 语句块中没有 break 语句时,JVM 并不会顺序输出每一个 case 对应的返回值,而是继续匹配,匹配不成功则返回默认 case。
public class Test {
public static void main(String args[]){
int i = 5;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
}
}
运行结果显示为default
如果 case 语句块中没有 break 语句时,匹配成功后,从当前 case 开始,后续所有 case 的值都会输出。
public class Test {
public static void main(String args[]){
int i = 1;
switch(i){
case 0:
System.out.println("0");
case 1:
System.out.println("1");
case 2:
System.out.println("2");
default:
System.out.println("default");
}
}
}
以上代码编译运行结果如下:
1
2
default
6.循环控制语句
1.while
先判断在执行
只要布尔表达式为 true,循环就会一直执行下去。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=1;
while(count<=10){
System.out.println("成绩是:");
int score=sc.nextInt();
if(score>=90) {
if(score>=95) {
System.out.println("奖励1000元!");
}
System.out.println("非常好,奖励一个mp4!");
}else if(score>=60&&score<90) {
System.out.println("优秀,再接再励");
}else if(score>=50&&score<60) {
System.out.println("不及格,请努力!");
}else {
System.out.println("叫家长!!!");
}
count++;
}
}
}
2.do…while语句
do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。
do…while 循环的结构如下:
do {
//代码语句
}while(布尔表达式);
布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false。
示例如下:
public class Test {
public static void main(String args[]){
int x = 10;
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
}
}
输出结果:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
3.for 循环
使一些循环结构变得更加简单。for循环执行的次数是在执行前就确定的。
特点:先判断,再执行。
例如,输出100次“好好学习!”使用while循环结构:
int i=0;
while(i<100){
System.out.println("好好学习!");
i++;
}
使用for循环结构:
for(int i=0;i<100;i++){
System.out.println("好好学习!");
}
7 break和continue
break用于do-while、while、for中时,可跳出循环而执行循环后面的语句。
eg:
public class Test {
public static void main(String args[]) {
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ) {
// x 等于 30 时跳出循环
if( x == 30 ) {
break;
}
System.out.print( x );
System.out.print("\n");
}
}
}
以上实例编译运行结果如下:
10
20
continue
为什么要使用continue?
假如需要循环录入Java课的学生成绩,统计分数大于等于80分的学生人数,如果成绩<80则不执行人数累加,直接执行下一次循环。此时可以使用continue。
continue 适用于任何循环控制结构中。作用是跳过循环体中剩余的语句而执行下一次循环。
例如以下代码,统计80分以上学生的比例:
import java.util.Scanner;
public class ContinueDemo {
/**
* 统计80分以上学生比例
*/
public static void main(String[] args) {
int score; // 成绩
int total; // 班级总人数
int num = 0; // 成绩大于或等于80分的人数
Scanner input = new Scanner(System.in);
System.out.print("输入班级总人数: ");
total = input.nextInt(); // 输入班级总数
for (int i = 0; i < total; i++) {
System.out.print("请输入第" + (i + 1) + "位学生的成绩: ");
score = input.nextInt();
if (score < 80) {
continue;
}
num++;
}
System.out.println("80分以上的学生人数是: " + num);
double rate = (double) num / total * 100;
System.out.println("80分以上的学生所占的比例为:" + rate + "%");
}
}