1.实现对姓名的添加,查看,删除,修改功能
package 暑假2022;
import java.util.*;
public class 增删查找 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myScanner = new Scanner ( System . in );
int index =-1;
String name , newname ;
String [] arr = new String []{"tom ","jerry","ack"};
while ( true ){
System . out . println ("1.查看 2.添加 3.修改 4.删除 5.退出");
int choice = myScanner . nextInt ();
switch ( choice ){
case 1:
for ( int i =0; i <arr . length ; i ++){
System . out . println ( arr [ i ]);
}
continue ;
case 2:
System.out.println ("请输入要添加的名字");
name = myScanner . next ();
String []newarr1= new String [ arr . length +1];
for ( int i=0; i <arr . length ; i ++){
newarr1[ i ]= arr [ i ];}
newarr1[ arr . length ]= name ;
arr =newarr1;
continue ;
case 3:
System . out . println ("请输入你要修改的名字");
name = myScanner . next ();
System . out . println ("请输入名字”);");
newname = myScanner . next ();
String []newarr2= new String [ arr . length ];
for ( int i =0; i < arr . length ; i ++){
if ( arr [ i ]. equals ( name )){
index = i ;
}
}
if ( index !=-1){
arr [ index ]= newname ;
}
else {
System . out . println ("未找到!");
}
case 4:
System . out . println ("此处实现删除");
name = myScanner . next ();
String []newarr3= new String [ arr . length -1]; for ( int i =0; i <arr . length ; i ++){
if ( name . equals ( arr [ i ])){
index = i ;
}
}
if ( index !=-1){
for ( int i = 0; i <arr . length ; i ++){
if ( i <index ){
newarr3[ i ]= arr [ i ];}
else if ( i > index ){
newarr3[ i -1]= arr [ i ];
}
}
arr =newarr3;
}
continue ;
case 0:
System . out . println ("谢谢使用");
break ;
default :
System . out . println ("选择错误,请重新选择");
continue ;
}
}
}
}
2.>循环录入8个成绩,查找到最大值最小值值,以及其位置
package 暑假2022;
import java.util.*;
public class 数组 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int[] arr= new int[8];
int sum=0;
int avg=0;
int max=0;
int maxindex=0;
int minindex=0;
int min=arr[0];
System.out.println("请输入数字");
for(int i=0;i<arr.length;i++) {
arr[i]=sc.nextInt();
sum=sum+arr[i];
if(max<arr[i]) {
max=arr[i];
maxindex=i;
}
if(min>arr[i]) {
min=arr[i];
minindex=i;
}
}
avg=sum/8;
System.out.println("sum="+sum+"avg="+avg+"max="+max+"min="+min+"max="+max+"maxindex="+maxindex+"minindex"+minindex);
}
}
3.>用户输入十个汉字,分别统计 王,李,张的姓氏的个数,其他均认为是非法字符,统计非法字符的个数
package 暑假2022;
import java.util.*;
public class 作业2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String[] arr= new String[10];
String m="王";
int m1=0;
String n="李";
int n1=0;
String s="张";
int s1=0;
int h=0;
System.out.println("请输入汉字:");
for(int i=0;i<arr.length;i++) {
arr[i]=sc.next();
if(arr[i].equals(m)){
m1++;
}
if(arr[i].equals(n)){
n1++;
}
if(arr[i].equals(s)){
s1++;
}else if(!arr[i].equals(s)&&!arr[i].equals(m)&&!arr[i].equals(n)){
h++;
}
}
System.out.println("王:"+m1);
System.out.println("李:"+n1);
System.out.println("张:"+s1);
System.out.println("其他"+h);
}
}
4.二分查找
package 暑假2022;
import java.util.Scanner;
public class 二分查找 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int[] arr= new int[] {1,23,34,54,65,67,66,68};
int index=-1;
int start=0;
int end=arr.length-1;
System.out.println("请输入要找的数字");
int n=sc.nextInt();
while(start<=end) {
int middle=(end+start)/2;
int num=arr[middle];
if(num==n) {
index=middle;
break;
}
if(num>n) {
end=middle-1;
}else {
start=middle+1;
}
}
System.out.println(index);
}
}
5.
*
package 暑假2022;
import java.util.;
public class Fuhao1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.print(“请输层数”);
int layer= sc.nextInt();
for(int i=1;i<=layer;i++){
int spacenum=layer-i;
for(int j=1;j<=spacenum;j++){
System.out.print(" ");
}
int starnum=2i-1;
for(int j=1;j<=starnum;j++){
System.out.print("");
}
System.out.println();
}
for(int i=layer;i>=1;–i){
int spacenum=layer-i;
for(int j=1;j<=spacenum;j++){
System.out.print(" ");
}
int starnum=2i-1;
for(int j=1;j<=starnum;j++){
System.out.print("*");
}
System.out.println();
}
}
}