我正在编写一个帮助用户制作热门X或热门收藏列表的程序。
用户将首先输入列表中的项目数,然后用于创建相同大小的数组。然后用户填充数组。最后,用户被问到一系列问题,其中程序将列表上的每个项目一次一个地与列表上的另一个项目进行比较,并在两个项目之间询问您更喜欢哪个项目。根据他们的选择,他们的分数保存在另一个存储整数值的数组中。然后按降序排序。
当我运行程序时,它不会将所有元素与每个元素进行比较。
例如说我有一个数组长三个元素,其中的项目是“香草”,“巧克力”和“草莓”。它会让我把香草和巧克力,香草和草莓进行比较。然而,它不会比较巧克力和草莓,然后打印结果。我想知道我得到了什么逻辑错误。我从未使用过气泡搜索字符串。
以下是我的参考代码:
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] + ".");
}
}
}
答案 0 :(得分:0)
没关系,我发现答案......实际上很简单。我只是将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;
}
老实说,我不知道为什么要做出这样的改变。可能是因为我累了并且昨晚正在研究这个问题并且正在思考一些疯狂的巨魔逻辑。