简体   繁体   English

我不明白此方法错误

[英]I dont understand this method error

I am making a ciphering program where user enters a word then a pattern then is will scrabble the word. 我正在编写一个加密程序,用户在其中输入一个单词,然后一个模式便会拼写该单词。

here is my error: 这是我的错误:

 1 error found:
  [line: 42]
Error: The method charArray(int) is undefined for the type Cipher

Here is my code: 这是我的代码:

import java.util.Scanner;

public class Cipher
{
  public static char cipher (int j){
    char[] cipher1 = {'a','b','c','d','e','f','g'};
  j = (int) (Math.random() * cipher1.length);//choose a random element from the array
    return cipher1[j];
  }

  public static void main (String[] args){

    System.out.print("Please type a sentence to be encrypted\n");
    Scanner inputScanner = new Scanner(System.in);
    String input = inputScanner.next();
      System.out.print("please enter");
   input = input.toLowerCase();
    int yu = input.length();
    char[] charArray = input.toCharArray();

        int w=1;
    do{
    try{
    w=2;
      System.out.println("please entrer pattern");
      String hello = inputScanner.next();
      int hello2= Integer.parseInt(hello);
      if(hello2<0){
        System.out.println("please enter proper number");
        w=1;
      }
    }catch (NumberFormatException f){
      System.out.println("please enter proper number");
    }

    }while (w==1);

    System.out.print("your encrypted code is ");

    for(int i = 0; i < yu; i++){
    System.out.print(charArray(i)); //THIS IS WHERE ERROR IS HIGHLIGHITNG
    for(int q = 0; q <= w; q++){
    System.out.print( cipher(1));
    }
    }
  }
}

The error seems to be occurring because there isn't a method called charArray in your Cipher class. 该错误似乎正在发生,因为您的Cipher类中没有名为charArray的方法。 I suspect what you really want is to get the elements from an array called charArray that you have defined earlier in your code. 我怀疑您真正想要的是从代码前面定义的名为charArray的数组中获取元素。 So the syntax should be charArray[i] to get element at index i. 因此,语法应为charArray[i]以获得索引i处的元素。

Yes, the method charArray(int) has not been defined here. 是的,此处尚未定义方法charArray(int)。

When you use are trying to print charArray[i] you have accidentally written charArray(i) 当您尝试打印charArray[i]您不小心写了charArray(i)

Instead of accessing the value at position i it is trying to execute a method that doesn't exist. 而不是访问位置i的值,而是尝试执行一个不存在的方法。 Just change () brackets to [] ones and you will see that your error disappears. 只需将()括号更改为[] ,您会发现错误消失了。

when you write it like charArray(i) it will try to call a method named charArray passing i as argument to it(which is not defined actually in your case) 当您像charArray(i)一样编写它时,它将尝试调用一个名为charArray的方法,将i作为参数传递给它(在您的情况下实际上未定义)

while when you say charArray[i] it will understand that you want to access i th element of charArray 而当您说charArray[i] ,它将理解您要访问charArrayi个元素

so you need to change 所以你需要改变

System.out.print(charArray(i));

to

System.out.print(charArray[i]);

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

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