JAVA用键盘输入的int替换字符串中的字符
我是大学一年级的学生,正在攻读软件工程学位。 所以我在java课程中有一个作业,我必须加密一个6位数的密码并显示给用户,其中一个步骤是 用第二个数字的剩余部分除以2替换第二个数字。 我尝试使用replace方法进行替换,但它给了我一个错误,告诉我不能用int替换char,下面是我的代码:JAVA用键盘输入的int替换字符串中的字符,java,Java,我是大学一年级的学生,正在攻读软件工程学位。 所以我在java课程中有一个作业,我必须加密一个6位数的密码并显示给用户,其中一个步骤是 用第二个数字的剩余部分除以2替换第二个数字。 我尝试使用replace方法进行替换,但它给了我一个错误,告诉我不能用int替换char,下面是我的代码: public class Assignment1 { public static void main(String[] args) { System.out.println("
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位数的数字并输出
加密密码。
您的程序应按如下方式运行:
您必须将
int
转换为char
。这两种数据类型不兼容,主要是因为ints
的宽度为32位,chars
的宽度仅为16位。要解决此问题,必须首先将int
转换为char
,然后执行相反的操作
另外,在创建新的字符串之前,请执行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);
更新:
在对我的帖子发表评论后,我意识到我的答案并没有根据最初的问题陈述正确地给出
现在的假设是从包含数字字符的字符串开始(即“123456”)。然后,根据说明操作这些数字中的每一个。因此,当执行步骤2时,结果必须是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
在构造新字符串之前,替换c
数组中的第二个字符。不要尝试在字符串中替换它,因为这将替换同一个数字的所有匹配项。此外,string#replace(…)
将替换所有匹配的子字符串,而不仅仅是一个字符。在Javadoc中自己阅读它,因为您正在学习,我建议您经常访问Javadoc以获取您想要使用的类方法,并了解它的作用。不要只凭名字来假设。我之前的评论应该给你上了一课。非常感谢你的帮助,我做到了,没有错误,但我尝试显示它只是为了看看它是否有效,我注意到了一个问题,如果余数是0,它会留下一个空白,如果是1,它会显示一个带有问号的框,你知道我如何解决这个问题吗?谢谢你,我真的。。。也许我误解了你最初的问题。当您转换一个数字字符串(即“123456”)并将其转换为char
数组时,您将得到“1”、“2”、“3”等。如果您将这些char
转换为整数,则最终得到的将是该字符的ASCII值,而不是数字值。我会稍微修改一下我的答案。非常感谢。信息技术worked@oussama如果它能帮助你考虑投票it@oussama在编写实际代码之前进行伪编码在您学习时会有很大帮助。即使对我这样做了将近20年的人来说,我也经常用通俗易懂的英语写下这个过程应该做什么,然后我把这个叙述变成代码。另外,您看到我在注释中添加数组值的做法了吗?它有助于记录每个步骤的预期结果,以便在调试代码时,可以将预期结果与实际结果进行比较。每次都这样做,你就不会后悔了。相信我。