//the sum of multiplies
int sumOfMultiples =0;
//loop that runs from i=1 as long as i*30 <=30
for(int i = 1; (3*i) <=30; i++){
sumOfMultiples += 3*i;
}
System.out.println("Sum of multiples of 3 from 3 to 30 = "+sumOfMultiples);
这给了我
的结果list = []
def multiples():
for num in range(1, 11):
if (num % 3) == 0:
list.append(str(num))
print ','.join(list)
multiples()
我希望我的代码只返回列表的最后一项
3
3,6
3,6,9
答案 0 :(得分:2)
代码:
def multiples():
lis = []
for num in range(1, 11):
if num % 3 == 0:
lis.append(str(num))
print ','.join(lis)
multiples()
答案 1 :(得分:0)
我找到了这个解决方案:
",".join(map(str,list))
如果list = [10074,10108],则结果将是:10074,10108