【问题标题】:JAVA Replacing a char in a string with a int from a keyboard inputJAVA用键盘输入中的int替换字符串中的char
【发布时间】:2021-02-04 16:05:43
【问题描述】:

我是一名大学新生,攻读软件工程学位。 所以我在java课程中有这个作业,我必须加密一个6位密码并将其显示给用户,其中一个步骤是 用第二个数字除以 2 的余数替换第二个数字。 我尝试使用 replace 方法来做,但它给了我一个错误,说我不能用 int 替换 char,这是我的代码:

public class Assignment1 {

    public static void main(String[] args) {
        System.out.println("Welcome to the Password encrypter. ");
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter a 6-digits passsword: ");
        final String password = keyboard.nextLine();
        System.out.println("encrypting your password:");
        char[] c = password.toCharArray(); 
        char temp = c[0];
        c[0] = c[5];
        c[5] = temp;
        String password1 = new String(c);
        char char2 = password1.charAt(1);
        int remainder = char2 % 2;
        String password2 = password1.replace(char2, remainder);
    }

}

(如果有人感兴趣,这里是完整的练习: 问题 1 - 密码加密(4 分) 编写一个完整的程序,要求用户提示一个 6 位数字并输出 加密密码。 你的程序应该表现如下:

  1. 显示欢迎消息。
  2. 询问用户输入的数字。
  3. 在程序中使用常量变量来存储输入值。
  4. 显示加密后的数字,相当于
  1. 交换第 1 位和第 6 位数字
  2. 用第二个数字除以 2 的余数替换第二个数字。
  3. 将输入的第三位加 1。请注意,如果第三个数字是“9”,则第三个数字将 步骤 3) 后为“0”,步骤 3) 中的其他数字没有进一步变化。暗示: 使用步骤 3) 之后的第三个数字的余数除以 10 来处理可能的 溢出。
  4. 将第四位数字替换为第三位数字除以 3 的余数。
  5. 交换第 4 位和第 5 位数字。 )

【问题讨论】:

  • 在构造新字符串之前替换c 数组中的第二个字符。不要尝试在字符串中替换它,因为这将替换所有出现的相同数字。
  • 另外,String#replace(...) 替换所有匹配的子字符串,而不仅仅是那个字符。自己阅读 Javadoc docs.oracle.com/javase/7/docs/api/java/lang/…
  • 既然你正在学习,我建议你总是去 Javadoc 寻找你想使用的类方法,然后弄清楚它的作用。不要只看名字。我之前的评论应该已经教会了你这一课。

标签: java


【解决方案1】:

您必须将int 转换为char。这两种数据类型不兼容,主要是因为ints是32位宽,chars只有16位宽。要解决此问题,您必须先将 int 转换为 char,然后执行相反的操作。

另外,在创建新的 String 之前对 char 数组进行所有操作

int digit = c[1];
char remainder = (char)(digit % 2);
c[1] = remainder;
// Add 1 to the third digit of your input
// Replace the fourth digit with the remainder of the third digit divided by 3.
// Swap the 4th and 5th digits.
String password = new String(c);

更新

在 OP 对我的帖子发表评论后,我意识到我的答案没有根据原始问题陈述正确构建。

现在的假设是您以包含 NUMERIC 字符(即“123456”)的String 开头。然后,根据指令操作这些数字中的每一个。因此,执行第 2 步时,结果必须是 NUMERIC 值 0 或必须将 1 添加到 char 数组索引。

如果以上正确,那么解决方法很简单:

System.out.println("Enter a 6-digits passsword: ");
final String password = keyboard.nextLine(); // assume "123456"
System.out.println("encrypting your password:");
char[] c = password.toCharArray(); // {'1', '2', '3', '4', '5', '6'} 

// swap first and last characters
char temp = c[0];
c[0] = c[5];
c[5] = temp; // c = {'6', '2', '3', '4', '5', '1'}

// Replace the second digit with the remainder of the second digit divided by 2
int digit = Integer.parseInt(s.substring(2, 3)); // Takes second digit and converts it to a number
c[1] = String.valueOf(digit % 2).charAt(0); // calculate mod 2 and converts it to a char --> c = {'6', '0', '3', '4', '5', '1'}

必须遵循类似的方法来操作数组中的剩余字符。完成所有步骤后,您可以简单地基于char 数组创建一个新的String。有两种方法可以做到这一点:

String encodedPwd = String.valueOf(c); // recommended
String encodedPwd = new String(c); // Discouraged

【讨论】:

  • 非常感谢您的帮助,我做到了,没有错误,但我尝试显示它只是为了看看它是否有效,我注意到一个问题,如果余数为 0,它会留下一个空白如果它是 1,它会显示一个带有问号的框,你知道我该如何解决这个问题吗?谢谢。
  • 我知道...也许我误解了您最初的问题。当您转换一个数字字符串(即“123456”)并将其转换为 char 数组时,您最终会得到 '1'、'2'、'3' 等。如果您随后将这些 chars 转换为将它们转换为整数,您将得到该字符的 ASCII 值,而不是数值。我会稍微修改我的答案。
  • @oussama 如果它可以帮助你考虑支持它
  • @oussama 在编写实际代码之前进行伪编码对您学习有很大帮助。即使对于我已经这样做了将近 20 年,我也经常用简单的英语写下这个过程应该做什么,然后我把这个叙述写成代码。另外,您看到我将数组值放入 cmets 中做了什么吗?它有助于写下每个步骤的预期结果,以便在调试代码时,可以将预期结果与实际结果进行比较。每次都这样做,你不会后悔的。相信我。
猜你喜欢
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 2011-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 2015-05-01
相关资源
最近更新 更多