我正在尝试制作一个程序,它可以让我输入 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.");