Java-基础知识

1.数据类型

数据类型说 明
char (字符型)用于存储单个字符 如:性别‘男’、‘女’,电灯‘开’、‘关’
int (整型)用于存储整数 如:一天的时间是24小时,一月份有31天
double (双精度)用于存储小数 如:蒙牛早餐奶的价格1.3元,手机待机时间6.5小时
String (字符串)用于存储一串字符 如:“我的爱好是踢足球”,“我喜欢Java程序”

2.变量

在这里插入图片描述

  • 通过变量名可以简单快速地找到它存储的数据。

在这里插入图片描述

3.步骤

第一步:声明变量,即“根据数据类型在内存申请空间”

 int money;   //数据类型  变量名;

第二步:赋值,即“将数据存储至对应的内存空间”

 money = 1000//变量名=数值;         

第一步和第二步可以合并:

int money = 1000//数据类型   变量名=数值;

第三步:使用变量,即“取出数据使用 ”

4.变量命名规则

Java语言中,变量命名要符合一定规则。简短且能清楚地表明变量的作用,通常第一个单词的首字母小写,其后单词的首字母大写。例如:myScore。

                                                     任意多的:                  
              1.字母                        1.数字
变量名=       2.下划线'_'    +              2.字母
              3.'$'符号                     3.下划线‘_’
                                            4.'$'符号

不同类型的变量存取:

public static void main(String[ ] args) {
      double score = 98.5;    
      String name = "张三";
      char sex = '男';
      System.out.println("本次考试成绩最高分: " + score);
	 System.out.println("最高分得主: " + name);
	 System.out.println("性别: " + sex);
}

5.运算符

算术运算符:

+ - * /

例:

import java.util.Scanner;
public class ScoreStat {

