Java 字符串数组操作
该数组是由25个可能的元素创建的,并且仅当用户输入一个字符串放入数组时才会填充该数组。除了方法中的主体之外,所有代码都是 据我所知Java 字符串数组操作,java,arrays,Java,Arrays,该数组是由25个可能的元素创建的,并且仅当用户输入一个字符串放入数组时才会填充该数组。除了方法中的主体之外,所有代码都是 据我所知 addFlower方法工作正常 sortFlowers方法工作正常 searchFlowers方法工作正常 removeFlower方法完全把我弄糊涂了。如果您能为我们提供任何帮助,我们将不胜感激 displayFlowers方法是当前的问题 我得到的输出在底部。我不明白为什么输出是这样的 import java.util.Scanner; public cl
方法工作正常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
import java.util.Scanner;
公共类分配01驱动程序
{
公共静态void main(字符串[]args)
{
新的Assignment01Driver();
}
//这将作为我们的节目总机
公共分配01驱动程序()
{
扫描仪输入=新扫描仪(System.in);
字符串[]flowerPack=新字符串[25];
System.out.println(“欢迎来到我的花包界面”);
System.out.println(“请从下面的选项中选择一个数字”);
System.out.println(“”);
while(true)
{
//为用户提供他们的选项列表
System.out.println(“1:将项目添加到包中”);
System.out.println(“2:从包中移除一个项目”);
System.out.println(“3:对包中的内容进行排序”);
System.out.println(“4:搜索一朵花”);
System.out.println(“5:显示包装中的花”);
System.out.println(“0:退出花卉包装接口”);
//获取用户输入
int userChoice=input.nextInt();
开关(用户选择)
{
案例1:
addFlower(花卉包装);
打破
案例2:
摘花(花卉包装);
打破
案例3:
酸模花;
打破
案例4:
探索花(花卉包装);
打破
案例5:
展示花卉(花卉包装);
打破
案例0:
System.out.println(“感谢您使用flower pack界面,再见!”);
系统出口(0);
}
}
}
私人花卉(串花套[])
{
字符串str;
int指数=0;
扫描仪输入=新扫描仪(System.in);
System.out.println(“您添加的是什么类型的花?”);
str=input.nextLine();
对于(int i=0;i0)
{
currentMin=flowerPack[j];
currentMinIndex=j;
}
}
}
如果(currentMinIndex!=i){
flowerPack[currentMinIndex]=flowerPack[i];
flowerPack[i]=当前最小值;
}
}
}
私人花卉(串花套[])
{
扫描仪输入=新扫描仪(System.in);
字符串str;
System.out.println(“您想搜索什么花?”);
str=input.nextLine();
布尔值=false;
对于(int i=0;i 1,“Lilly”您必须在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;
}
}
}
int count=1;
对于(int i=0;i
int count = 1;
for (int i = 0; i < flowerPack.length -1 && flowerPack[i] != null; i++)
{
if (flowerPack[i].equalsIgnoreCase(flowerPack[i+1]))
{
count++;
}
else
{
System.out.println(flowerPack[i] + "s - " + count);
count = 1;
}
}
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();
boolean del=false;
for (int i = 0; i < flowerPack.length - 1; i++)
{
if (flr.equalsIgnoreCase(flowerPack[i]))
{
del = true;
}
if (del)
{
flowerPack[i] = flowerPack[i + 1];
}
if (flowerPack[i] == null) {
break;
}
}
}