简体   繁体   English

解析用户输入以关联数组值

[英]Parsing user input to correlating array values

I am trying to assign a number to a letter grade that a user inputs. 我正在尝试为用户输入的字母等级分配一个数字。 The user will input a letter such as A, B, or C and then based on what they enter a value is stored in an integer. 用户将输入字母,例如A,B或C,然后根据输入的内容将值存储在整数中。

I figured the easiest way to do this was setup an array such as: 我认为最简单的方法是设置一个数组,例如:

char[] grade = char[] grade = {'A','B','C','D','F'};
grade[0] = 4;
grade[1] = 3;
// ... as so on

So, whenever a user inputs 'A' for their grade, I use the 4 when I need to. 因此,每当用户输入“ A”作为其成绩时,我会在需要时使用4。

I am trying to figure out how to read an input (JOptionPane) and read the letter they enter to the corresponding value I have assigned it. 我试图弄清楚如何读取输入(JOptionPane)并将它们输入的字母读取为我为其分配的相应值。 How do I go about parsing the letter input based on my array? 如何基于数组解析字母输入?

I'm not sure, whether I understood you right: 我不确定我是否理解正确:

int grade (char input) 
{
    return 5 - (input - 'A');
}

Think of it as a graph. 可以将其视为图形。 In computer encoding, Ascii or UTF8, the characters AF are sequentially encoded, with A being the lowest, but not 0 or 1, but 65 or something, which we don't remember exactly. 在计算机编码(Ascii或UTF8)中,字符AF是按顺序编码的,A是最低的,但不是0或1,而是65或类似的东西,我们不记得了。

  5  |               *
  4  |                 *   
  3  |                   * 
  2  |                     * 
  1  |                       *
  0  +-- ... ------------------*----->
                     A B C D E F 
                    65 6 7 8 9 70

Drawing this graph, I mentioned that you jump form D to F - is that intentionally? 绘制此图时,我提到您将表格D跳到F-是故意的吗? If not: 如果不:

If we subtract from 5 the difference from input and 'A', we get 5 - 0 for 'A', and 5 - 1 for 'B' and so on. 如果从5中减去输入和'A'的差,则'A'得到5-0,'B'得到5-1,依此类推。 Since we don't want to look up the number for 'A', we use 'A' directly, which is fine, since we can perform arithmetics on characters. 由于我们不想查找“ A”的数字,因此可以直接使用“ A”,这很好,因为我们可以对字符进行算术运算。

We could as well write 我们最好写

return 70 - input;

or 要么

return 'F' - input;

The standard form of a linear equation is y = mx + n, where n is the cut through the y-axis (70), and m = -1, the gradient, negative in our case. 线性方程的标准形式为y = mx + n,其中n是穿过y轴的切口(70),而m = -1(在本例中为负)。

It might be easier to just cast the character to an int. 仅将字符转换为int可能会更容易。 A char basically has an int value. char基本上具有int值。 Doing this: 这样做:

int i = (char)'A';

will yield 65. For a lower case a it would be 97. You could cast the char to int, then use that value to do bounds checking and some arithmetic. 将产生65.对于较低的情况下, a会是97你可以投的字符为int,然后使用该值做边界检查和一些算术。 Sequential letters will yield sequential integers. 顺序字母将产生顺序整数。 This is safe since you're running on a JVM and don't have to take bizarro character set orders for different platforms into account. 这是安全的,因为您正在JVM上运行,而不必考虑不同平台的奇异字符集顺序。

Apart from that, seeing how you have limited allowed inputs, a map could work well too: 除此之外,看看您如何限制允许的输入,地图也可以很好地工作:

Map<Character, Integer> grades = new HashMap<Character, Integer>();
grades.put('A', 4); //optionally also: grades.put('a' 4);
...

Type params and auto-boxing and unboxing makes this a lot more convenient these days. 键入参数以及自动装箱和拆箱使这些天变得更加方便。

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

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