我正在编写一个程序,帮助用户制作Top X或Top favorites列表。
用户将首先输入列表中的项目数,然后用于创建相同大小的数组。然后用户填充数组。最后,用户会被问到一系列问题,程序会将列表中的每个项目与列表中的另一个项目逐一进行比较,并在两者之间询问你更喜欢哪一个。根据他们的选择,他们的分数保存在另一个存储整数值的数组中。然后按降序进行排序。
然而,当我运行程序时,它不会将所有元素与每个元素进行比较。
例如,我有一个三个元素长的数组,项目是"香草"、"巧克力"one_answers"草莓"。它会要求我将香草与巧克力进行比较,然后将香草与草莓进行比较。然而,它不会将巧克力与草莓进行比较,然后打印结果。我想知道我犯了什么逻辑错误。我从未对字符串使用过冒泡搜索。
这是我的代码供参考:
import java.util.Scanner;
public class FavoriteListMaker {
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
int ListSize = inputDevice.nextInt();
inputDevice.nextLine();
String [] TopXA = new String [ListSize];
int [] TopXB = new int[ListSize];
for (int x = 0; x < TopXA.length; ++ x)
{
System.out.println("Please enter an item to be organized on the list");
TopXA[x] = inputDevice.nextLine();
System.out.println("You have " + (ListSize - x - 1) + " items left to fill on the list.");
}
System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
System.out.println("At the end the item with the most points wins.");
int comparisonsToMake = TopXA.length - 1;
for (int y = 0; y < TopXA.length - 1; ++ y)
{
for (int z = 0; z < comparisonsToMake; ++ z)
{
if(TopXA[y] != TopXA[z + 1])
{
String compareA = TopXA[y];
String compareB = TopXA[z + 1];
System.out.println("Do you prefer " + compareA + " or " + compareB + " .");
System.out.println("If you prefer " + compareA + " Please press 1. If you prefer " + compareB + " please press 2.");
int choice = inputDevice.nextInt();
inputDevice.nextLine();
switch(choice)
{
case 1:
TopXB[y] =+ 1;
break;
case 2:
TopXB[z + 1] =+ 1;
break;
default:
System.out.print("I'm sorry but that is not a valid input.");
}
}
}
--comparisonsToMake;
}
int comparisonsToMakeB = TopXB.length - 1;
for(int a = 0; a < TopXB.length - 1; ++ a)
{
for(int b = 0; b < comparisonsToMakeB; ++b)
{
if(TopXB[b] < TopXB[b + 1])
{
String temp = TopXA[b];
TopXA[b] = TopXA[b + 1];
TopXA[b + 1] = temp;
}
}
--comparisonsToMakeB;
}
for(int q = 0; q < TopXA.length; ++ q)
{
System.out.print("Your number " + (q + 1) + " pick is " + TopXA[q] + ".");
}
}
}
没关系,我找到了答案。。。。其实很简单。我只是简单地将z的所有特定实例更改为y。
int comparisonsToMake = TopXA.length - 1;
for (int y = 0; y < TopXA.length - 1; ++ y)
{
for (int z = 0; z < comparisonsToMake; ++ z)
{
if(TopXA[y] != TopXA[z + 1]) // changed TopXA[z + 1] to [y + 1]
{
String compareA = TopXA[y];
String compareB = TopXA[y + 1]; //instance of change
System.out.println("Do you prefer " + compareA + " or " + compareB + " .");
System.out.println("If you prefer " + compareA + " Please press 1. If you prefer " + compareB + " please press 2.");
int choice = inputDevice.nextInt();
inputDevice.nextLine();
switch(choice)
{
case 1:
TopXB[y] =+ 1;
break;
case 2:
TopXB[y + 1] =+ 1; //instance of change.
break;
default:
System.out.print("I'm sorry but that is not a valid input.");
}
}
}
--comparisonsToMake;
}
老实说,我不知道我为什么要做出这样的改变。可能是因为我累了,昨晚正在做这件事,并且在思考一些疯狂的巨魔逻辑。