2014-04-05 106 views
0

我正在制作一个加密程序,用户输入一个单词,然后模式然后将拼字的单词。我不明白这种方法错误

这里是我的错误:

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

这里是我的代码:

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)); 
    } 
    } 
    } 
} 
+0

您是否在代码中的任何位置创建了一个名为'charArray'的方法? –

+0

它不是我用它来发送数组 – user3331406

回答

0

错误似乎发生,因为没有一个在你Cipher类称为charArray方法。我怀疑你真正想要的是从你的代码中早先定义的名为charArray的数组中获取元素。因此,语法应该是charArray[i]以获取索引i处的元素。

+0

所以切换chararray密码(这是我的方法名称) – user3331406

+0

我不会跟着你。你想要的输出是什么? –

+0

确定shree.pat18你说的没有工作,但我如何运行该程序,以便如果输入的模式是1,因此每输入一个词就只能添加一个词。 我想你说什么,我进入为hello字 然后图案1,但它给了我4个字母,每1 HadeEcdbLaebLbagObbg - > HELLO – user3331406

0

是的,这里没有定义charArray(int)方法。

当您使用要打印charArray[i]你不小心写charArray(i)

而是在i它正试图执行一个不存在的方法位置访问值。只需将()括号更改为[]即可,您将看到您的错误消失。

+0

是的,我做到了,但现在我在程序运行的地方有问题,但它输出4个乱码字母或eevry 1这就是用户输入的单词 – user3331406

0

当你写它像charArray(i)它会尝试调用一个名为charArray

的方法传递i作为参数传递给它(这不是你的情况实际上被定义),而当你说charArray[i]它会知道你是想访问的charArray

i个元素,所以你需要改变

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

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