Java字符数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22278454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java char array
提问by user3397631
I have to select one of the following objectives:
我必须选择以下目标之一:
//1. Make a parallel array of the number of correct answers. Send the method //answers and the answerKey. Return the filled parallel array that contains //the number correct.
//1. 制作一个正确答案数量的平行数组。发送方法 //answers 和 answerKey。返回包含//正确数字的填充并行数组。
correct = correctAnswer(answers, answerKey);
正确=正确答案(答案,answerKey);
//2. Make a parallel array containing students' letter grades. Send correct
//and return the parallel array.
//2. 制作一个包含学生字母成绩的平行数组。发送正确的
// 并返回并行数组。
grade= getGrade(correct);
等级= getGrade(正确);
//3. Count how many of each grade
//3. 计算每个年级有多少
numOfEach=count(grade);
numOfEach=计数(等级);
import java.io.*;
public class template
{
public static void main(String[] args) throws IOException
{
//creating objects and variables
int lineCount;
//count how many lines in the file
lineCount = countLines(counter);
String answerKey;
String[] studentID = new String[lineCount];//store student id
String[] answers = new String[lineCount];//store student answers
int[] correct = new int[lineCount];//store number they got correct
char[] grade = new char[lineCount];//store letter grade
char[] key = new char[10];//holds answer key
char[] numOfEach=new char[5];//hold number of each grade
//initialize file
initFile()
//Page 323 tells us that if I pass arrays the actual array will be modified so I can call a //method to read in the entire file
readFile(answers,studentID,key,lineCount);
//make parallel array of number of correct answers. Send the methods answers and // //answerKey return the filled parallel array containing the number correct.
correct= correctAnswer(answers, answerKey);
//Make a parallel array containing student's letter grade. Send correct and return parallel //array.
grade= getGrade(correct);
//Count how many of each grade
numOfEach=count(grade);
//Output the desired results
outputResults(studentID, correct, grade);
}
}
I decided to select the last one "//3. Count how many of each grade
我决定选最后一个"//3. 数一数每个年级有多少
numOfEach=count(grade);"
numOfEach=count(grade);"
I'm kind of stuck, so far I came up with this:
我有点卡住了,到目前为止我想出了这个:
public static void count(char[] grade){
int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;
for (int i = 0; i < grade.length; i++){
if (grade[i] == 'A'){
numofa++;
}else if (grade[i] == 'B'){
numofb++;
}else if (grade[i] == 'C'){
numofc++;
}else if (grade[i] == 'D'){
numofd++;
}else if (grade[i] == 'F'){
numoff++;
}
}
}
However, I don't know how to populate the numOfEach char array with the data I analyze (How many of each letter grade)
但是,我不知道如何用我分析的数据填充 numOfEach 字符数组(每个字母等级有多少)
采纳答案by aliteralmind
Instead of
代替
public static void count(char[] grade) {
int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;
for (int i = 0; i < grade.length; i++){
if (grade[i] == 'A'){
numofa++;
}else if (grade[i] == 'B'){
numofb++;
}else if (grade[i] == 'C'){
numofc++;
}else if (grade[i] == 'D'){
numofd++;
}else if (grade[i] == 'F'){
numoff++;
}
}
}
Use
用
public static int[] count(char[] grade) {
int[] numOfEach = new int[5]; //5: a, b, c, d, f
int idxA = 0;
int idxB = 1;
int idxC = 2;
int idxD = 3;
int idxF = 4;
for (int i = 0; i < grade.length; i++){
if (grade[i] == 'A'){
numOfEach[idxA]++;
}else if (grade[i] == 'B'){
numOfEach[idxB]++;
}else if (grade[i] == 'C'){
numOfEach[idxC]++;
}else if (grade[i] == 'D'){
numOfEach[idxD]++;
}else if (grade[i] == 'F'){
numOfEach[idxF]++;
}
}
return numOfEach;
}
You cannot hold the amountof each grade in a char
array.
您不能在数组中保存每个等级的数量char
。
Well...you could, but I do not understand the purpose of doing so:
嗯...你可以,但我不明白这样做的目的:
public static char[] count(char[] grade) {
char[] numOfEachAsChars = new char[5]; //5: a, b, c, d, f
int idxA = 0;
int idxB = 1;
int idxC = 2;
int idxD = 3;
int idxF = 4;
for (int i = 0; i < grade.length; i++){
if (grade[i] == 'A'){
numOfEachAsChars[idxA]++;
}else if (grade[i] == 'B'){
numOfEachAsChars[idxB]++;
}else if (grade[i] == 'C'){
numOfEachAsChars[idxC]++;
}else if (grade[i] == 'D'){
numOfEachAsChars[idxD]++;
}else if (grade[i] == 'F'){
numOfEachAsChars[idxF]++;
}
}
return numOfEachAsChars;
}
As you can see in the below example, the raw-numeric value of the character 1
is notone, so I don't recommend the char-array-returning function.
正如你可以在下面的例子中看到的,人物的原始数值1
是不是一个,所以我不建议的字符数组,返回功能。
/**
<P>{@code java RawNumericValueOfChar}</P>
**/
public class RawNumericValueOfChar {
public static final void main(String[] ignored) {
int intValueOfCharOne = '1';
int intValueOfCharALower = 'a';
int intValueOfCharAUpper = 'A';
System.out.println("intValueOfCharOne=" + intValueOfCharOne);
System.out.println("intValueOfCharALower=" + intValueOfCharALower);
System.out.println("intValueOfCharAUpper=" + intValueOfCharAUpper);
int intValueOfCharOneInc = ((int)'1') + 1;
int intValueOfCharALowerInc = ((int)'a') + 1;
int intValueOfCharAUpperInc = ((int)'A') + 1;
System.out.println("intValueOfCharOneInc=" + intValueOfCharOneInc);
System.out.println("intValueOfCharALowerInc=" + intValueOfCharALowerInc);
System.out.println("intValueOfCharAUpperInc=" + intValueOfCharAUpperInc);
}
}
Output:
输出:
[C:\java_code\]java RawNumericValueOfChar
intValueOfCharOne=49
intValueOfCharALower=97
intValueOfCharAUpper=65
intValueOfCharOneInc=50
intValueOfCharALowerInc=98
intValueOfCharAUpperInc=66
Here is some information that may be helpful:
以下是一些可能有用的信息:
回答by AdityaPande
First of all numOfEach should be an int array and not a char array
首先 numOfEach 应该是一个 int 数组而不是一个 char 数组
You can write count function as :
您可以将计数函数编写为:
public static void count(char[] grade,int []count)//changed
{
int numofa = 0, numofb = 0, numofc = 0, numofd = 0, numoff = 0;
for (int i = 0; i < grade.length; i++){
if (grade[i] == 'A'){
numofa++;
}else if (grade[i] == 'B'){
numofb++;
}else if (grade[i] == 'C'){
numofc++;
}else if (grade[i] == 'D'){
numofd++;
}else if (grade[i] == 'F'){
numoff++;
}
}
//changed
count[0]=numofa;
count[1]=numofb;
count[2]=numofc;
count[3]=numofd;
count[4]=numoff;
}
and the call to this function would be like
对这个函数的调用就像
count(grade,numOfEach);