一、实验目的:
1、学会使用一维与二维数组管理简单数据。
2、学会编写简单的菜单驱动(命令行式)的Java程序
二、实验环境:
Window10 Eclipsec
三、实验内容:
1.定义一个int型的一维数组,数组的长度由键盘输入,为数组中的元素随机赋值。依次完成如下功能:
(1) 输出数组中的元素。每行输出最多十个数字,数字之间用Tab键分隔;
package code2;
import java.util.Scanner;
public class one {
public static void main(String[] args) {
// TODO Auto-generated method stubint size;
System.out.println("输入数组长度:");
Scanner scanner=new Scanner(System.in);
size=scanner.nextInt();
int a[]=new int[size];
for(int i=0;i<size;i++){
a[i]=(int)(Math.random()*size);
if(i%10==0)
System.out.println();
System.out.print(a[i]+" ");
}
}
}
(2) 计算数组中元素之和,并输出;
package code2;
import java.util.Scanner;
public class two {
public static void main(String[] args) {
// TODO Auto-generated method stub
int size,sum=0;
System.out.println("输入数组长度:");
Scanner scanner=new Scanner(System.in);
size=scanner.nextInt();
int a[]=new int[size];
for(int i=0;i<size;i++){
a[i]=(int)(Math.random()*size);
if(i%10==0)
System.out.println();
System.out.print(a[i]+" ");
sum=sum+a[i];
}
System.out.println();
System.out.println("数组元素和:"+sum);
}
}
(3) 求出数组中元素的最大值及其位置(若有多个相同的最大值,仅输出第一个),并输出。
package code2;
import java.util.Scanner;
public class three {
public static void main(String[] args) {
// TODO Auto-generated method stub
int size;
int max=0,n=0;
System.out.println("输入数组长度:");
Scanner scanner=new Scanner(System.in);
size=scanner.nextInt();
int a[]=new int[size];
System.out.println("数组元素为:");
for(int i=0;i<size;i++){
a[i]=(int)(Math.random()*size);
if(i%10==0)
System.out.println();
System.out.print(a[i]+" ");
}
System.out.println();
max=a[0];
for(int j=1;j<size;j++){
if(a[j]>max){
max=a[j];
n=j;
}
}
System.out.println("a["+n+"] has maximum value "+a[n]);
}
}
2.定义一个二维整形数组data[5][6],数组中的元素在区间[0, 100)上随机赋值。找出数组中所有的具有这类性质的元素及其位置:该元素在所在行是最大的,但在其所在列也是最大的。如果没有这样的元素,则输出“没有这样的元素”。
package code2;
public class max {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] data = new int[5][6];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
data[i][j]= (int)(Math.random()*100)%101;
}
}
System.out.println("随机产生的数组为:");
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
System.out.print(data[i][j] + "\t");
}
System.out.println();
}
find(data);
}
public static void find(int array[][]){
boolean flag = true;
for (int i = 0; i < array.length; i++) {
int max = array[i][0];
int max_j = 0;
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]>max){
max = array[i][j];
max_j = j;
}
else continue;
}
for ( int k = 0; k < array.length; k++) {
if(max <array[k][max_j])
{
flag = false;
break;
}else
if(k==array.length-1){
flag = true; }
else continue;
}
if(flag){
System.out.println("位置第" + (i+1) + "行," + (max_j+1) + "列的"+array[i][max_j]);
}
else{
System.out.println("第"+(i+1) + "行"+"没有这样的元素");
}
}
}
}
3.Write a menu-driven program that provides three options (编写一个菜单驱动程序,提供如下三个选项):
a) the first option allows the user to enter a temperature in Celsius and displays the corresponding Fahrenheit temperature (第一个选项允许用户输入一个摄氏温度,并输出其相应的华氏温度);
b) the second option allows the user to enter a temperature in Fahrenheit and displays the corresponding Celsius temperature (第二个选项允许用户输入一个华氏温度,并输出其相应的摄氏温度);
c) the third option allows the user to quit (第三个选项允许用户关闭程序).The formulate that you need are as follows, where C represents a Celsius temperature and F a Fahrenheit temperature: (以下是你需要的公式,其中C代表摄氏温度,F代表华氏温度)
F = 9C/5 + 32
C = 5(F – 32)/9
package code2;
import java.util.Scanner;
public class menu {
public static void main(String[] args) {
// TODO Auto-generated method stubint p;
double C,F;
System.out.println("1、允许用户输入一个摄氏温度,并输出其相应的华氏温度");
System.out.println("2、允许用户输入一个华氏温度,并输出其相应的摄氏温度");
System.out.println("3、允许用户关闭程序");
for(;;){
System.out.println("请输入你的选项:");
Scanner scanner=new Scanner(System.in);
p=scanner.nextInt();
if(p==3){
System.out.println("关闭程序!");
break;}
else if(p==1){
System.out.println("请输入一个摄氏温度:");
C=scanner.nextDouble();
F=9*C/5+32;
System.out.println("相应的华氏温度:"+F);
}else if(p==2)
{
System.out.println("请输入一华氏温度:");
F=scanner.nextDouble();
C = 5*(F-32)/9;
System.out.println("相应的摄氏温度:"+C);
}}} }
4.超级递增序列指的是一个整数序列,这个序列中的每一个整数都要比它前面所有整数的和大。编写一个程序,读入一组整数,然后判断这组整数是否为超级递增序列。输入格式为:数组长度n 数1 数2 数3 … 数n输出格式为:“数1 数2 数3 … 数n”是(或不是)超级递增序列。示例:当输入为5 1 2 4 9 20时,输出应为“1 2 4 9 20”是超级递增序列;当输入为6 1 4 9 14 25 65时,输出应为“1 4 9 14 25 65”不是超级递增序列。
package code2;
import java.util.Scanner;
import java.util.Arrays;
public class superlist {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入数组长度和初始化数组:");
Scanner scanner= new Scanner(System.in);
int n=scanner.nextInt();
int[] N= new int[n];
for (int i=0; i<N.length;i++) {
N[i]=scanner.nextInt();
}
System.out.println();
out(N,n); }
public static void out(int N[],int n){
Arrays.sort(N);
int[] N2= new int[n];
System.arraycopy(N, 0, N2, 0, n);
boolean flag = true;
for (int i = 1; i < N2.length-1; i++) {
if(N2.length<=2) {
flag = true;
}else {
N2[i]+=N2[i-1];
if(N2[i]>N2[i+1])
{
flag=false;
break;
}else {
flag=true;
}
}
}
if(flag)
{
System.out.print("“");
for (int i = 0; i < N.length; i++) {
if(i==N.length-1){
System.out.print( N[i]);
}else{
System.out.print( N[i]+" ");
}
}
System.out.println("“是超级递增序列。");
}else {
System.out.print("”");
for (int i = 0; i < N.length; i++)
{
if(i==N.length-1){
System.out.print( N[i]);
}else{
System.out.print( N[i]+" ");
}
}
System.out.println("”不是超级递增序列。");
}
}
}
5.(选做)编写一个程序,从键盘读入一个句子(句子中可能包含空格、大小写字母、数字、标点符号等),试统计该句子中字符(不区分大小写)、数字、空格及其它字符的个数。
package code2;
import java.util.Scanner;
public class count {
static int countcharacter=0;
static int countdigital=0;
static int countblank=0;
static int countother=0;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("请输入要统计的句子:");
String str = input.nextLine();
char[] ch=str.toCharArray();
count(ch);
input.close(); }
public static void count(char[] str){
for (int i = 0; i < str.length; i++) {
if(str[i]>= 'a' && str[i] <= 'z' || str[i] > 'A' && str[i] <= 'Z'){
countcharacter++;
}else if(str[i] >= '0' && str[i] <= '9'){
countdigital++;
}else if(str[i] == ' '){
countblank++;
}else{
countother++;
}
}
System.out.println("数字个数:"+countdigital);
System.out.println("英文字母个数:"+countcharacter);
System.out.println("空格个数:"+countblank);
System.out.println("其它字符个数:"+countother);
} }