	/*
	 * 成绩统计
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("STB的成绩是:");
		int stb = input.nextInt();     //STB分数
		System.out.print("Java的成绩是:");
		int java = input.nextInt();    //Java分数
		System.out.print("SQL的成绩是:");
		int sql = input.nextInt();     //SQL分数
		int diffen;       //分数差
		double avg;       //平均分
		System.out.println("-----------------------");
		System.out.println("STB\tJava\tSQL");
		System.out.println(stb + "\t" + java + "\t" + sql);
		System.out.println("-----------------------");
		diffen = java - sql; 			//计算Java课和SQL课的成绩差
		System.out.println("Java和SQL的成绩差:" + diffen);
		avg = (stb + java + sql) / 3;	//计算平均分
		System.out.println("3门课的平均分是: " + avg);
	}
}

关系运算符:
常用的关系运算符有哪些?

><
==!=
>=<=

关系运算符的比较结果使用boolean类型。boolean类型一共有两个值:
 真:true
 假:false
例如,控制台输入张三的成绩,与李四的成绩进行比较,输出比较结果:

import java.util.Scanner;
public class BoolTest {
	public static void main(String[] args) {
		int liSi = 80;     //学员李四成绩
  	  	boolean isBig ;    //声明一个boolean类型的变量
  	  	Scanner input = new Scanner(System.in); 	//Java输入的一种方法
  	  	System.out.print("输入学员张三成绩: "); 	//提示要输入学员张三的成绩
  	  	int zhangSan =  input.nextInt();  		//输入张三的成绩 
  	  	isBig = zhangSan > liSi ;    //将比较结果保存在boolean变量中
  	  	System.out.println( "张三成绩比李四高吗 ? "+isBig );//输出比较结果
	}
}

6.控制语句

选择控制语句:

  • if else
    基本的if选择结构:
基本的if选择结构:
  • if…else if…else 结构
    语法:
if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}

实例代码:

public class Test {
   public static void main(String args[]){
      int x = 30;
 
      if( x == 10 ){
         System.out.print("Value of X is 10");
      }else if( x == 20 ){
         System.out.print("Value of X is 20");
      }else if( x == 30 ){
         System.out.print("Value of X is 30");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}

  • 嵌套if
    使用嵌套的 if…else 语句是合法的。也就是说你可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。
    语法:
if(布尔表达式 1){
   如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
      如果布尔表达式 2的值为true执行代码
   }
}

实例:

public class Test {
 
   public static void main(String args[]){
      int x = 30;
      int y = 10;
 
      if( x == 30 ){
         if( y == 10 ){
             System.out.print("X = 30 and Y = 10");
          }
       }
    }
}

  • switch case
    switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
    switch case 语句语法格式如下:
switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。
示例如下:

public class Test {
   public static void main(String args[]){
      //char grade = args[0].charAt(0);
      char grade = 'C';
 
      switch(grade)
      {
         case 'A' :
            System.out.println("优秀"); 
            break;
         case 'B' :
         case 'C' :
            System.out.println("良好");
            break;
         case 'D' :
            System.out.println("及格");
            break;
         case 'F' :
            System.out.println("你需要再努力努力");
            break;
         default :
            System.out.println("未知等级");
      }
      System.out.println("你的等级是 " + grade);
   }
}

如果 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

循环控制语句:

  • while
    while是最基本的循环,它的结构为:
while( 布尔表达式 ) {
  //循环内容
}

特点:先判断,再执行。
示例如下:

public class Test {
   public static void main(String args[]) {
      int x = 10;
      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

以上实例编译运行结果如下:
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

  • 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

  • for
    Java 提供了另一种语句 —— for 循环,使一些循环结构变得更加简单。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("好好学习!");
}

  • break和continue
  • break

为什么要使用break?
假如在一次长跑比赛中,当运动员跑到第8圈的时候坚持不下去了,想退出比赛,可以使用break。

for (int i = 0; i<10; i++) {
  //跑4000米;
if ( 不能坚持 ) {
         break;     //退出比赛
  }
}

break用于do-while、while、for中时,可跳出循环而执行循环后面的语句。
在这里插入图片描述
例如以下代码:

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 + "%");
	}
}

数组

声明一个变量就是在内存空间划出一块合适的空间。
声明一个数组就是在内存空间划出一串连续的空间。

数组的基本要素:
 标识符:数组的名称,用于区分不同的数组
 数组元素:向数组中存放的数据
 元素下标:对数组元素进行编号,从0开始,数组中的每个元素都可以通过下标来访问
 元素类型:数组元素的数据类型

在这里插入图片描述
使用步骤:
(1) 声明数组:告诉计算机数据类型是什么

int[ ] score1;             //Java成绩
int score2[ ];             //C#成绩
String[ ] name;        //学生姓名

语法:

数据类型    数组名[ ] ;
数据类型[ ]  数组名 ;

(2) 分配空间:告诉计算机分配几个连续的空间

score = new int[30]; 
avgAge = new int[6];     
name = new String[30];

语法:

数据类型[ ]  数组名=new 数据类型[大小];

(3) 赋值:向分配的空间里放数据

score[0] = 89;
score[1] = 79;
score[2] = 76;
……

在这里插入图片描述
此外也可以边声明边赋值:

int[ ] score = {89, 79, 76};

int[ ] score = new int[ ]{89, 79, 76};

(4) 处理数组
取数组中的元素,求平均分:

int [ ] score = {60, 80, 90, 70, 85};
double avg;
avg = (score[0] + score[1] + score[2] + score[3] + score[4])/5;  

结合循环:
数组的元素类型和数组的大小都是确定的,所以当处理数组元素时候,我们通常使用基本循环或者 For-Each 循环。

public class TestArray {
   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};
 
      // 打印所有数组元素
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }
      // 计算所有元素的总和
      double total = 0;
      for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
           System.out.println("Total is " + total);
      // 查找最大元素
            double max = myList[0];
      for (int i = 1; i < myList.length; i++) {
         if (myList[i] > max) max = myList[i];
      }
      System.out.println("Max is " + max);
   }
}

以上实例编译运行结果如下:
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值