Java 小知识
String 初始化
String str = "123";
String strTwo = new String("123");
String strThree = new String();
strThree = "123";
1、对比2个值得时候,不能用==对比,必须用
String strExample = new String("123");
if("123".equals(strExample)) {
System.out.println("是");
}
2、字符串转换为字符数组
char[] charArray = strExample.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]+" ");
}
3、以某某后缀结束的判断方法 类似的startWith
String strs = "ABCDEabcde123";
if(strs.endsWith("123")) {
System.out.println("是");
}
4、以某某后缀结束的判断方法 类似的toUpperCase
String strcase = "ABCDEABCDE";
System.out.println(strcase.toLowerCase());
实例:成绩菜单系统
package shuzu;
import java.util.Scanner;
public class 成绩菜单 {
public static void main(String[] args) {
String[]Name=new String[2];
int[]Chinese=new int[2];
int index =xuan();
ze(index,Name,Chinese);
}
public static int xuan(){
System.out.print("1:新增");
System.out.print("2:修改");
System.out.print("3:删除");
System.out.print("4:查询");
System.out.print("5:统计");
System.out.println("\n→→→→→→→选择功能→→→→→→→");
Scanner input = new Scanner(System.in);
return input.nextInt();
}
public static void chaxun(String name[],int []score) {
System.out.println("1:查询所有学生分数");
System.out.println("2:通过学生姓名查询相应的分数");
Scanner input = new Scanner(System.in);
System.out.println("选择菜单:");
int m = input.nextInt();
switch (m) {
case 1:
queryAll(name,score);
ze(xuan(),name,score);
break;
case 2:
querySingle(name,score);
ze(xuan(),name,score);
break;
default:
System.out.println("没有这个功能!");
return;
}
}
public static void ze(int index,String Name[],int []Chinese){
switch(index){
case 1:
xingzeng(Name, Chinese);
ze(xuan(),Name, Chinese);
break;
case 2:
xiugai(Name,Chinese);
ze(xuan(),Name, Chinese);
break;
case 3:
shanchu(Name,Chinese);
ze(xuan(),Name, Chinese);
break;
case 4:
chaxun(Name,Chinese);
ze(xuan(),Name, Chinese);
break;
case 5:
tongji(Name,Chinese);
ze(xuan(),Name, Chinese);
break;
default:
System.out.println("没有这个功能:");
return;}
}
public static void xingzeng(String Name[],int[]Chinese){
for(int i=0;i<Name.length;i++){
Scanner input = new Scanner(System.in);
System.out.println("输入姓名");
String StudentName = input.nextLine();
Name[i] =StudentName ;
System.out.println("输入成绩");
int chinese = input.nextInt();
Chinese[i] = chinese;
}
for(int i=0;i<Name.length;i++){
System.out.println("姓名:"+Name[i]+" 成绩:"+Chinese[i]);
}
return;
}
public static void xiugai(String Name[],int[]Chinese){
Scanner input = new Scanner(System.in);
System.out.println("输入姓名");
String StudentName = input.nextLine();
System.out.println("输入分数");
int fenshu = input.nextInt();
for(int i=0;i<Name.length;i++){
if(Name[i].equals(StudentName)){
Chinese[i]=fenshu;
}
}
for(int i=0;i<Chinese.length;i++){
System.out.println("姓名:"+Name[i]+" 成绩:"+Chinese[i]);
}
return;
}
public static void shanchu(String Name[],int[]Chinese){
Scanner input = new Scanner(System.in);
System.out.println("输入姓名");
String StudentName = input.nextLine();
for(int i=0;i<Name.length;i++){
if(Name[i].equals(StudentName)){
Chinese[i]=0;
Name[i]=" ";
}
}
for(int i=0;i<Chinese.length;i++){
System.out.println("姓名:"+Name[i]+" 成绩:"+Chinese[i]);
}
return;
}
public static void queryAll(String Name[],int []Chinese){
Scanner input = new Scanner(System.in);
System.out.println("输入姓名");
String StudentName = input.nextLine();
for(int i=0;i<Name.length;i++){
if(Name[i].equals(StudentName)){
System.out.println("姓名:"+Name[i]+" 成绩:"+Chinese[i]);
}
}
return;
}
public static void querySingle(String Name[],int []Chinese){
for(int i=0;i<Chinese.length;i++){
System.out.println("姓名:"+Name[i]+" 成绩:"+Chinese[i]);
}
return;
}
public static void tongji(String Name[],int []Chinese){
int sum =0;
for(int i=0;i<Chinese.length;i++){
sum+=Chinese[i];
}
System.out.println("所有学生平均成绩:"+sum/Chinese.length);
return;
}
}