代码填空
在A B C D E F 六人中随机抽取3人中奖,要求中奖人不能重复。请完善以下代码:
public class MyTest
{
public static void main(String[] args)
{
Vector a = new Vector();
for(char i=’A’; i<=’F’; i++) //将字符放到动态的数组里面
a.add(“” + i); //将字符转换为字符串
for(int k=0; k<3; k++)
{
int d = (int)Math.random()*(6-k);// 产生0-5的随机整数
System.out.println(a.remove(d));
//将抽到的的字符显示出来并删除,在后面的抽取时候就不会存在重复的
}
}
}
代码填空
不同进制的数值间的转换是软件开发中很可能会遇到的常规问题。下面的代码演示了如何把键盘输入的3进制数字转换为十进制。试完善之。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int n = 0;
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if(c<‘0’ || c > ‘2’) throw new RuntimeException(“Format error”);
n=n*3+(c-‘0’);//字符型转换为整型
}
System.out.println(n);
代码填空
有如下程序,完成的功能为:找出数组中的最大元素。请填写程序的中空白,使程序运行正确。
public class test
{
public static void main(String[] args) {
int array[]={0,34,67,90,21,-9,98,1000,-78};
System.out.println(new test().findMax (array, 0));
}
public int findMax(int array[],int index)
{
if(array==null || array.length==0)
{
return 0;
}
int max=array[0];
if(index<array.length-1)
{
max=findMax(array,index+1);
//采用递归的方式取得当前后一位值,并将返回值赋到max
}
if(max<array[index]) max= array[index];
return max;
}
}