好的,所以我在学校的AP计算机科学课上使用了EdHesive的AP计算机科学A课程和该课程的作业6,我的朋友和我在一起工作,但我们&# 39;过去一周我一直在苦苦挣扎,想弄清楚如何编写代码来完成它。
这是我们迄今为止所写的内容。我有一节评论,因为我正在测试是否可以让if语句工作,但它一直拒绝以它应该的方式工作。
import java.util.*;
import java.lang.*;`
class Main {
public static void main(String args[]) {`
Scanner s = new Scanner (System.in);
System.out.println("Enter the values for the first array, up to 10000 values, enter 'End' to quit");
String [] firstArray = new String[9999];
for(int i = 0; i <= 9999; i++)
{
firstArray[i] = s.nextLine();
if(firstArray[i].toLowerCase().contains("end"))
{
String [] firstArrayOut = new String[i];
for(int x = 0; x < i; x++)
{
if((x < i) && (x != (i - 1)))
{
firstArrayOut[x] = firstArray[x].toLowerCase().substring(0, 1).toUpperCase() + firstArray[x].substring(1).toLowerCase() + " ";
}
else
firstArrayOut[x] = firstArray[x].toLowerCase().substring(0, 1).toUpperCase() + firstArray[x].substring(1).toLowerCase();
}
break;
}
}
System.out.println("\nEnter the values for the second array, up to 10000 values, enter 'End' to quit");
String [] secondArray = new String[9999];
for(int i = 0; i <= 9999; i++)
{
secondArray[i] = s.nextLine();
if(secondArray[i].toLowerCase().contains("end"))
{
String [] secondArrayOut = new String[i];
for(int x = 0; x < i; x++)
{
if((x < i) && (x != (i - 1)))
{
secondArrayOut[x] = secondArray[x].toLowerCase().substring(0, 1).toUpperCase() + secondArray[x].substring(1).toLowerCase() + " ";
}
else
secondArrayOut[x] = secondArray[x].toLowerCase().substring(0, 1).toUpperCase() + secondArray[x].substring(1).toLowerCase();
}
for(int y = 0; y <= (secondArrayOut.length - 1); y++)
{
if(secondArrayOut[y].substring(0).contains("A"))
{
//Testing to see if it will order them, but I'm still struggling with getting them to print out in ABC order
System.out.println(secondArrayOut[y]);
}
/* else if(secondArrayOut[y].substring(0).contains("B"))
{
}
else if(secondArrayOut[y].substring(0).contains("C"))
{
}
else if(secondArrayOut[y].substring(0).contains("D"))
{
}
else if(secondArrayOut[y].substring(0).contains("E"))
{
}
else if(secondArrayOut[y].substring(0).contains("F"))
{
}
else if(secondArrayOut[y].substring(0).contains("G"))
{
}
else if(secondArrayOut[y].substring(0).contains("H"))
{
}
else if(secondArrayOut[y].substring(0).contains("I"))
{
}
else if(secondArrayOut[y].substring(0).contains("J"))
{
}
else if(secondArrayOut[y].substring(0).contains("K"))
{
}
else if(secondArrayOut[y].substring(0).contains("L"))
{
}
else if(secondArrayOut[y].substring(0).contains("M"))
{
System.out.println("Matthew");
}
else if(secondArrayOut[y].substring(0).contains("N"))
{
}
else if(secondArrayOu[y]t.substring(0).contains("O"))
{
}
else if(secondArrayOut[y].substring(0).contains("P"))
{
}
else if(secondArrayOut[y].substring(0).contains("Q"))
{
}
else if(secondArrayOut[y].substring(0).contains("R"))
{
}
else if(secondArrayOut[y].substring(0).contains("S"))
{
}
else if(secondArrayOut[y].substring(0).contains("T"))
{
}
else if(secondArrayOut[y].substring(0).contains("U"))
{
}
else if(secondArrayOut[y].substring(0).contains("V"))
{
}
else if(secondArrayOut[y].substring(0).contains("W"))
{
}
else if(secondArrayOut[y].substring(0).contains("X"))
{
}
else if(secondArrayOut[y].substring(0).contains("Y"))
{
}
else(secondArrayOut[y].substring(0).contains("Z"))
{
}*/
}
break;
}
}
//System.out.println("\nFirst Array\n" + firstArrayOutABC + "\n\nSecond Array\n" + secondArrayOutABC + "\n\nMerged Array\n" + mergedArrayABC);
}
}
即使我将两个阵列都以ABC顺序打印出来,每个名字的第一个字母是唯一的大写字母,我仍然必须合并它们然后打印出来。我们将非常感谢您提供有关此任务的任何帮助,我希望有另一种方式来订购它们而不使用if和else if语句会更容易,谢谢!
答案 0 :(得分:0)
import java.util.*;
import java.lang.*;
import java.io.*;
public class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
// Create a list of strings
ArrayList<String> al = new ArrayList<String>();
al.add("Geeks For Geeks");
al.add("Friends");
al.add("Dear");
al.add("Is");
al.add("Superb");
/* Collections.sort method is sorting the
elements of ArrayList in ascending order. */
Collections.sort(al);
// Let us print the sorted list
System.out.println("List after the use of" +
" Collection.sort() :\n" + al);
}
}
答案 1 :(得分:0)
public void sort(String[] args) {
for(int i=0;i<args.length-1;i++){
for(int j=i+1;j<args.length;j++){
if(args[i].compareTo(args[j]) >=0){
String temp = args[i];
args[i] = args[j];
args[j] = temp;
}
}
}
for(String s : args){
System.out.println(s);
}
}
它的选择排序。
答案 2 :(得分:0)
由于目标最有可能证明您了解排序算法,因此您应该从这里开始。查找一些基本的排序算法并选择一个来实现。
对于比较,在Java中,您有String.compareTo(String o)
。该方法的功能如下。
String s = "hello", o = "goodbye";
if(s.compareTo(o) < 0) {
//s is less than o (alphabetically before).
} else if (s.compareTo(o) == 0) {
//the are equal
} else if (s.compareTo(o) > 0) {
// s is greater than o (alphabetically after).
}
请参阅https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)
您可以使用此选项与排序算法一起对您可能拥有的任何字符串列表进行排序。出于学习目的,您可能希望从简单的排序开始,例如冒泡,选择或插入排序。如果你谷歌“选择排序Java”,你应该找到一些很好的教程。
答案 3 :(得分:0)