简体   繁体   English

如何让数组互相交互?

[英]How do I make arrays interact with each other?

I'm having trouble with a school assignment: 我在完成学业任务时遇到了麻烦:

Write a program to create two arrays namely asciiArray and decimalArray of 52 elements each. 编写一个程序来创建两个数组,即asciiArray和decimalArray,每个数组包含52个元素。 In the asciiArray, store letters of the English alphabet; 在asciiArray中,存储英文字母的字母; both lower and upper case letters. 小写和大写字母。 In the decimalArray, store the corresponding decimal values of each of the letters in the specific position in the asciiArray. 在decimalArray中,将每个字母的相应十进制值存储在asciiArray中的特定位置。 For example, If asciiArray[0] holds 'A' then decimalArray[0] will hold the value 65. Pass these arrays to a method displayDecValue. 例如,如果asciiArray [0]保持'A',则decimalArray [0]将保持值65.将这些数组传递给方法displayDecValue。 Inside the method, prompt the user to enter any of the letters of the English alphabet and display the corresponding decimal value. 在方法内,提示用户输入英文字母的任何字母并显示相应的十进制值。

I have some of the coding down, but I do not know how to make the arrays interact with each other and return the value. 我有一些编码,但我不知道如何使数组相互交互并返回值。 Sorry if this is simple; 对不起,如果这很简单; I have never done Java before. 我以前从未做过Java。 We also can't use anything advanced to write the code (it's beginner's Java). 我们也不能使用任何高级编写代码(它是初学者的Java)。

import java.util.Scanner;

public class ParallelArrays {
    public static void main (String [] args) {
        char[] asciiArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
                              'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                              'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                              'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                              'W', 'X', 'Y', 'Z' };
        int[] decimalArray = {97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,
                              114,115,116,117,118,119,120,121,122,65,66,67,68,69,70,71,72,73,74,
                              75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90};
        displayDecValue(asciiArray, decimalArray);
    }

    public static void displayDecValue(char [ ] ascii, int [ ] dec) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a letter (a-z or A-Z): ");

        ascii = input.next().charAt(0);

        dec[0] = (int)ascii[0];

        System.out.printf("Decimal value of %c is: " + dec[0], ascii);
    }
}

Following are things which you should wonder 以下是你应该想知道的事情

1) You are storing the input from the user into array variable ascii. 1)您正在将用户的输入存储到数组变量ascii中。 What would happen here? 这会发生什么?

2) You are reassigning predefined dec array at index position 0, with ascii[0]. 2)您使用ascii [0]在索引位置0重新分配预定义的dec数组。 What does this mean? 这是什么意思?

3) How would you search for the char in the ascii array(Hint : loop) and match it with dec array? 3)如何在ascii数组(提示:循环)中搜索char并将其与dec数组匹配?

4) Is the print statement following C or Java syntax? 4)print语句是否遵循C或Java语法?

Then you will be able to fix the code easily. 然后,您将能够轻松修复代码。

You no need to manually feed decimal array values. 您无需手动输入十进制数组值。 you can do something like below. 你可以做类似下面的事情。 if we convert or typecast char to int we will get decimal value of that character. 如果我们将char转换或类型转换为int,我们将得到该字符的十进制值。

eg int output= (int)'a'; 例如int output= (int)'a'; // here output is 97 that equivalent to decimal value of character 'a'. //这里的输出是97,相当于字符'a'的十进制值。

    int[] decimalArray = new int[asciiArray.length];
    for (int i = 0; i < asciiArray.length; i++) {
        decimalArray[i] = (int)asciiArray[i];
    }

the complete example is below. 完整的例子如下。

import java.util.Scanner;

public class ParallelArrays {
    public static void main(String[] args) {
        char[] asciiArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X', 'Y', 'Z' };

        int[] decimalArray = new int[asciiArray.length];
        for (int i = 0; i < asciiArray.length; i++) {
            decimalArray[i] = (int)asciiArray[i];
        }
        displayDecValue(asciiArray, decimalArray);
    }

    public static void displayDecValue(char[] ascii, int[] dec) {
        Scanner input = new Scanner(System.in);

        System.out.print("Enter a letter (a-z or A-Z): ");

        char inputChar = input.next().charAt(0);

        for (int i = 0; i < ascii.length; i++) {

            if (inputChar == ascii[i]) {
                System.out.printf("Decimal value of '" + ascii[i] + "' is: "
                        + dec[0]);
                break;
            }

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM