gpt4 book ai didi

java - 用java将数组相乘

转载 作者:行者123 更新时间:2023-11-30 03:01:37 25 4
gpt4 key购买 nike

我很好奇如何将数组乘以一个因子?不是每个单元格(t[0]、t[1] 等)都是单独的,而是作为一个整数。例如:t[0] = 9 t[1] =2 t[2] = 5, t[] = 925。925 次 3 =2775

基本上,我收到一个值并将其从 ASCII 转换为十进制(我已经这样做了)。但是,我想乘以它的 3 倍。是否需要将整个数组存储为字符串,然后使用乘法函数?

本节相关代码

byte[] readBuf =(byte[]) msg.obj);
char x;
String readMessage = newString(readBuf,0,msg.arg1);
int[] t = new int[readMessage.length()];
for(int i = 0; i<readMessage.length(); i++)
{
x = readMessage.charAt(i);
int z = (int) x;//Array has been converted from ASCII into decimal values
t[i] = z;//Array has been populated with decimal values

//Confused about the next part, Convert back into string and then multiply string?
}

最佳答案

为什么要为 char 和 int 创建额外的变量,为表格使用空间,以及将内容从一个变量传递到另一个变量,而这些都可以在一行中完成?据我了解,这就是您所需要的。

byte[] readBuf =(byte[]) msg.obj);
String readMessage = newString(readBuf,0,msg.arg1); //You create the string here
String final=""; //new string to be parsed
for(int i = 0; i<readMessage.length(); i++){
final+=""+(int)readMessage.charAt(i); // get the charAt(i) cast it to int and give it to the string
}
return Integer.parseInt(final)*factor; //return the int multiplied by 3

关于java - 用java将数组相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429967/

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