gpt4 book ai didi

java - 在不重新分配原始字符串值的情况下将 int 的值压入字符串

转载 作者:行者123 更新时间:2023-11-29 03:22:39 25 4
gpt4 key购买 nike

所以我有一个值作为整数从类中返回,并且根据输入的文本长度重复了几次。我可以打印出每个字符的输出,但我想将输出组合成一个字符串。这是输出示例。

Enter plain-text: hi

The vaule of H is 7

The encrypted value is 721 The decrypted value is 7

The vaule of I is 8

The encrypted value is 330 The decrypted value is 8

我希望它将最终密文打印为 "721330",以便将其发送到数据库并将该值与数据库中存储的值进行比较。

执行此操作的最佳方法是什么?

@SuppressWarnings("资源") public static void main(String[] args) {

    int p = 29;
int q = 41;
int d = 83;
int n = 0;
int w = 0;
int e=27;
int c =1;
String plainText;
String Str = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

//System.out.println(m);
n = p*q;
//System.out.println("The n value equals " + n);
w = (p-1)*(q-1);
//System.out.println("The w value equals " + w);

int qo=0;
int re=0;

w = w*1+d*0;
d = w*0+d*1;


qo = w/d;
//System.out.println("quotient value " + qo);
re = w-qo*d;
//System.out.println("reminder value " + re);
qo=d/re;
//System.out.println("quotient value " + qo);
re = d-qo*re;
//System.out.println("reminder value " + re);
Scanner scn=new Scanner(System.in);

System.out.println("Enter plain-text:");
plainText=scn.nextLine();

plainText=plainText.toUpperCase();
char[] chars = plainText.toCharArray();

for (int i=0;i<chars.length;i++)
{

int m= Str.indexOf(chars[i]);

System.out.println("The value of " +chars[i] + " is " + m +"\n");

toBin(e);
//System.out.println("The binary value of e " + toBin(e));
de_encryption(c,n,m,toBin(e));
int C = de_encryption(c,n,m,toBin(e));

//String final_output = String.valueOf(C);

StringBuilder sb = new StringBuilder();
int part = C;
sb.append(part);

String result = sb.toString();
//I want the final_output value to be appended into a stringbuilder
System.out.println("The encrypted value is " + result);

toBin(d);
//System.out.println("The binary value of d " + toBin(d));

int D = de_encryption(c,n,C,toBin(d));
StringBuilder sb1 = new StringBuilder();
int part1 = D;
sb1.append(part1);

String result1 = sb1.toString();
System.out.println("The decrypted value is "+ result1 +"\n");


}

}

最佳答案

如果我没理解错的话,您是在问我们如何通过连接多个部分来创建一个字符串。

使用 StringBuilder :

StringBuilder sb = new StringBuilder();
for (...) {
String part = ...;
sb.append(part);
}
String result = sb.toString();

关于java - 在不重新分配原始字符串值的情况下将 int 的值压入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22741926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com