Java 找到给定列表中的奇数(-6到38),然后将赔率放入数组中
这是到目前为止我的代码。有一些冗余,因为我试图确保没有任何混乱,虽然我怀疑它会,只是为了比我想象的更安全Java 找到给定列表中的奇数(-6到38),然后将赔率放入数组中,java,Java,这是到目前为止我的代码。有一些冗余,因为我试图确保没有任何混乱,虽然我怀疑它会,只是为了比我想象的更安全 int[] odds=new int[22]; // array to hold values odds[0]=-5; // couldn't figure out how to make these work, so i manually odds[1]=-3; // input them, help on that would be appreciated as well
int[] odds=new int[22]; // array to hold values
odds[0]=-5; // couldn't figure out how to make these work, so i manually
odds[1]=-3; // input them, help on that would be appreciated as well
for (int count=0; count<39; count++)
{
int temp=0;
if ((count%2)==0) // i have put the array in the odd part as well with
{ // same result
int index=2;
odds[index]=temp-1; // turns the even odd by subtracting 1
index++;
}
temp++;
}
int[]赔率=新整数[22];//用于保存值的数组
赔率[0]=-5;//不知道如何使这些工作,所以我手动
赔率[1]=-3;//输入他们,在这方面的帮助将不胜感激
对于(int count=0;count更干净的实现:
ArrayList<int> odds = new ArrayList<int>();
for (int number = -6; number <= 38; number++) {
if(number % 2 == 1)
odds.add(number);
}
感谢你帮我回答。感谢你让我很容易忽略这些小问题。非常感谢
int[] odds=new int[22];
odds[0]=-5;
odds[1]=-3;
int temp=0;
int index=2;
for (int count=0; count<39; count++)
{
if ((count%2)==0)
{
odds[index]=temp-1;
index++;
}
temp++;
}
int[]赔率=新的int[22];
赔率[0]=-5;
赔率[1]=-3;
内部温度=0;
int指数=2;
对于(int count=0;count与您可能面临的问题无关,您应该将int temp=0
和int index=2;
行移到for
循环之外。现在它总是分别用0和2初始化,并最终覆盖赔率[2]的值
.Done,在JGrasp中得到同样的结果,很奇怪,网站接受了。谢谢。有趣的是,我怎么忽略了这一点。它对我来说运行得很好。这是赔率
数组的输出:-5-3-1 1 3 5 7 9 11 15 17 19 21 23 25 27 29 31 33 37
谢谢你的帮助。我也需要记住关于math.abs的内容。哈哈。非常感谢你的c看起来也更瘦。
int[] odds=new int[22];
odds[0]=-5;
odds[1]=-3;
int temp=0;
int index=2;
for (int count=0; count<39; count++)
{
if ((count%2)==0)
{
odds[index]=temp-1;
index++;
}
temp++;
}