【问题标题】:I don't know how to work with arrays for an average letter program我不知道如何处理普通字母程序的数组
【发布时间】:2013-10-20 05:01:31
【问题描述】:

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


}



}

【问题讨论】:

  • 让我们从语法错误开始: (1) 顶部是否有“public class SomeName {”字样? (2) 在getAverage 的开始之后,你有一个额外的{。这将立即导致很多错误。 (3)int letter[] = new int[] { letterOne, ...int 之后需要方括号。对lettersArray 使用相同的语法。 (4)getAverage被声明为返回一个int,所以你需要在return之后放置一些整数变量或表达式。 (5) char max = letters[0] 需要;。这不会解决所有问题,但会让你更接近编译。

标签: java arrays ascii


【解决方案1】:

1:ASCII 值。

在 Java 中,char 包含一个 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 中创建一个数组

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

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

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

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

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

【讨论】:

    【解决方案2】:

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

    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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2021-04-20
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多