您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页编程题

编程题

来源:筏尚旅游网
只做了编程题,其他的没有做 1、求1/1+1/2+1/3+1/4+........+1/100;

public class Count { public static void main(String[] args) { double sum=0;

for (int i = 1; i <=100; i++) { sum=sum+1.0/i;

}

System.out.println(sum); }

}

2、求1+2+4+7+11+16+22+29;这里只加到了29

public class Count {

public static void main(String[] args) {

int t=0; long p=0; int i = 1;

这里求1+2+4+7+11+16+22+29,由于最后一个数是29,所以写i<=29,做后一个数是多少,i就小于等于多少

} }

while(i<=29){ }

System.out.println(p); p=p+i; t=t+1; i=i+t;

3、求数组中的最大值和最小值

public class Count {

public static void main(String[] args) {

int[] a={23,68,78,25,9,3}; int max=a[0]; int min=a[0];

for (int i = 0; i < a.length; i++) {

//红色的代码用于求最大值,如果只求最小值,就将红色的代码删掉

if(max//蓝色的代码用于求最小值,如果只求最大值,就将蓝色的代码删掉

}

if(min>a[i]){ }

min=a[i]; max=a[i];

//红色的代码用于输出最大值,如果只求最小值,就将红色的代码删掉

System.out.println(\"最大值为:\"+max);

//蓝色的代码用于输出最小值,如果只求最大值,就将蓝色的代码删掉

System.out.println(\"最小值为:\"+min);

}

}

4、求字符串中数值和字母的个数

public class Count {

public static void main(String[] args) {

int numCount=0; int lettCount=0;

String str=\"1232vgtgHyR\";

for (int i = 0; i < str.length(); i++) {

char strChar=str.charAt(i);

//蓝色的代码用于求数值的个数,如果只求字母的个数,就将蓝色的代码删掉

if(strChar>='0'&&strChar<='9'){ }

//红色的代码用于求字母的个数,如果只求数值的个数,就将蓝色的代码删掉

){

}

//蓝色的代码用于输出数值的个数,如果只求字母的个数,就将蓝色的代码删掉

}

lettCount++;

if(strChar>='a'&&strChar<='z'||strChar>='A'&&strChar<='Z'

numCount++;

System.out.println(\"数值的个数为:\"+numCount);

//红色的代码用于输出字母的个数,如果只求数值的个数,就将蓝色的代码删掉 }

}

System.out.println(\"字母的个数为:\"+lettCount);

5、选择排序

public class SelectSort {

public static void main(String[] args) {

int[] array = new int[] { 5, 1, 2, 8, 4, 6, 9, 7, 3, 0 }; int temp; // 临时变量

System.out.println(\"原有数组内容:\");

printArray(array);// 输出原有数组 int index;

// 索引变量

for (int i = 1; i < array.length; i++) {// 正序排列

}

index = 0;

for (int j = 1; j <= array.length - i; j++) { }

// 交换在data.length-i和index两个位置上的数组元素 temp = array[array.length - i];

array[array.length - i] = array[index]; array[index] = temp;

if (array[j] > array[index]) { }

index = j;

System.out.println(\"正序排列数组内容:\");

printArray(array);// 输出排序后的数组

for (int i = 1; i < array.length; i++) { // 倒序排列

index = 0;

for (int j = 1; j <= array.length - i; j++) { }

// 交换在data.length-i和index两个位置上的数组元素 temp = array[array.length - i];

if (array[j] < array[index]) { }

index = j;

}

}

}

array[array.length - i] = array[index]; array[index] = temp;

System.out.println(\"倒序排列数组内容:\");

printArray(array);// 输出排序后的数组

/**

* 遍历数组的方法,该方法输出数组所有内容 * @param array 要遍历的数组 */

public static void printArray(int[] array) { }

for (int i : array) { }

System.out.println(\"\\n\");

System.out.print(i + \" \");

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务