1.写出两种声明包含12个doubke类型的值的一维数组的方式?
答:
double a[ ]=new double[12];
double[ ] a=new double[12];
2.说明如何把一维整数数组初始化为值1~5?
答:int a[ ]={1,2,3,4,5};
3.编写一个使用数组来查找10个double值的平均数的程序,10个值任选?
答:
public class luo
{
public static void main(String[] args)
{
int nums[]= {1,2,3,4,5,6,7,8,9};
int sum=0;
for(int x=0;x<nums.length;x++)
{
sum+=nums[x];
}
System.out.println("数组的平均数:"+sum/nums.length);
}
}
4.对一个字符串数组进行排序并证实其可运行性?
答:
public class luo
{
public static void main(String[] args)
{
double nums[]= {1.2,1.0,4.2,3.8,11.2,8.1,6.66,5.12};
double t;
int i;
System.out.print("原数值:");
for(i=0;i<8;i++)
{
System.out.print(nums[i]+" ");
}
for(int y=0;y<8;y++)
{
for(int b=7;b>y;b--)
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
}
System.out.println();
System.out.print("排序后:");
for( i=0;i<7;i++)
{
System.out.print(nums[i]+" ");
}
}
}
5.String类的indexOf()和lastlndexOf()方法之间的不同之处?
答:indexOf()方法找到第一次出现的指定子串;lastIndexOf()
方法找到最后一次的指定子串。
6.所有字符串都是String类型的对象,请写出如何在字符串面值“I am Java”上调用length()和charAt()方法。
答:System.out.println("I like you " .length);
显示10。charAt()方法类似。
7.扩展Encode加密类,修改它以使其使用包含8个字符的字符串作为密钥?
答:大家在评论区写答案,一起探讨啊。
8.位运算符可以应用于double类型吗?
答:不能。
9.请用?运算符来重写下面代码?
if(x<0)y=10;
else y=20;
答:x<0?10:20;
10.在下面的代码段中,&使位运算符还是逻辑运算符呢?请给出你的理由?
boolean a,b;
//........
if(a&b).......
答:是逻辑运算符,应为操作数是boolean类型。
11.溢出数组边界是错误吗?用负值编制数组索引正确吗?
答:都是错误的,所有数组都是从0开始的。
12.什么是无符号右移运算符?
答:>>>.
13.重写本章前面的MinMax类,以便使用for-each形式的for循环?
答:
14.用Bubble类中执行排序的for循环能够转换为for-each形式的for循环码?如果不能为什么呢?
答:不可以。进行排序的Bubble类中的for不能转换成for-each循环。对外层循环,循环计数器的当前值会在内层循环中用到。而对于内层循环,顺序不正确的值需要被交换,这意味着需要使用赋值操作。使用for-each循环时,不能对底层的数组进行赋值。
15.可以用String控制switch语句码?
答:可以。