【发布时间】:2013-10-11 22:53:36
【问题描述】:
我正在尝试制作一个程序,它可以让我输入 10 个字符并将它们存储在一个数组中。 只需单个字符就足够了,例如 (d, s, a, e, h, j, e,)。然后让我使用线性搜索算法查找其中一个字符并给出数组中的位置。
我尝试对其进行编程,但我只能使用整数来实现。 到目前为止,这是我的代码。
不知道怎么改成字母/字符?
public static void main(String args[])
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " Letters");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("What letter do you want to find?");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */
{
System.out.println(search + " is present at location " + (c + 1) + ".");
break;
}
if (c == n) /* Searching element is absent */
System.out.println(search + " Letter is not found.");
【问题讨论】:
-
请务必标出对您有帮助的答案,如果您觉得效果最好,请接受。这有助于我们所有人都能找到和使用好的答案。谢谢!
标签: java arrays algorithm linear-search