Java学习关于循环和数组练习题整理

网友投稿 302 2023-01-01


Java学习关于循环和数组练习题整理

循环例子:

while循环和do…while循环

whlie(条件语句) {

循环体

}//先进行条件语句的判断,再进行循环体

do {

循环体

}whlie (条件语句)//先执行一次循环后再进行条件语句的判断

break语句

break语句:结束全部循环,具体应用如下:

//1+2+3+...+n<1000,求n

//此题可以利用break语句在和大于1000时结束循环,输出n的值

public static void deal() {

int sum = 0;

int i = 1;

for (; ; i++) {

sum = sum + i;

if (sum > 1000) {

break;//当和大于1000时,利用break语句跳出循环

}

}

System.out.println(i);

}

相关实例练习题:

1.输出100到1000的水仙花数:

public class LoopTest{

public static void main(String[] args){

int bai = 0;

int shi = 0;

int ge = 0;

for(int i = 100; i < 1000; i++){

bai = i/100;

shi = i/10%10;

ge = i%10;

if(bai*bai*bai+shi*shi*shi+ge*ge*ge == i ){

System.out.println(i);

}

}

}

}

2.打印所有字母:

public class LoopTest{

public static void main(String[] args){

char lower = 'a';

char upper = 'A';

for(int i = 0 ; i < 26 ; i++){

System.out.println(lower+" "+upper);

lower++;

upper++;

}

}

}

3.打印99乘法表:

public class LoopTest{

public static void main(String[] args){

for(int i = 1; i <=9 ; i++){

for(int j = 1 ; j<=i ; j++){

System.out.print(j+"*"+i+"="+j*i+" ");

}

System.out.println();

}

}

}

4.按格式遍历数组:

public class ArrayTest{

public static void main(String[] args){

int[] arr1 = {4,6,7,1};

int[] arr2 = {1,2,3,9,10};

printArray(arr1);

printArray(arr2);

}

public static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

5.数组元素逆序(注意:不是反向遍历):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,2,3,4,7,8,9};

reverse(arr);

printArray(arr);

}

public static void reverse(int[] arr){

for(int min=0,max = arr.length-1; min

int temp = arr[min];

arr[min] = arr[max];

arr[max] = temp;

}

}

public static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

6.选择排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

selectSort(arr);

printArray(arr);

}

public static void selectSort(int[] arr){

for(int i = 0; i < arr.length-1; i++){

for(int j = i+1; j

if(arr[i]>arr[j]){

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

publhttp://ic static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

5.数组元素逆序(注意:不是反向遍历):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,2,3,4,7,8,9};

reverse(arr);

printArray(arr);

}

public static void reverse(int[] arr){

for(int min=0,max = arr.length-1; min

int temp = arr[min];

arr[min] = arr[max];

arr[max] = temp;

}

}

public static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

6.选择排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

selectSort(arr);

printArray(arr);

}

public static void selectSort(int[] arr){

for(int i = 0; i < arr.length-1; i++){

for(int j = i+1; j

if(arr[i]>arr[j]){

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

publhttp://ic static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

int temp = arr[min];

arr[min] = arr[max];

arr[max] = temp;

}

}

public static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

6.选择排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

selectSort(arr);

printArray(arr);

}

public static void selectSort(int[] arr){

for(int i = 0; i < arr.length-1; i++){

for(int j = i+1; j

if(arr[i]>arr[j]){

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

publhttp://ic static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

6.选择排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

selectSort(arr);

printArray(arr);

}

public static void selectSort(int[] arr){

for(int i = 0; i < arr.length-1; i++){

for(int j = i+1; j

if(arr[i]>arr[j]){

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

publhttp://ic static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if(arr[i]>arr[j]){

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

}

publhttp://ic static void printArray(int[] arr){

System.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

7.冒泡排序:

public class ArrayTest{

public static void main(String[] args){

int[] arr = {3,1,5,7,8,9,2};

bubbleSort(arr);

printArray(arr);

}

public static void bubbleSort(int[] arr){

for(int i = 0; i

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

for(int j = 0; j

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if(arr[j]>arr[j+1]){

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}

}

}

}

public static void printArray(int[] arr){

SvhHVmWystem.out.print("[");

for(int i = 0 ; i

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

if( i == arr.length-1){

System.out.println(arr[i]+"]");

}else{

System.out.print(arr[i]+",");

}

}

}

}

8.折半查找法(注意必须是有序的数组):

public class ArrayTest{

public static void main(String[] args){

int[] arr = {1,3,5,7,9,11};

int index = binarySearch(arr, 7);

System.out.println(index);

}

public static int binarySearch(int[] arr,int key){

int min = 0;

int max = arr.length-1;

int mid = 0;

while( min<=max ){

mid = (min+max)/2;

if(key>arr[mid]){

min = mid+1;

}else if(key

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}

max = mid-1;

}else{

return mid;

}

}

return -1;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口测试工具返回值自适应(接口功能测试)
下一篇:接口测试工具返回值(接口测试常见的返回码)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~