---最大值、最小值
import java.io.*;
import java.util.*;
public class test3 {
public static void main(String args[]) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str;
int i, sum = 0, a[] = new int[100];
for (i = 0; i < 5; i++) {
System.out.print("輸入值:");
str = buf.readLine();
a[i] = Integer.parseInt(str);
sum = sum + a[i];
}
int max = a[0], min = a[0];
for (i = 0; i < 5; i++) {
if (a[i] > sum / 5)
System.out.println("大於平均值"+a[i]);
}
for (i = 1; i < 5; i++) {
if (a[i] > max)
max = a[i];
if (a[i] < min)
min = a[i];
}
System.out.println("最大值: " + max);
System.out.println("最小值: " + min);
}
}
--------------------輸出索引值
import java.io.*;
import java.util.*;
public class test3 {
public static void main(String args[]) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str;
int i, sum = 0, a[] = new int[100];
for (i = 0; i < 5; i++) {
System.out.print("輸入值:");
str = buf.readLine();
a[i] = Integer.parseInt(str);
sum = sum + a[i];
}
int max = a[0], min = a[0];
int maxindex = 0, minindex = 0;
for (i = 0; i < 5; i++) {
if (a[i] > sum / 5)
System.out.println("大於平均值"+a[i]);
}
for (i = 1; i < 5; i++) {
if (a[i] > max){
max = a[i];
maxindex=i;}
if (a[i] < min){
min = a[i];
minindex=i;
}
}
System.out.println("最大值: " + max);
System.out.println("maxIndex = " +maxindex);
System.out.println("最小值: " + min);
System.out.println("minIndex = " +minindex);
}
}
-----console
輸入值:6
輸入值:10
輸入值:8
輸入值:2
輸入值:7
大於平均值10
大於平均值8
大於平均值7
最大值: 10
maxIndex = 1
最小值: 2
minIndex = 3
----------輸入A 顯示B
import java.io.*;
import java.util.*;
public class test {
public static void main(String args[]) throws IOException {
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
String str;
int i, sum = 0, a[] = new int[100];
String ss = buf.readLine();
String str1[] = { "A", "B", "C", "D", "E", "F" };
String str2[] = { "B", "C", "D", "E", "F", "G" };
// ss=E;
for (i = 0; i < str1.length; i++) {
if (ss.equals(str1[i])) {
System.out.print(str2[i]);
}
}
}
}
留言列表