Java 实例

 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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值