6.24

来源:互联网 发布:2017中国机电贸易数据 编辑:程序博客网 时间:2024/04/28 14:51

1、创建一个数组

public class ke1 {


public static void main(String[] args) {
        String a[]=new String [4];
        for(int i=0;i<a.length;i++){
        String b=JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"姓名");
        a[i]=b;
        }
        for(int i=0;i<a.length;i++){
        System.out.println(a[i]);
        }


}

2、找数数组中的最大值以及所在的下标;

public static void main(String[] args) {
int a[] = { 4, 58, 32, 48, 41, 39 };
int max = 0;
int num = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
num = i;
}
}
System.out.println(num);
System.out.println(max);
}
}

3、查找判断是否在数组里面;

import javax.swing.JOptionPane;


public class ke3 {
// 上面的是用在的位置做判断,下面的是用boolean来做判断
//这个是判断
public static void main(String[] args) {
// String a = JOptionPane.showInputDialog(null, "请输入你要查找的逗比");
// String[] s = { "张三", "李四", "王二麻子", "朱六", "赵七" };
// int num = -1;
//
// for (int i = 0; i < s.length; i++) {
// if (a.equals(s[i])) {
// num = i;
// break;
// }
// }
// if (num != -1) {
// System.out.println(a + "在我们班上,在第" + (num + 1) + "排位置上");
// } else
// System.out.println(a + "不在我们班上");

String a=JOptionPane.showInputDialog(null,"请输入逗比的名字");
String []s={"啊啊","哦哦","额额","咦咦","呜呜"};
int num=-1;
boolean t=false;
for(int i=0;i<s.length;i++){
if(a.equals(s[i])){
num=i;
t=true;
break;
}

}
if(t==false){
JOptionPane.showMessageDialog(null,a+" 不在班上撒");

}else{
JOptionPane.showMessageDialog(null, a+"在我们班上");
}

}
}

4、打印工资在同一页上,并且输出最大的工资

import javax.swing.JOptionPane;


//打印出4个人的工资用字符串的方式打印在一页上面,并找出其中工资最高的人
public class ke4 {


public static void main(String[] args) {
String name[] = new String[4];
int money[] = new int[4];
int index = -1;
int max = 0;
for (int i = 0; i < name.length; i++) {
String s = JOptionPane.showInputDialog(null, "请输入第" + (i + 1)
+ "个员工姓名");
String b = JOptionPane.showInputDialog(null, "请输入第" + (i + 1)
+ "个员工工资");
name[i] = s;
money[i] = Integer.parseInt(b);
if (money[i] > max) {
max = money[i];
index = i;
}
}
String str = "姓名       工资\n";
for (int i = 0; i < name.length; i++) {
str = str + name[i] + "   " + money[i] + "\n";


}
JOptionPane.showMessageDialog(null, str);
JOptionPane.showMessageDialog(null, name[index] + "的工资是" + money[index]
+ ",是最高的");
}


}

5、输入姓名,可以查找到对应的工资;

import javax.swing.JOptionPane;


public class ke5 {
// 输入姓名,可以查找出工资 ,主要是找出对应的下标, 从一个数组的下表号,套出另外一个数组下表号对应的值
//这个是查找
public static void main(String[] args) {
String name[] = { "张无忌", "赵敏", "丁敏君", "纪晓芙" };
String money[] = { "5000", "10000", "2000", "2500" };
String a = JOptionPane.showInputDialog(null, "请输入倚天屠龙记的人物姓名");
int index = -1;
for (int i = 0; i < name.length; i++) {
// 一定要注意到!!!!字符串相等是equals不要又忘了!!!!!
if (a.equals(name[i])) {
index = i;
}
}
if (index == -1) {
JOptionPane.showMessageDialog(null, "查无此人");
} else {
JOptionPane.showMessageDialog(null, a + "的工资是:" + money[index]);
}
}
}

6、把数组从大到小进行排序

public class ke7 {


public static void main(String[] args) {
// 设定一个a的数组
int a[] = { 3, 1, 8, 2, 6 };
// 循环判定a数组里面从下标0开始计算的数
for (int i = 0; i < a.length; i++) {
// 循环判定a数组里面下标从1开始计算的数
for (int j = i + 1; j < a.length; j++) {
// 判定如果a[i]<a[i+1],
if (a[i] < a[j]) {
// a[i]<a[j]的时候,是属于降序的排列 如果a[i]>a[j],是属于升序排列
// 这里用的是酱油和醋的方式,进行替换,然后再重新进行对比
int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}


}
// 一切都是以a[i]为基础开始计算的,所以打印a[i]
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}


}

7、替换数组中的数,先求出换的数的位置,然后后面的依次往前移;

public static void main(String[] args) {
int[] a = { 3, 5, 7, 9, 12 };
int index = -1;
// 先找到7的位置
for (int i = 0; i < a.length; i++) {
if (a[i] == 7) {
index = i;
break;
}
}
// 用index后面的数去替换前面的数,a[]=a[i+1]就是第四位替换第三,然后第五替换第四循环。
// length-1的意思是指不替换就溢出了
for (int i = index; i < a.length - 1; i++) {
a[i] = a[i + 1];
}
// 直接打印整个序列,在index后面都是替换,所以,index的位数直接被替换了
for (int i = 0; i < a.length - 1; i++) {
System.out.println(a[i]);
}

0 0
原创粉丝点击