-1

我需要使用 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);

我正在尝试将这封信作为 ASCII 数字存储在这里

   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

    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);


}



}
4

2 回答 2

0

1:ASCII 值。

在 Java 中,achar包含一个 ascii,又名:数字,又名:整数,值,但是,当您尝试打印它时,将显示由该值表示的字符。

    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:我不知道如何制作一个数组以在 main 中使用

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

3:不知道在这里返回什么。

你在你的方法声明中说你会返回一个int,你该死的更好,否则Java会生气。

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

在这篇文章的第一部分查看我的代码。

于 2013-10-12T02:18:10.863 回答
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

参考:

从扫描仪获取字符输入

http://www.asciitable.com/

于 2013-10-12T02:39:24.217 回答