我得到一道题目,要求输入4个英文字母,左右一个简单的string,然后将他变换成char,再改为int,然后将之平方,再变成char。 比如输入“abcd”,运行后就会得到“kjih”。我尝试自己写了java,但只写到一半
import javax.swing.JOptionPane;
  public class Assignment2 {
  public static void main(String[] args) {
     String word =
       JOptionPane.showInputDialog("Enter your four-letter word: ");
char[] code = word.toCharArray();int code2 = (int)code;  \\问题就是这里System.out.print(code2);
}
}说这个是不可转换的类型,这个应该怎样解决?以防我理解错误,原题在这~~
Write a Java program that encrypts a four-letter word. The word should be entered by the
user as a single string using the JOptionPane class. Each letter in the word should be
replaced by the Unicode character that is seven characters after it, and then the sequence of
letters should be reversed. The encrypted word should then be displayed in the terminal
window. For example, the string “abcd” is encoded as “kjih” and the string “java” is encoded as
“h}hq”.

解决方案 »

  1.   

    code变量是char[] 不是char 
      

  2.   

    你怎么能把一个char数组直接向一个int 变量里放呢?
      

  3.   

    那我应该如何从String转变成char,再改为int呢?
      

  4.   

    字符串转化为char数组,楼主应该会吧, 那char[]数组,应该转化为int数组吧?
    比如:
    char ch[] = {'a','b','c'};
    int[] arr = new arr[ch.length];
    for(int i = 0; i< ch.length; i++){
          arr[ i ] = Integer.parsetInt(String.valueOf(ch[i]));
    }
      

  5.   

    Each letter in the word should be 
    replaced by the Unicode character that is seven characters after it
    条件和你给的数也不符合啊,而且每有分,不做了(这个java api中没有转换的,找半天也没找到)
      

  6.   

    abcd应该对应的是hijk啊,你怎么给的还是个反得;我做的程序输出的是hijk你可以反向输出就是你那个,不过题目没让你反向输出,郁闷,误导我。做出来了,你给分,我发代码
      

  7.   

    题目有要求将字母反过来吧~“and then the sequence of letters should be reversed”,这个,应该是~还有要怎样给分?刚来这论坛~~
      

  8.   

    很明显我刚开始看这题的时候理解错了,应该是要用JOptionPane class输入4个字母,然后将其变为Unicode character,之后再+7,最后将得到的字母反过来输出,比如输入“abcd”,就得到“kjih” 应该是这样才对~~
      

  9.   

    一天没消息,不难为你了,本来就是个挺简单的题,我不过时想得点分然后问问题
    这次当是给新手的免费解答吧, public static void main(String args[]) {
         char[] code2;
         char a;
          String word = 
               JOptionPane.showInputDialog("Enter your four-letter word: "); 
        
         code2=word.toCharArray() ;
        
              for(int i:code2)
               {i+=7;
         System.out.println((char)i);
          }
        

        }