因此对于这个程序,我需要从用户那里获得三个字符串并按字母顺序对这些字符串进行排序。需要注意的是,我无法使用数组。此外,如果字符串不是以字母开头,也不是不接受。到目前为止,我已经设法从用户那里获得三个字符串,并且能够确定是否应该接受它们。我无法弄清楚的是如何按字母顺序对它们进行排序。我遇到的主要问题是忽略排序时不接受的字符串。这是从用户获取字符串的代码。
Scanner scan = new Scanner(System.in);
System.out.print("Please input the first name: ");
String name1 = scan.nextLine();
name1 = name1.substring(0, 1).toUpperCase() + name1.substring(1,name1.length()).toLowerCase();
if (name1.charAt(0) >= 'A' && name1.charAt(0) <= 'Z' ){
System.out.println("The first name is: " + name1);
}else{
System.out.println("Error: The name was not accepted");
name1 = null;
}
System.out.print("Please input the second name: ");
String name2 = scan.nextLine();
name2 = name2.substring(0, 1).toUpperCase() + name2.substring(1,name2.length()).toLowerCase();
if (name2.charAt(0) >= 'A' && name2.charAt(0) <= 'Z'){
System.out.println("The second name is: " + name2);
}else{
System.out.println("Error: The name was not accepted");
name2 = null;
}
System.out.print("Please input the third name: ");
String name3 = scan.nextLine();
name3 = name3.substring(0, 1).toUpperCase() + name3.substring(1,name3.length()).toLowerCase();
if (name3.charAt(0) >= 'A' && name3.charAt(0) <= 'Z'){
System.out.println("The third name is: " + name3);
}else{
System.out.println("Error: The name was not accepted");
name3 = null;
}
答案 0 :(得分:1)
我提出了这个解决方案。在没有数组的情况下编写此代码是可读性的噩梦。
import java.util.Scanner;
public class NameSort {
private static final Scanner scan = new Scanner(System.in);
public static void nameStort() {
String name1 = readName("first");
String name2 = readName("second");
String name3 = readName("third");
String potentialName1 = null;
String potentialName2 = null;
String potentialName3 = null;
String potentialName4 = null;
String potentialName5 = null;
String potentialName6 = null;
String potentialName7 = null;
if (name1 == null) {
if (name2 == null) {
potentialName4 = name3;
} else {
potentialName4 = name2;
if (name3 != null) {
if (name2.compareTo(name3) > 0) {
potentialName2 = name3;
} else {
potentialName6 = name3;
}
}
}
} else {
potentialName4 = name1;
if (name2 == null) {
if (name3 != null) {
if (name1.compareTo(name3) > 0) {
potentialName2 = name3;
} else {
potentialName6 = name3;
}
}
} else {
if (name1.compareTo(name2) > 0) {
potentialName2 = name2;
} else {
potentialName6 = name2;
}
if (name3 != null) {
if (name1.compareTo(name3) > 0) {
if (name2.compareTo(name3) > 0) {
potentialName1 = name3;
} else {
potentialName3 = name3;
}
} else {
if (name2.compareTo(name3) > 0) {
potentialName5 = name3;
} else {
potentialName7 = name3;
}
}
}
}
}
System.out.println("The sorted names are: ");
printIfNotNull(potentialName1);
printIfNotNull(potentialName2);
printIfNotNull(potentialName3);
printIfNotNull(potentialName4);
printIfNotNull(potentialName5);
printIfNotNull(potentialName6);
printIfNotNull(potentialName7);
}
private static void printIfNotNull(String potentialName) {
if (potentialName != null) {
System.out.println(potentialName);
}
}
public static String readName(String position) {
System.out.print("Please input the " + position + " name: ");
String name = toTitleCase(scan.nextLine().trim());
if (!name.isEmpty() && Character.isLetter(name.charAt(0))) {
System.out.println("The " + position + " name is: " + name);
return name;
} else {
System.out.println("Error: The name was not accepted");
}
return null;
}
public static String toTitleCase(String word) {
if (word.isEmpty()) {
return word;
}
// one way to do it
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
}
public static void main(String[] args) {
nameStort();
}
}
但如果没有赋值中的愚蠢数组或集合限制,代码就更具可读性:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class NameSort {
private static final Scanner scan = new Scanner(System.in);
public static void nameStort() {
List<String> names = Arrays.asList(readName("first"), readName("second"), readName("third"));
Collections.sort(names);
for (String name : names) {
printIfNotEmpty(name);
}
}
private static void printIfNotEmpty(String name) {
if (!name.isEmpty()) {
System.out.println(name);
}
}
public static String readName(String position) {
System.out.print("Please input the " + position + " name: ");
String name = toTitleCase(scan.nextLine().trim());
if (!name.isEmpty() && Character.isLetter(name.charAt(0))) {
System.out.println("The " + position + " name is: " + name);
return name;
} else {
System.out.println("Error: The name was not accepted");
}
return "";
}
public static String toTitleCase(String word) {
if (word.isEmpty()) {
return word;
}
// one way to do it
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
}
public static void main(String[] args) {
nameStort();
}
}
答案 1 :(得分:0)
对于任何想知道这里的人来说,这是我对这个问题的解决方案。 公共课Assignment3 {
public static void main(String[] args) {
//read names and return error if the name starts with a non-alphabetic character
Scanner scan = new Scanner(System.in);
System.out.print("Please input the first name: ");
String name1 = scan.nextLine();
name1 = name1.substring(0, 1).toUpperCase() + name1.substring(1,name1.length()).toLowerCase();
if (Character.isLetter(name1.charAt(0))){
System.out.println("The first name is: " + name1);
}else{
System.out.println("Error: The name was not accepted");
name1 = "";
}
System.out.print("Please input the second name: ");
String name2 = scan.nextLine();
name2 = name2.substring(0, 1).toUpperCase() + name2.substring(1,name2.length()).toLowerCase();
if (Character.isLetter(name2.charAt(0))){
System.out.println("The second name is: " + name2);
}else{
System.out.println("Error: The name was not accepted");
name2 = "";
}
System.out.print("Please input the third name: ");
String name3 = scan.nextLine();
name3 = name3.substring(0, 1).toUpperCase() + name3.substring(1,name3.length()).toLowerCase();
if (Character.isLetter(name3.charAt(0))){
System.out.println("The third name is: " + name3);
}else{
System.out.println("Error: The name was not accepted");
name3 = "";
}
//Sort the names alphabetically
String first = "";
if(name1.compareTo(name2) <= 0 && name1.compareTo(name3) <= 0){
first = name1;
}else if (name2.compareTo(name3) <= 0 && name2.compareTo(name1) <= 0){
first = name2;
}else if (name3.compareTo(name1) <= 0 && name3.compareTo(name2) <=0){
first = name3;
}
String middle = "";
if (name1.compareTo(name2)*name1.compareTo(name3) <= 0){
middle = name1;
}else if (name2.compareTo(name1)*name2.compareTo(name3) <= 0){
middle = name2;
}else if (name3.compareTo(name2)*name3.compareTo(name1) <= 0){
middle = name3;
}
String last = "";
if(name1.compareTo(name2) >= 0 && name1.compareTo(name3) >= 0){
last = name1;
}else if (name2.compareTo(name3) >= 0 && name2.compareTo(name1) >= 0){
last = name2;
}else if (name3.compareTo(name1) >= 0 && name3.compareTo(name2) >=0){
last = name3;
}
if(first.isEmpty() && middle.isEmpty()){
System.out.printf("The only name is \"%s\" ", last);
}else if(first.isEmpty()){
System.out.printf("the names are \"%s\"and \"%s\" ",middle,last);
}else{
System.out.printf("the names are \"%s\", \"%s\", and \"%s\" ",first,middle,last);
}
}
}