gpt4 book ai didi

java - 字符串数组操作

转载 作者:行者123 更新时间:2023-12-01 06:23:18 24 4
gpt4 key购买 nike

该数组创建时包含 25 个可能的元素,并且仅当用户输入要放入数组中的字符串时才会填充该数组。除方法内的主体之外的所有代码都是 provided .

据我所知

  • addFlower方法运行正常
  • sortFlowers方法运行正常
  • searchFlowers方法运行正常

removeFlower方法让我完全困惑。任何有关前往这里的方向的帮助将不胜感激。

displayFlowers方法是目前的问题。

我得到的输出位于底部。我不明白为什么输出是这样的。

import java.util.Scanner;

public class Assignment01Driver
{
public static void main(String[] args)
{
new Assignment01Driver();
}

// This will act as our program switchboard
public Assignment01Driver()
{
Scanner input = new Scanner(System.in);
String[] flowerPack = new String[25];

System.out.println("Welcome to my flower pack interface.");
System.out.println("Please select a number from the options below");
System.out.println("");

while (true)
{
// Give the user a list of their options
System.out.println("1: Add an item to the pack.");
System.out.println("2: Remove an item from the pack.");
System.out.println("3: Sort the contents of the pack.");
System.out.println("4: Search for a flower.");
System.out.println("5: Display the flowers in the pack.");
System.out.println("0: Exit the flower pack interfact.");

// Get the user input
int userChoice = input.nextInt();

switch (userChoice)
{
case 1:
addFlower(flowerPack);
break;
case 2:
removeFlower(flowerPack);
break;
case 3:
sortFlowers(flowerPack);
break;
case 4:
searchFlowers(flowerPack);
break;
case 5:
displayFlowers(flowerPack);
break;
case 0:
System.out.println("Thank you for using the flower pack interface. See you again soon!");
System.exit(0);
}
}
}

private void addFlower(String flowerPack[])
{
String str;
int index = 0;
Scanner input = new Scanner(System.in);
System.out.println("What type of flower are you adding?");
str = input.nextLine();

for (int i = 0; i < flowerPack.length; i++)
{
if (flowerPack[i] != null)
{
index++;
if (index == flowerPack.length)
{
System.out.println("The pack is full");
}
} else
{
flowerPack[i] = str;
System.out.println("Added: " + str + " at index " + i);
break;
}
}
}

private void removeFlower(String flowerPack[])
{
String flr;
Scanner input = new Scanner(System.in);
System.out.println("What flower do you want to remove?");
flr = input.nextLine();

for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flr.equalsIgnoreCase(flowerPack[i]))
{
flowerPack[i] = flowerPack[i + 1];
}
}
}

private void sortFlowers(String flowerPack[])
{
for (int i = 0; i < flowerPack.length; i++)
{
String currentMin = flowerPack[i];
int currentMinIndex = i;

for (int j = i; j < flowerPack.length; j++)
{
if (flowerPack[j] != null)
{
if (currentMin.compareToIgnoreCase(flowerPack[j]) > 0)
{
currentMin = flowerPack[j];
currentMinIndex = j;
}
}
}
if (currentMinIndex != i) {
flowerPack[currentMinIndex] = flowerPack[i];
flowerPack[i] = currentMin;
}
}
}

private void searchFlowers(String flowerPack[])
{
Scanner input = new Scanner(System.in);
String str;
System.out.println("What flower would you like to search for?");
str = input.nextLine();
boolean found = false;

for (int i = 0; i < flowerPack.length; i++) {
if (flowerPack[i] != null && flowerPack[i].equalsIgnoreCase(str))
{
found = true;
break;
}
}
if (found)
{
System.out.println("We found your flower.");
}
else
{
System.out.println("That flower was not found.");
}
}

private void displayFlowers(String flowerPack[])
{
sortFlowers(flowerPack);
int count = 1;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flowerPack[i] != null)
{
if (flowerPack[i].equalsIgnoreCase(flowerPack[i+1]))
{
count++;
}
}
else
{
if (flowerPack[i] == null)
{
break;
}
}
System.out.println(flowerPack[i] + "s - " + count);
count = 1;
}
}
}


Input:
1 <enter>, "Lilly" <enter"
1 <enter>, "Lilly" <enter>
1 <enter>, "Rose" <enter>
5 <enter>

Output:
Lillys - 2
Lillys - 1
Roses - 1

最佳答案

您必须在 for 循环之外声明 i

int count = 1;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flowerPack[i] != null)
{
if (flowerPack[i].equalsIgnoreCase(flowerPack[i+1]))
{
count++;
}
else
{
System.out.println(flowerPack[i] + "s - " + count);
count = 1;
}
}
else
{
if (flowerPack[i] == null)
{
break;
}
}
}

关于java - 字符串数组操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35175376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com