2016-01-30 67 views
4

我必须编写一个方法来切换字符串的第一个和最后一个字母。例如,字符串“java”会变成“aavj”。在Java中切换字符串的第一个和最后一个字母?

这是我到目前为止有:

import javax.swing.JOptionPane; 

public class printTest { 
    public static void main(String[] args) { 
     String password; 
     password = JOptionPane.showInputDialog("Please input your password"); 
     int length = password.length(); 
     String firstChar = password.substring(0, 1); 
     String lastChar = password.substring(length - 1); 

     password = lastChar + password + firstChar; 
     JOptionPane.showMessageDialog(null, password); 
    } 
} 

与该代码我得到以下输出“ajavaj”当我输入的“java”,所以我怎么能解决这个问题?我需要切换前两个字母,仍然有字符串的中间。我该怎么办?

回答

7

你需要在这条线串密码:

password = lastChar + password.substring(1, length-1) + firstChar; 
+0

太谢谢你了!这工作完美。 – logonin

1
char c[] = password.toCharArray(); 
char temp = c[0] 
c[0] = c[password.length-1]; 
c[password.length-1] = temp; 

你去那里,交换这两个字母。 c[0]将是您的第一个字母,您将它存储在temp变量中,然后修改c[0](您的第一个字母)的值与c[password.length-1](最后一个字母)中的值,然后使用temp中的一个存储修改此值变量

+0

我编辑你的答案,因为声明这样的字符数组是不鼓励的 – Idos

+1

@Idos:我回滚了你的编辑,因为它在事情的宏伟计划中最终没什么问题。 – Makoto

+0

嗯,我想。 cool – Idos

3

通过执行password = lastChar + password + firstChar;您将原始密码字符串与其他两个字符串连接起来,即lastChar & firstChar。通过这样做,你实际上会得到一个新的字符串lastCharfirstChar追加和不交换。

此外,字符串是不可变的,每次你试图操纵它时,你最终都会创建一个新的字符串。您应该使用char阵列来避免此问题。

尝试这段代码:

import javax.swing.JOptionPane; 
public class printTest 
{ 
    public static void main(String[] args) 
    { 
      /* Capture Password */ 
      String password = JOptionPane.showInputDialog("Please input your password"); 
      char[] pass = password.toCharArray(); 

      /* Swap Logic */ 
      char temp = pass[0]; 
      pass[0] = pass[pass.length - 1]; 
      pass[pass.length - 1] = temp; 

      /* Show MessageDialog */ 
      JOptionPane.showMessageDialog(null, new String(pass)); 
    } 
} 
+0

解释为什么你认为这段代码是正确的,以及它为什么起作用。 – Makoto

+0

@Markoto增加了额外的信息。为什么它低调? – user2004685

+0

你对'char'数组的解释没有意义,因为你正在创建一个新的字符串*无论如何*。可以这么说,你正在创建一个更少的String实例,但在这里使用'char'数组并不足够令人信服。 – Makoto

1

你得到的第一个字符的方式是正确的,但不是中间或最后。下面是显示所有这三种情况的解决方案:

String password = "123456789"; 
    int length = password.length(); 

    String firstChar = password.substring(0, 1); 
    String middle = password.substring(1, length - 1); 
    String lastChar = password.substring(length - 1, length); 

    password = lastChar + " " + middle + " " + firstChar; 
    System.out.println(password); 

这将打印:

9 2345678 1 
2

我想这应该做的伎俩:

import javax.swing.JOptionPane; 
    public class printTest 
    { 
    public static void main(String[] args) 
    { 
    String password; 
    password = JOptionPane.showInputDialog("Please input your password"); 
    int length = password.length(); 
     String password_changed = password.substring(1, password.length() - 1); 
     String firstChar = password.substring(0,1); 
     String lastChar = password.substring(length - 1); 



     password = lastChar + password_changed + firstChar; 
     JOptionPane.showMessageDialog(null, password); 
    } 
    } 

您做出额外的字符串(在本例password_changed),以便删除密码变量的第一个和最后一个字母。您可以使用该新变量在最后更改密码变量。

1

并不比其他的答案太大的不同,但另一种方式:

password = password.charAt(password.length() - 1) 
      + password.substring(1, password.length() - 1) 
      + password.charAt(0); 
相关问题