有没有办法专门从数组 Java 中挑选出元素到 Ada
Posted
技术标签:
【中文标题】有没有办法专门从数组 Java 中挑选出元素到 Ada【英文标题】:Is there a way to specifically pick out elements from an array Java to Ada 【发布时间】:2021-06-01 13:03:55 【问题描述】:所以我正在尝试转换这个 java 代码:
static int evenPosition(int [] num)
int sum = 0;
for(int i = 0; i < num.length; i++)
sum = (num[0] + num[2] + num[4] + num[6] + num[8] + num[10])*3;
//System.out.println(sum[i]);
return sum;
到 ada 代码:
type numSize is range 0 .. 9999999999;
type SIZE is range 1 .. 12;
type barcode is array(SIZE) of numSize;
type odd is range 1..3;
N1 : barcode := (0, 7, 9, 4, 0, 0, 8, 0, 4, 5, 0, 1);
N2 : barcode := (0, 2, 4, 0, 0, 0, 1, 6, 2, 8, 6, 0);
N3 : barcode := (0, 1, 1, 1, 1, 0, 8, 5, 6, 8, 0, 7);
N4 : barcode := (0, 5, 1, 0, 0, 0, 1, 3, 8, 1, 0, 1);
procedure evenPosition(num : in numSize) is
sum : numSize := 0;
begin
sum := num;
for i in odd loop
sum := sum + sum;
Put_Line("This is the sum");
Put_Line(sum);
end loop;
end;
我对 ADA 很陌生,我的问题是有一种方法可以选择特定元素,例如 java 有什么
sum = (num[0] + num[2] + num[4]...)
如果我的措辞不正确,我很抱歉,我试图在网上找到不同的方法来做到这一点,但似乎无法找到我正在寻找的答案。请善待;-;
编辑
我很抱歉造成混乱,但我设法解决了我想做的事情。
procedure Hw2Bnguyen is
type numSize is range 0 .. 9999999999;
type SIZE is array(1 .. 12) of Integer;
--type barcode is array(SIZE) of numSize;
-- type odd is range 1..3;
-- N1 : barcode := (0, 7, 9, 4, 0, 0, 8, 0, 4, 5, 0, 1); valid
--N2 : barcode := (0, 2, 4, 0, 0, 0, 1, 6, 2, 8, 6, 0); not
-- N3 : barcode := (0, 1, 1, 1, 1, 0, 8, 5, 6, 8, 0, 7); valid
-- N4 : barcode := (0, 5, 1, 0, 0, 0, 1, 3, 8, 1, 0, 1); not
barcode : SIZE;
num : Integer := 0;
num2 : Integer := 0;
num3 : Integer := 0;
num4 : Integer := 0;
begin
begin
Put("Enter message: (press enter each time)");
for i in 1..12 loop
barcode(i) := Integer'Value(Get_Line);
end loop;
--for k in 1..SIZE'Length loop
-- Put(barcode(k));
--end loop;
for k in 1 ..1 loop
num := barcode(k);
end loop;
for k in 3 ..3 loop
num := num + barcode(k);
end loop;
for k in 5 ..5 loop
num := num + barcode(k);
end loop;
for k in 7 ..7 loop
num := num + barcode(k);
end loop;
for k in 9 ..9 loop
num := num + barcode(k);
end loop;
Put("This is an odd sum: ");Put(num*3);
New_Line;
for k in 2 ..2 loop
num2 := barcode(k);
end loop;
for k in 4 ..4 loop
num2 := num2 + barcode(k);
end loop;
for k in 6 ..6 loop
num2 := num2 + barcode(k);
end loop;
for k in 8 ..8 loop
num2 := num2 + barcode(k);
end loop;
for k in 10 ..10 loop
num2 := num2 + barcode(k);
end loop;
Put("This is an even sum: ");Put(num2);
New_Line;
num3 := (num*3) + num2;
Put("Total from both numbers: "); Put(num3);
【问题讨论】:
您好,您是要对 N1、N2、N3 和 N4 的值求和吗? N1(0)+N2(0)+N3(0)+N4(0)。 嗨 Smionean,我正在尝试将数组中的元素添加在一起。所以如果有意义的话,我想从数组 N1 中添加元素到偶数的位置? 在您的编辑中,不需要所有这些循环(无论如何它们都只有一次迭代......),您只需执行 num := barcode(1) + barcode(3) + barcode (5) + 条码(7) + 条码(9); 【参考方案1】:您可以使用mod
(或rem
,它们仅对负数不同)运算符来检查数字是奇数还是偶数:
procedure Hw2Bnguyen is
type SIZE is array(1 .. 12) of Integer;
Barcode : SIZE;
Sum_Odd : Integer := 0;
Sum_Even : Integer := 0;
Sum_All : Integer := 0;
begin
Put("Enter message: (press enter each time)");
for Number of Barcode loop
Number := Integer'Value(Get_Line);
end loop;
for k in Barcode'Range loop
if k mod 2 = 0 then
Sum_Even := Sum_Even + Barcode(k);
else
Sum_Odd := Sum_Odd + Barcode(k);
end if;
end loop;
Put("This is an odd sum: ");Put(Sum_Odd*3);
New_Line;
Put("This is an even sum: ");Put(Sum_Even);
New_Line;
Sum_All := (Sum_Odd*3) + Sum_Even;
Put("Total from both numbers: "); Put(Sum_All);
end Hw2Bnguyen;
【讨论】:
以上是关于有没有办法专门从数组 Java 中挑选出元素到 Ada的主要内容,如果未能解决你的问题,请参考以下文章