上代码:
1 冒泡排序实现对给定数组的排序:
//冒泡排序实现对给定数组的排序
import java.lang.String; //这一段应该可以不要
//冒泡排序
public class Shiyan{
public static void main(String[] args){
int array[] = {35,23,87,90,20};
for(int i=1;i<array.length;i++){ //循环判断
for(int j=0;j<array.length;j++){
if(array[i]<array[j]){ //进行大小的比较,交换位置
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for(int i=0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
}
}
输出:
20 23 35 87 90
进程已结束,退出代码0
2 编程求一到二十的阶乘和:
//1~20的阶乘和
public class Shiyan{
public static void main(String[] args){
long sum=0,b=1; //因为阶乘的数值比较大,所以为了数据的溢出采用长整型long
for(int i=1;i<=20;i++){
b*=i;
sum+=b;
}
System.out.println("一到二十的阶乘和为:"+sum);
}
}
输出:
一到二十的阶乘和为:2561327494111820313
进程已结束,退出代码0
3 逐个输出1~1000之间可以同时被3、5、7整除的数字:
//输出1~1000能被3,5,7整除的数
public class Shiyan{
public static void main(String[] args){
for(int a=1;a<=1000;a++){
if(a%3==0&&a%5==0&&a%7==0){
System.out.println("1~1000能被3、5、7整除的数有:"+a);
}
}
}
}
输出:
1~1000能被3、5、7整除的数有:105
1~1000能被3、5、7整除的数有:210
1~1000能被3、5、7整除的数有:315
1~1000能被3、5、7整除的数有:420
1~1000能被3、5、7整除的数有:525
1~1000能被3、5、7整除的数有:630
1~1000能被3、5、7整除的数有:735
1~1000能被3、5、7整除的数有:840
1~1000能被3、5、7整除的数有:945
进程已结束,退出代码0
4 打印100~1000范围内的所有“水仙花数”,水仙花数就是指一个3位数,其给位数字立方和等于该数本身:
//打印水仙花数
public class Shiyan{
public static void main(String[] args){
int a,b,c;
for(int n=100;n<=999;n++){
a=n/100;
b=n%100/10;
c=n%10;
if(n==a*a*a+b*b*b+c*c*c){
System.out.println("100~1000内的水仙花数有:"+n);
}
}
}
}
输出:
100~1000内的水仙花数有:153
100~1000内的水仙花数有:370
100~1000内的水仙花数有:371
100~1000内的水仙花数有:407
进程已结束,退出代码0
5 写出下面程序运行的结果,并画出流程图,代码流程图如下,流程图知识有点不记得了,可能有错误:
public class Shiyan{
public static void main(String[] args){
int a=25,b=20,e=3,f=0;
boolean d=a<b;
System.out.println("a=25,b=20,e=3,f=0");
System.out.println("因为关系表达式a<b为假,所以其逻辑值为:"+d);
if(e!=0&&a/e>5)
System.out.println("欣慰e非0且a/e为8大于5,所以输出a/e="+a/e);
if(f!=0&&a/f>5)
System.out.println("a/f="+a/f);
else
System.out.println("因为f值为0,所以输出f="+f);
}
}
输出:
a=25,b=20,e=3,f=0
因为关系表达式a<b为假,所以其逻辑值为:false
欣慰e非0且a/e为8大于5,所以输出a/e=8
因为f值为0,所以输出f=0
进程已结束,退出代码0