我被困了(有点),我似乎无法看到完成此代码的方法。我必须设计一个没有数组的程序(我的老师很糟糕,不允许数组-_-)程序的目标是接受3-5个单词,并使用字母顺序排列它们。我制作了一个可以使用3个单词的程序,但是我很困惑如何制作一个按字母顺序排列5个单词的程序。任何帮助,将不胜感激。注意,我的问题不同于其他Alphabetize w / o Array问题,因为它涉及5个单词,而不是3个,2个或任何其他数量。谢谢您的帮助!代码在下面。
import java.util.Scanner;
public class Alphabetize
{ int nums = 1;
int x = 0;
String input01 = "";
String input02 = "";
String input03 = "";
String input04 = "";
String input05 = "";
int compare01, compare02, compare03, compare04, compare05;
public static void main(String[] args)
{
Alphabetize a = new Alphabetize();
a.getInput();
a.comparer();
}
public void getInput()
{
Scanner kb = new Scanner(System.in);
System.out.println("How many words do you want to input");
nums = kb.nextInt();
System.out.println("Enter " + nums +" words");
if(nums == 3)
{
input01 = kb.next();
input02 = kb.next();
input03 = kb.next();
}
else if(nums == 4)
{
input01 = kb.next();
input02 = kb.next();
input03 = kb.next();
input04 = kb.next();
}
else
{
input01 = kb.next();
input02 = kb.next();
input03 = kb.next();
input04 = kb.next();
input05 = kb.next();
}
}
public void comparer()
{
if (nums ==3)
{
compare01 = input01.compareTo(input01);
System.out.println(input01 + compare01);
compare02 = input01.compareTo(input02);
System.out.println(input02 + compare02);
compare03 = input01.compareTo(input03);
System.out.println(input03 + compare03);
}
else if (nums ==4)
{
compare01 = input01.compareTo(input01);
System.out.println(input01 + compare01);
compare02 = input01.compareTo(input02);
System.out.println(input02 + compare02);
compare03 = input01.compareTo(input03);
System.out.println(input03 + compare03);
compare04 = input01.compareTo(input04);
System.out.println(input04 + compare04);
}
else
{
compare01 = input01.compareTo(input01);
System.out.println(input01 + compare01);
compare02 = input01.compareTo(input02);
System.out.println(input02 + compare02);
compare03 = input01.compareTo(input03);
System.out.println(input03 + compare03);
compare04 = input01.compareTo(input04);
System.out.println(input04 + compare04);
compare05 = input01.compareTo(input05);
System.out.println(input05 + compare05);
}
}
}