Java学习心得-day4
package day4;
import javax.swing.JOptionPane;
public class day4Test {
void printArrary(double[] num){//打印所找数组元素下标
String str = JOptionPane.showInputDialog(null,"请输入一个数组下标:");
int index = Integer.parseInt(str);
if (index >= num.length)
JOptionPane.showMessageDialog(null,"数组下标越界:");
else
JOptionPane.showMessageDialog(null,"该下标对应的数据为:"+num[index]);
}
void findNumber(double[] num){//查找数据所对应的下标
boolean flag = false;
String str = JOptionPane.showInputDialog(null,"请输入一个要查找的数据:");
double number = Double.parseDouble(str);
for (int i=0; i<num.length; i++)
if (number == num[i]){
JOptionPane.showMessageDialog(null,"该数据对应的下标为:"+i);
flag = true;
}
if (flag == false)
JOptionPane.showMessageDialog(null,"找不到你需要的数");
}
double average(double[] num,double aver){//输出数组平均数
double sum = 0.0;
for (int i=0; i<num.length; i++)
sum += num[i];
aver = sum/num.length;
return aver;
}
void printString(){//遍历输出字符串
String str = JOptionPane.showInputDialog(null,"请输入一个要遍历的字符串:");
char[] ch = str.toCharArray();//将字符串转换成字符数组
String result = "";
for (int i=0; i<ch.length; i++)
result = result + ch[i] + " ";
JOptionPane.showMessageDialog(null,"遍历输出字符串:"+result);
}
void func1(){//输出大写字母表
String str = "a";
char ch = str.charAt(0);
JOptionPane.showMessageDialog(null,"小写的a转换成大写的A:"+(char)(ch-32));
String s = "";
for (int i=0; i<26; i++)
s = s +(char)( ch + i)+" ";
JOptionPane.showMessageDialog(null,"遍历输出A-Z:"+s);
}
void toUpper(){//输出大写字母表
String str = "a";
String s = str.toUpperCase();//将字符串变成大写
JOptionPane.showMessageDialog(null,"小写的a转换成大写的A:"+s);
char[] ch = s.toCharArray();
String s1 = "";
for (int i=0; i<26; i++)
s1= s1 + (char)(ch[0]+i)+" ";
JOptionPane.showMessageDialog(null,"遍历输出A-Z:"+s1);
}
void toLower(){//输出小写字母表
String s = "A";
char[] arr = new char[26];
for (int i=0;i<26;i++)
arr[i] = (char)(s.charAt(0)+i);
String str = "";
for (int i=0; i<26; i++)
str = str + (char) (arr[i] + 32);
JOptionPane.showMessageDialog(null,"遍历输出a-z:"+str);
}
public static void main(String[] args) {
String str = JOptionPane.showInputDialog(null,"请输入数组长度:");
int N = Integer.parseInt(str);
JOptionPane.showMessageDialog(null,"请输入"+N+"个数据:");
double[] num = new double[N];
for (int i=0; i<N; i++){
String str1 = JOptionPane.showInputDialog(null,"请输入第"+(i+1)+"个数据:");
num[i] = Double.parseDouble(str1);
}
String s = "";
for (int j=0; j<N; j++){
s += num[j];
s += " ";
}
JOptionPane.showMessageDialog(null,"遍历输出输入的数组:"+s);
double value = Double.parseDouble(JOptionPane.showInputDialog(null,"请输入一个要修改下标的数组元素所对应的值:"));
int index = Integer.parseInt(JOptionPane.showInputDialog(null,"输入一个数组下标:"));
num[index] = value;
JOptionPane.showMessageDialog(null,"修改后的num["+index+"]"+"="+num[index]);
String str1 = "";
for (int j=0; j<N; j++){
str1 += num[j] + " ";
}
JOptionPane.showMessageDialog(null,"遍历输出修改过后的数组:"+str1);
day4Test d = new day4Test();
d.printArrary(num);//输出下标对应的数据
d.findNumber(num);//查找数据对应的下标
JOptionPane.showMessageDialog(null,"平均数为:"+d.average(num, 0.0));//求数组平均数
d.printString();//打印字符串
d.toUpper();//输出大写字母表
d.toLower();//输出小写字母表
}
}

浙公网安备 33010602011771号