public class Solution {
public static void main(String[] args) throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(reader.readLine());
}
int min = list.get(0).length();
int max = list.get(0).length();
for (int i = 1; i < list.size(); i++) {
if (min > list.get(i).length())
min = list.get(i).length();
if (max < list.get(i).length())
max = list.get(i).length();
}
shortestFound:
longestFound:
for (int i = 0; i < list.size(); i++) {
//look for longest string
if (list.get(i).length() == max){
for (int j = 0; j < list.size(); j++) {
//look for shortest string
if (list.get(j).length() == min)
//If the longest string appears before the shortest string,
//display the longest string.
if (j == i+1){
System.out.println(list.get(i));
//if longest Found,Interrupt all loops
break longestFound;
}
}
}
//if didn't find the longest, keep going to look for shortest string
if (list.get(i).length() == min){
for (int j = 0; j < list.size(); j++) {
//look for longest string
if (list.get(j).length() == max)
// If the shortest string appears before the longest string,
//display the longest string.
if (j == i+1){
System.out.println(list.get(i));
//if shortest Found ,Interrupt all loops
break shortestFound;
}
}
}
}
}
}package zh.codegym.task.task07.task0712;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
最短或最长
*/
public class Solution {
public static void main(String[] args) throws Exception {
//在此编写你的代码
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(reader.readLine());
}
int min = list.get(0).length();
int max = list.get(0).length();
for (int i = 1; i < list.size(); i++) {
if (min > list.get(i).length())
min = list.get(i).length();
if (max < list.get(i).length())
max = list.get(i).length();
}
min:
max:
for (int i = 0; i < list.size(); i++) {
if (list.get(i).length() == max){
for (int j = 0; j < list.size(); j++) {
if (list.get(j).length() == min)
if (j == i+1){
System.out.println(list.get(i));
break max;
}
}
}
if (list.get(i).length() == min){
for (int j = 0; j < list.size(); j++) {
if (list.get(j).length() == max)
if (j == i+1){
System.out.println(list.get(i));
break min;
}
}
}
}
// for (int i = 0; i < list.size(); i++) {
// if (list.get(i).length() == max){
// for (int j = 0; j < list.size(); j++) {
// if (list.get(j).length() == min)
// if (j == i+1){
// System.out.println(list.get(i));
// break;
// }
// }
// }
// break;
// }
}
}
They means:
If the shortest string appears before the longest string, the program should display the shortest string.
If the longest string appears before the shortest string, the program should display the longest string.
Oh I'm so hurt.