java 考试体

来源:百度知道 编辑:UC知道 时间:2024/06/05 19:17:27
用java编写一个程序
使1+2+3+4……+n
a、b、c三个整数由小到大顺序排列
java程序执行的入口方法是?
下列不属于循环语句的是?
A、if else B、while C、for D、do while
反正就这几道题你们都是高手就看着办把

第一题 用java编写一个程序:
public int test(int n){
int j = 0;
for(int i = 1;i <= n;i++){
j += i;
}
return j;
}
第二题 a、b、c三个整数由小到大顺序排列:
用冒泡排序
public void sort(Comparable[] obj)
{
if (obj == null)
{
throw new NullPointerException("The argument can not be null!");
}
Comparable tmp;
for (int i = 0; i < obj.length; i++)
{ //切记,每次都要从第一个开始比。最后的不用再比。
for (int j = 0; j < obj.length - i - 1; j++)
{ //对邻接的元素进行比较,如果后面的小,就交换
if (obj[j].compareTo(obj[j + 1]) > 0)
{
tmp = obj[j];
obj[j] = obj[j + 1];
obj[j + 1] = tmp;
}
}
}
}
第三题 java程序执行的入口方法是?:
main方法
下列不属于循环语句的是