实验二、数组
一、实验目的:
1、学会使用一维与二维数组管理简单数据。
2、学会编写简单的菜单驱动(命令行式)的Java程序
二、实验环境:
BLUEJ
三、实验内容:
(写出主要的内容)
1.定义一个int型的一维数组,数组的长度由键盘输入,为数组中的元素随机赋值。依次完成如下功能:
(1) 输出数组中的元素。每行输出最多十个数字,数字之间用Tab键分隔;
(2) 计算数组中元素之和,并输出;
(3) 求出数组中元素的最大值及其位置(若有多个相同的最大值,仅输出第一个),并输出。
代码:
import java.util.Scanner;
import java.util.Random;
public class Experiment
{
static void Print(int array[])
{
int i;
for(i=0;i<array.length;i++)
{
System.out.print(String.format("%3d",array[i])+" ");
if((i+1)%10==0)
System.out.println();
}
System.out.println();
}
static int Sum(int array[])
{
int sum=0;
for(int i=0;i<array.length;i++)
sum=sum+array[i];
return sum;
}
static void FindMax(int array[])
{
int max=array[0],max_situation=0;
for(int i=1;i<array.length;i++)
{
if(max<array[i])
{
max=array[i];
max_situation=i;
}
}
System.out.println("数组中元素的最大值为:"+max+",所处位置为array["+max_situation+"]。");
}
public static void main(String[] args)
{
int n,i;
System.out.print("请输入数组长度:");
Scanner scanner=new Scanner(System.in);
n=scanner.nextInt();
int array[]=new int[n];
Random rand=new Random();
for(i=0;i<n;i++)
{
array[i]=rand.nextInt(100);
}
Print(array,n);
System.out.println("数组中元素之和为:"+Sum(array));
FindMax(array);
}
}
2.定义一个二维整形数组data[5][6],数组中的元素在区间[0, 100)上随机赋值。找出数组中所有的具有这类性质的元素及其位置:该元素在所在行是最大的,但在其所在列是最小的。如果没有这样的元素,则输出“没有这样的元素”。
代码:
import java.util.Scanner;
import java.util.Random;
public class Experiment
{
public static void main(String[] args)
{
int array[][]= new int[5][6];
int i,j;
Random r=new Random();//拟随机数
for(i=0;i<5;i++)
{
for(j=0;j<6;j++)
{
array[i][j]=r.nextInt(100);
}
}
for(i=0;i<5;i++)//输出这个数组
{
for(j=0;j<6;j++)
{
System.out.print(String.format("%2d",array[i][j])+" ");
if(j==5)
System.out.println();
}
}
judge(array);//调用判断函数
}
public static void judge(int array[][])
{
int i=0,j=0,flagi=0,flagj=0;
boolean tryi,tryj,result=false;
for(i=0;i<5;i++)//遍历每一行
{
for(j=0;j<6;j++)//遍历每一行上的每一个元素
{
tryi=true;tryj=true;
for(flagi=0;flagi<5;flagi++)
{
if(array[flagi][j]<array[i][j])//如果本列存在元素比这个数字小,判断条件置false
{
tryi=false;
break;
}
}
for(flagj=0;flagj<6;flagj++)
{
if(array[i][flagj]>array[i][j])//如果本行存在元素比这个数字大,判断条件置false
{
tryj=false;
break;
}
}
if(tryi&&tryj)//如果都为true,则存在
{
System.out.println("存在一个这样的元素:"+array[i][j]+",位于["+i+"]["+j+"]。");
result=true;
}
}
}
if(!result)
{
System.out.println("没有这样的元素。");
}
}
}
- 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
代码:
import java.util.Scanner;
import java.util.Random;
public class Experiment
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
int choice=1;
while(choice!=3)
{
System.out.print("Please input your choice:\n1.C TO F\n2.F TO C\n3.EXIT THIS PROGRESS\n");
choice=scanner.nextInt();
if(choice==1)
{
double C,F;
System.out.print("请输入一个摄氏温度 C=");
C=scanner.nextDouble();
F=9*C/5+32;
System.out.println(C+"°C="+F+"°F");
System.out.println();
}
else if(choice==2)
{
double C,F;
System.out.print("请输入一个华氏温度F=");
F=scanner.nextDouble();
C=5*(F-32)/9;
System.out.println(F+"°F="+C+"°C");
System.out.println();
}
}
System.exit(0);
}
}
4.Adapt the above program so that the user is not allowed to enter a temperature below absolute zero: this is -273.15C, or -459.67F. (修改以上程序,不允许用户输入的温度低于绝对零度,即-273.15C或-459.67F)
代码:
import java.util.Scanner;
import java.util.Random;
public class Experiment
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
int choice=1;
while(choice!=3)
{
System.out.print("Please input your choice:\n1.C TO F\n2.F TO C\n3.EXIT THIS PROGRESS\n");
choice=scanner.nextInt();
if(choice==1)
{
double C,F;
System.out.print("请输入一个摄氏温度 C=");
C=scanner.nextDouble();
if(C<(-273.15))
continue;
F=9*C/5+32;
System.out.println(C+"°C="+F+"°F");
System.out.println();
}
else if(choice==2)
{
double C,F;
System.out.print("请输入一个华氏温度F=");
F=scanner.nextDouble();
if(F<(-459.67))
continue;
C=5*(F-32)/9;
System.out.println(F+"°F="+C+"°C");
System.out.println();
}
}
System.exit(0);
}
}
5、(选做)超级递增序列指的是一个整数序列,这个序列中的每一个整数都要比它前面所有整数的和大。编写一个程序,读入一组整数,然后判断这组整数是否为超级递增序列。
输入格式为:数组长度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”不是超级递增序列。
代码:
import java.util.Scanner;
import java.util.Random;
public class Experiment
{
public static void main(String[] args)
{
int n=1,i,j,temp;
System.out.println("");
Scanner scanner=new Scanner(System.in);
while(n!=0)
{
n=scanner.nextInt();
int array[]=new int[n];
for(i=0;i<n;i++)
array[i]=scanner.nextInt();
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
int sum=array[0];
boolean flag=true;
for(i=1;i<n;i++)
{
if(array[i]>=sum)
{
sum=sum+array[i];
continue;
}
else
{
flag=false;
break;
}
}
for(i=0;i<n;i++)
System.out.print(array[i]+" ");
if(flag)
System.out.println("是超级递增序列");
else
System.out.println("不是超级递增序列");
}
}
}
- (选做)编写一个程序,从键盘读入一个句子(句子中可能包含空格、大小写字母、数字、标点符号等),试统计该句子中字符(不区分大小写)、数字、空格及其它字符的个数。
代码:
import java.util.Scanner;
public class Experiment
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.print("请输入要统计的句子:");
String str=scanner.nextLine();
char charray[]=str.toCharArray();
count(charray);
scanner.close();
}
public static void count(char str[])
{
int lowercase=0,uppercase=0,number=0,blank=0,punctuation=0,other=0;
for(int i=0;i<str.length;i++)
{
if(str[i]>='a'&&str[i]<='z')
lowercase++;
else if(str[i]>='A'&&str[i]<='Z')
uppercase++;
else if(str[i]>='0'&&str[i]<='9')
number++;
else if(str[i]==' ')
blank++;
else if(str[i]==','||str[i]=='.'||str[i]=='?'||str[i]=='!')
punctuation++;
else
other++;
}
System.out.println("该句子中包含:");
System.out.println("小写字母:"+lowercase+"个");
System.out.println("大写字母:"+uppercase+"个");
System.out.println("数字:"+number+"个");
System.out.println("空格:"+blank+"个");
System.out.println("标点符号:"+punctuation+"个");
System.out.println("其他:"+other+"个");
}
}
四、心得体会:
通过本次实验,我掌握了数组的使用,也进一步理解了数组的定义,JAVA与C语言不同的是,JAVA可以指定数组的大小。C、C++不能通过输入n后再定义数组:int n;cin>>n;int array[n]。这样子就是错误的,但是JAVA可以:int n;Scanner scanner=new Scanner(System.in);n=scanner.nextInt();int array[]=new int[n];。所以其实JAVA相较于C语言来说更为简便一些,这样不再需要固定数组的长度或大小,可以通过输入随意调节数组的大小。我进一步理解了数组。