我不知道如何使用平均字母程序的数组

时间:2013-10-12 01:46:44

标签: java arrays ascii

我需要使用getAverage方法和main方法创建一个程序,让用户输入五个字符,然后计算/打印平均ASCII值和最高字母,但Eclipse给了我很多错误,我是我不确定我在做什么

   public static int getAverage(char [] ascii, int [] decimal, int [] letters) {
{       

    System.out.println("Enter 5 letters from the English Alphabet: ");
    Scanner input = new Scanner(System.in);

我试图在这里以字母的形式存储信件

   System.out.println("Letter 1 (a-z or A-Z): ");
    char a = (input.next()).charAt(0);
        int letterOne = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == a)
                letterOne=i;
        }
    System.out.println("Letter 2 (a-z or A-Z): ");
    char b = (input.next()).charAt(0);
        int letterTwo = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == b)
                letterTwo=i;
        }
    System.out.println("Letter 3 (a-z or A-Z): ");
    char c = (input.next()).charAt(0);
        int letterThree = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == c)
                letterThree=i;
    }
    System.out.println("Letter 4 (a-z or A-Z): ");
    char d = (input.next()).charAt(0);
        int letterFour = -1;
        for(int i=0; i<ascii.length; i++){
            if (ascii[i] == d)
                letterFour=i;
    }
    System.out.println("Letter 5 (a-z or A-Z): ");
    char e = (input.next()).charAt(0);
        int letterFive = -1;
            for(int i=0; i<ascii.length; i++){
                if (ascii[i] == e)
                    letterFive=i;
    }

我不知道怎么把它们放到我可以在主要使用的阵列中

    int[] letter = new int{letterOne, letterTwo, letterThree, letterFour, letterFive}; 

    int [] lettersArray = {a, b, c, d, e};
    int average = ((a+b+c+d+e)/5);


    System.out.println("Your average value is: " + average);

如果有什么事情不确定该返回此处?

     return ;

}


public static void main(String[] args)
{
    int [] decimalArray = new int[52];
    char[] asciiArray = new char[52];

    int base = 65;

    for (int i=0; i<26;i++){
        decimalArray[i] = base;
        asciiArray[i] = (char) base;
        base++;
    }
    base = 97;
    for(int i = 26; i<52; i++){
        decimalArray[i] = base;
        asciiArray[i] = (char) base;
        base++;
    }

    int [] lettersArray = new int[5];

不确定如何从CHAR变成INT INTE

    int[] letters = new int[5];
    char max = letters[0]
            for(int i = 0; i<5; i++){
                if(max < letters[i])
                    max = letters[i];
            }

    getAverage(asciiArray, decimalArray, lettersArray);

    System.out.println("The highest letter is: " + max);


}



}

2 个答案:

答案 0 :(得分:0)

1:Ascii值。

在Java中,char包含ascii,又名:numeric,aka:integer,value,但是,当您尝试打印它时,将显示该值表示的字符。

    char a = 97;
    System.out.println( a ); // output: a

    char avg = ('a' + 'c') / 2;
    System.out.println( avg ); // output: b

    int b = (int)avg;
    System.out.println( b ); // output: 98

2:我不知道如何在主

中使用数组

主要方法是static,这意味着它不存在首先创建类的实例,并且可以随时调用。您声明的数组不是静态的,main可能不引用数组,因为在创建类的实例之前,数组不存在。使数组静态(自担风险),或使用main创建类的new实例,然后告诉实例该做什么。

3:不知道该返回什么地方。

你在方法声明中说你会返回int,你该死的好,或者Java会生气。

4:不确定如何将char转换为int。

在本文的第一部分中查看我的代码。

答案 1 :(得分:0)

除非您需要访问之前用户输入的值,否则您不一定需要数组。你可以这样做:

import java.util.Scanner;

class ASCII_Average_Calculator {
  public static void main(String []args){
    int n_letters = 5;//the amount of letters you want to capture

    int sum = 0;
    int ascii = 0;//this will hold the ascii value
    double average = 0;

    String letters = new String();

    for (int i = 0; i < n_letters; ++i) {
      System.out.println("Letter " + (i+1) + " (a-z or A-Z):");

      Scanner input = new Scanner(System.in);
      char c = input.next().charAt(0);//see this link for more: https://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner

      ascii = (int)c;//cast the character to an int

      //perform comparisons to see if it is not within the limits for
      //lowercase ascii values: (97 to 122)
      //uppercase ascii values: (65 to 90)
      //and ensure that the value captured is within those limits (i.e. valid for a-z and A-Z)
      while ((!(ascii > 96) && (ascii <= 96 + 26)) ||
          (!(ascii > 64) && (ascii <= 64 + 26))) {
        System.out.println("Invalid entry: \"" + c + "\"! Please try again.");
        System.out.println("Letter " + (i+1) + " (a-z or A-Z):");
        input = new Scanner(System.in);
        c = input.next().charAt(0);
        ascii = (int)c;
      }

      sum += ascii;//increment the sum

      if (i == n_letters - 1) {
        letters += c;
      }
      else {
        letters += c + " , ";
      }
    }

    //finally, compute the average
    average = sum/(double)n_letters;

    System.out.println("The average of the letters: \"" + letters + "\" is " + average);
  }
}

示例输入:

Letter 1 (A-z or A-Z):
a
Letter 2 (A-z or A-Z):
v
Letter 3 (A-z or A-Z):
b
Letter 4 (A-z or A-Z):
x
Letter 5 (A-z or A-Z):
e

输出:

The average of the letters: "a , v , b , x , e" is 106.8

参考文献:

Take a char input from the Scanner

http://www.asciitable.com/