黑马程序员技术交流社区
标题:
shuzu
[打印本页]
作者:
liuguodong
时间:
2015-8-26 22:43
标题:
shuzu
import java.util.Scanner;
class Aa {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
int[][] arr2 = {{1,2,3},{4,5},{6,7,8,9}};
char[] chr = {'一','二','三','四','五','刘','⑦'};
print(arr);
int max = print_Max(arr);
System.out.println("Max = "+max);
int index = print_Index(7,arr);
if(index != -1){
System.out.println("索引位置"+index);
}else
System.out.println("找不到值");
erWei(arr2);
MaoPao(arr);
System.out.println("====================");
half_find(arr,6);
System.out.println("====================");
sowp(arr);
System.out.println("====================");
print_Chinese(chr);
System.out.println("====================");
erWei(arr2);
}
public static void print(int[] arr) {
for(int i = 0;i < arr.length;i++) {
System.out.print(+arr[i]+" ");
}
}
public static int print_Max(int[] arr) {
int max = arr[0];
for(int i = 1;i < arr.length; i++) {
if(max <= arr[i]) {
max = arr[i];
}
}
return max;
}
public static int print_Index(int x,int [] arr) {
for(int i = 0; i < arr.length;i++ ) {
if(x==arr[i]) {
return i;
}
}
return -1;
}
public static void MaoPao(int[] arr) {
for(int i = 0; i < arr.length - 1;i++) { //需要冒泡的次数
for(int j = 0;j < arr.length - i-1;j++) {
int temp = 0;
if(arr[j] >= arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void half_find(int[] arr,int x) {
int left = 0;
int right = arr.length-1;
int mid = arr.length/2;
for(;;){
if(x == arr[mid]) {
System.out.println("Index ="+mid);
break;
}
if(x > arr[mid]) {
left = mid + 1;
System.out.println("left = mid + 1 = " +left+" "+"mid = "+mid );
mid = (left + right)/2;
}
if(x < arr[mid]) {
right = mid - 1;
System.out.println("right = mid - 1 = " +right+" "+"mid = "+mid);
mid = (left + right)/2;
}
if(left > right) {
System.out.println("未找到");
break;
}
}
}
public static void sowp(int[] arr) {
for(int i = 0; i < arr.length/2;i++ ) {
int temp = 0;
temp = arr[i];
arr[i] = arr[arr.length-1-i];
arr[arr.length-1-i] = temp;
}
for(int i = 0;i < arr.length;i++) {
System.out.print(arr[i]+" ");
}
}
public static void print_Chinese(char[] chr) {
Scanner sc = new Scanner(System.in);
int key = sc.nextInt();
if(key>7||key<1) {
System.out.println("输入错误,请输入1-7的数字");
return;
}
for(int i = 0;i < chr.length;i++) {
if(i == key - 1) {
System.out.println(key+"位置对应的数字"+chr[i]);
break;
}
}
}
public static void erWei(int[][] arr) {
for(int i = 0; i < arr.length;i++) {
for(int j = 0;j < arr[i].length;j++) {
System.out.print(arr[i][j]);
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2