数组
int[] arr1 = { -3, 12, -5, 6, 9, 12, -8 };
// 计算其中正数的个数,负数的个数,所有正数的总和.
// 获得其中最小的成员
String[] arr2 = { "亚瑟", "安琪拉", "兰陵王", "周瑜", "诸葛亮" };
// 获得2字名的英雄,并输出.
// 3字名的英雄有多少个?
String[] arr3 = { "亚瑟-肉", "宫本武藏-刺客", "张飞-肉", "鲁班-射手", "狄仁杰-射手"
};
// 获取所有的"肉"型英雄,并输出.
// 请问"射手"型英雄有几个?
String[] arr4 = { "罗智利-20", "陈海燕-12", "刘颖聪-30", "邬艳-25", "王璐苑-14"
};
// 请问这些员工的平均年龄是多少?
// 找出其中的未成年员工
// 请问未成年员工的平均年龄是多少?
int[] arr5 = { 13, 15, 6, 9, 8, 12, 5 };
// 计算所有成员的平均值
// 计算其中>10的成员的平均值
public class day04_pre {
public static void main(String[] args) {
int[] arr1 = {-3, 12, -5, 6, 9, 12, -8};
// 计算其中正数的个数,负数的个数,所有正数的总和.
// 获得其中最小的成员
int count1 = 0, count2 = 0, sum = 0, min = arr1[0];
for (int a : arr1) {
if (a > 0) {
count1++;
sum = sum + a;
}
if (a < 0) {
count2++; // 负数
}
for (int b : arr1) {
if (b < min) {
min = b;
}
}
System.out.println("正数个数为:" + count1);
System.out.println("负数个数为:" + count2);
System.out.println("正数和:" + sum);
System.out.println("最小数是:" + min);
}
String[] arr2 = {"亚瑟", "安琪拉", "兰陵王", "周瑜", "诸葛亮"};
// 获得2字名的英雄,并输出.
// 3字名的英雄有多少个?
int nameN = 0;
for (String a : arr2) {
if (a.length() == 2) {
System.out.println("姓名两个字的有:" + a);
}
if (a.length() == 3) {
nameN++;
}
}
System.out.println("姓名带三字的人数有" + nameN);
String[] arr3 = {"亚瑟-肉", "宫本武藏-刺客", "张飞-肉", "鲁班-射手", "狄仁杰-射手"
};
// 获取所有的"肉"型英雄,并输出.
// 请问"射手"型英雄有几个?
int shot = 0;
for (String name : arr3) {
if (name.endsWith("肉")) {
System.out.println("肉型英雄:" + name);
}
if (name.endsWith("射手")) {
shot++;
}
}
System.out.println("射手英雄数为:" + shot);
String[] arr4 = {"罗智利-20", "陈海燕-12", "刘颖聪-30", "邬艳-25", "王璐苑-14"
};
// 请问这些员工的平均年龄是多少?
// 找出其中的未成年员工
// 请问未成年员工的平均年龄是多少?
double sumTot = 0; //全体求和
double sumAdu = 0; //未成年年龄求和
int aduCount = 0; //未成年人数求和
for (String emp : arr4) {
//字符串切割
String[] arr = emp.split("-");
//把字符串转换成整数
int age = Integer.parseInt(arr[1]);//integer.parseint()是将整型数据Integer转换为基本数据类型int
sumTot = sumTot + age;
if (age < 18) {
System.out.println("未成年人员:" + arr[0]);
sumAdu = sumAdu + age;
aduCount++;
}
}
System.out.println("员工的平均年龄为:" + sumTot / arr4.length);
System.out.println("未成年员工的平均年龄为:" + sumAdu / aduCount);
int[] arr5 = {13, 15, 6, 9, 8, 12, 5};
// 计算所有成员的平均值
// 计算其中>10的成员的平均值
int x = 0; //所有变量
int y = 0; //大于10变量成员
int z = 0; //大于10成员计数器
for (int a : arr5) {
x = x + a;
if (a > 10) {
y = y + a;
z++;
}
}
System.out.println("所有成员的平均值为" + x / arr5.length);
System.out.println("大于10的成员计数为:" + z);
System.out.println("大于10成员的平均值为:" + y / z);
}
}
一、案例
案例
从键盘输入两个整数,计算两个数的和
import java.util.Scanner;
public class day04_eg {
public static void main(String[] args) {
//创建扫描器
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个数:");
int x = input.nextInt();
System.out.println("请输入第二个数:");
int y = input.nextInt();
//计算两个数的和
int z = x+y;
System.out.println("两个数的和为:"+z);
}
}
知识点
Scanner输入语句
案例
int[] arr={12,15,3,9,20,8,6,10};
从键盘输入下标,输出匹配的成员
import java.util.Scanner;
public class day04_eg2 {
public static void main(String[] args) {
int[] arr={12,15,3,9,20,8,6,10};
// 从键盘输入下标 输出匹配成员 创建扫描器
Scanner input = new Scanner(System.in);
System.out.println("请输入角标");
int index = input.nextInt();
// 查询并输出匹配的成员
System.out.println(arr[index]);
}
}
知识点
训练巩固已有知识点
案例(贯穿项目相关)
String[] str={"王婧","苏小雨","唐纯莉","王璐苑","姜藤椒","姜子牙","唐骏"};
从键盘输入姓氏,查询匹配的名单.
输入关键字,模糊查询包含的人员名单
import java.util.Scanner;
public class day04_eg3Plss {
public static void main(String[] args) {
String[] str = {"王婧", "苏小雨", "唐纯莉", "王璐苑", "姜藤椒", "姜子牙", "唐骏"};
/**
* 接收用户输入的字符串,需要注意你光标位置
*/
Scanner input = new Scanner(System.in);
// 从键盘输入姓氏 查询匹配的名单
System.out.println("请输入:");
String firstName = input.next();
int count = 0;
// 遍历数组
for (String name : str) {
// 挑选
if (name.startsWith(firstName)) {
// 输出
System.out.println(name);
//累计
count++;
}
}
if (count == 0) {
System.out.println("查无此人");
}
// 输入关键字,模糊查询包含的人员名单
System.out.println("请输入:");
String key = input.next();
int count2 = 2;
//遍历数据
for (String name : str) {
// 挑选 Java String类. contains () 方法用于判断字符串中是否包含指定的字符或字符串。
if (name.contains(key)) {
//输出
System.out.println(name);
count2++;
}
}
if (count2 == 0) {
System.out.println("查无此人");
}
}
}
知识点
训练巩固已有知识点
contains()
二,难点补充
a.8种基础数据类型,归类整理,整数家族几个,小数家族几个····
/**
* 重难点补充
* 数据类型
*/
public class day04_eg3Diff {
public static void main(String[] args) {
/**
* 整数家族
* byte<short<int<long
*/
//字节型数
byte a = -128;
//短整数
short b = 9999;
//整数
int n = 999999;
//长整型,如果超出int的取值范围,需要在后面加L
long m = 9999999;
/**
* 小数家族
* float<double
*/
double c = 3.14;
float d = 3.14F;
/**
* 数值类型容量排序
* byte < short < int < float < double
*/
/**
* 字符
* char
*/
/**
* boolean
*/
/**
* 基本数据类型转换
* 自动类型转换
* 强制类型转换
*/
int k1 = 120 + 8;
byte k2 = (byte) (120+8);
System.out.println(k1);
System.out.println(k2);
}
}
b.++i和i++?
/**
* 重难点补充
* ++
* --
*/
public class day04_eg3Diff2 {
public static void main(String[] args) {
int a = 5;
System.out.println(a++);//先加后用
System.out.println(a);
System.out.println(++a);//先加后用
System.out.println(a);
int b =5;
System.out.println(b--);//先用后减
System.out.println(b);
System.out.println(--b);//先减后用
System.out.println(b);
int n = 5;
// 5 + 6 + 5
int j = n++ + n + --n;
System.out.println(j); // 存疑 16?
System.out.println(n);
}
}
c.赋值运算符
public class day04_eg3Diff3 {
public static void main(String[] args) {
/**
* 赋值运算符
* 把右边的值赋值给左边的变量
* =
*
* 复合运算符
* +=
* -=
* *=
* /=
* %=
*/
int n = 5;
n *= 3;//相当于n=n*3
System.out.println(n);
}
}
d.==和equals
/**
* 比较运算符
* == 比较基本数据类型的值是否相等
*
* equals 比较字符串的值是否相等
*/