数组与字符串 编程练习

1.要求:

从键盘输入n个数,求这n个数中的最大和最小值,并输出
 程序中采用的输出语句形式为:

                    System.out.println("max number is:"+max);

		System.out.print("min number is:"+min);

2.测试样例:

输入:
 8 2 3 4 5 6 7 8 9 

输出:
max number is:9

min number is:2

//从键盘输入n个数,求这n个数中的最大和最小值,并输出;
import java.util.*;
public class Exercise5_2{
public static void main(String[] args){
int n,max,min;
Scanner reader = new Scanner(System.in);
//System.out.print(“please input the number:”);
n = reader.nextInt();
int[] a = new int[n];
if (n>0){
for(int i=0;i<n;i++){
//System.out.print("please input "+(i+1)+ “th number:”);
a[i]=reader.nextInt();
}
max = a[0];
min = a[0];

		for(int i=0; i<n; i++){
			if(a[i]>max)
				max=a[i];
			if(a[i]<min)
				min=a[i];				
		}
		System.out.println("max number is:"+max);
		System.out.print("min number is:"+min);
	}
	else	
		System.out.print("no such number!");			
}	

}

1.要求:

从键盘输入n个数,输出这些数中大于其平均值的数。
 程序中采用的输出语句形式为:

                    System.out.println("the average is:"+average);

                    System.out.print("the number greater than average is:");

		for(int i=0; i<n; i++){

			if(a[i]>average){

				System.out.print(" "+a[i]);

			}

		}

2.测试样例:

输入:
 8 2 3 4 5 6 7 8 9 

输出:
the average is:5.5

the number greater than average is: 6 7 8 9

//从键盘输入n个数,输出这些数中大于其平均值的数
//filename:EXERCISE5_1
import java.util.*;
public class EXERCISE5_1
{
public static void main(String[] args)
{
int n;
double sum=0,average;
Scanner reader = new Scanner(System.in);
//System.out.print(“please input the number:”);
n = reader.nextInt();
int[] a = new int[n];
if (n>0)
{
for(int i=0;i<n;i++)
{
//System.out.print("please input “+(i+1)+ “th number:”);
a[i]=reader.nextInt();
sum = sum +a[i];
}
average = sum/n;
System.out.println(“the average is:”+average);
System.out.print(“the number greater than average is:”);
for(int i=0; i<n; i++)
{
if(a[i]>average)
{
System.out.print(” "+a[i]);
}
}
}
else
{
//System.out.print(“no such number!”);
}

}

}

1.要求:

从键盘输入一个字符串和一个字符,从该字符串中删除给定的字符

程序中采用的输出语句形式为:

  System.out.println("original String is :"+ str);
  System.out.println("erase char is :"+ sChar);
  System.out.println("new string is :"+ newStr);

2.测试样例:

输入:

sdfgrfhfshj
f

输出:

original String is :sdfgrfhfshj
erase char is :f
new string is :sdgrhshj

//从键盘输入一个字符串和一个字符,从该字符串中删除给定的字符
import java.util.;
import java.io.
;
public class Exercise5_9 {
public static void main(String[] args) throws Exception{
String str,temp,newStr="";
char sChar,cChar;
int n=8;
Scanner buf = new Scanner(System.in);
//System.out.print(“please input string:”);

	str = buf.nextLine();
	
	//System.out.print("please input erase char:");
	temp = buf.nextLine();
	sChar = temp.charAt(0);
	
	n = str.length();
	for ( int i=0;i<n;i++){
		cChar = str.charAt(i);
		if (cChar!=sChar)
			newStr = newStr + cChar;
	}
	System.out.println("original String is :"+ str);
	System.out.println("erase char is :"+ sChar);
	System.out.println("new string is :"+ newStr);
}	

}

1.要求:

从键盘输入一个字符串和子串开始的位置与长度,截取该字符串的子串并输出

程序中采用的输出语句形式为:

  System.out.println("the input string is:"+str);
  System.out.println("the sub string is:"+substr);

2.测试样例:

输入:
abcdefghijklmn

5 5 

输出:

 the input string is:abcdefghijklmn
 the sub string is:efghi

//从键盘输入一个字符串和子串开始的位置与长度,截取该字符串的子串并输出
import java.util.*;
public class Exercise5_8 {
public static void main(String[] args){
String str,substr="";
int begin,len;
Scanner buf = new Scanner(System.in);
//System.out.print(“please input string:”);
str = buf.nextLine();
//System.out.print(“please input substring location:”);
begin = buf.nextInt();
//System.out.print(“please input substring length:”);
len = buf.nextInt();
if((begin-1+len)<=str.length()){
substr = str.substring(begin-1,begin+len-1);
System.out.println(“the input string is:”+str);
System.out.println(“the sub string is:”+substr);
}
else
System.out.println(“the string has less length!”);
}
}

1.要求:

求一个三阶方阵的对角线上各个元素之和

程序中采用的输出语句形式为:

  System.out.print("sum of the diagonal nums is:" + sum);

2.测试样例:

输入:
 8 2 3 4 5 6 7 8 9 

输出:

 sum of the diagonal nums is:22

//求一个三阶方阵的对角线上各个元素之和
import java.util.*;
public class Exercise5_3 {
public static void main(String[] args){
Scanner buf = new Scanner(System.in);
int[][] nums = new int[3][3];
int i,j,sum=0;
//System.out.println(“please input 3X3 matrix:”);
for(i=0;i<3;i++)
for(j=0;j<3;j++){
nums[i][j] = buf.nextInt();
}
for(i=0;i<3;i++){
sum = sum+nums[i][i];
}
System.out.print(“sum of the diagonal nums is:” + sum);
}
}

1.要求:

编程统计用户从键盘输入的字符串中所包含的字母,数字和其他字符的个数;

程序中采用的输出语句形式为:

  System.out.println("the string has the number of characterwww is:"+ nChar);
  System.out.println("the string has the number of integer is:"+ nDig);
  System.out.println("the string has the number of other character is:"+ nOther);

2.测试样例:

输入:    

abcdefghijklmn1234####


输出:    

the string has the number of characterwww is:14
the string has the number of integer is:4
the string has the number of other character is:4

//编程统计用户从键盘输入的字符串中所包含的字母,数字和其他字符的个数;
import java.util.*;
public class Exercise5_10 {
public static void main(String[] args) throws Exception{
String str;
char c=’ ';
int nDig=0,nChar=0,nOther=0;
Scanner reader = new Scanner(System.in);
//System.out.print(“please input string:”);
str = reader.nextLine();

	for ( int i=0;i<str.length();i++){
		c = str.charAt(i);
		if(c>='a'&&c<='z'){
			nChar++;
		}
		else if(c>='0'&&c<='9'){
			nDig++;
		}
		else
			nOther++;			
	}
	System.out.println("the string has the number of characterwww is:"+ nChar);
	System.out.println("the string has the number of integer is:"+ nDig);
	System.out.println("the string has the number of other character is:"+ nOther);
}	

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值