【发布时间】:2015-02-02 07:44:33
【问题描述】:
好的,所以我应该使用它作为我的代码的开头:
public static int addOdds(int[] input){}
这将返回总和。
我已经用预加载的数组完成了添加赔率部分。 这很简单,但让我心慌的是如何让这个数组作为用户的输入。
我从 java.utils 知道 Scanner,但我不确定如何让它与我的代码一起运行以使其接受一个数组(如果可以的话)。
我考虑过使用:
public string void main(String [] args){}
并用它调用 Scanner,并使用 Integer.parseint(),但我认为它不能解析数组。
然后从扫描仪中调用输入数组以传递给 addOdds 方法。
例如 {2,3,7,8,4,1} 将是代码必须作为输入的内容。
提前谢谢,我有点难过。
如果有帮助,这里有一个示例查询; inputTwo 与问题无关:
public class Proj2Tester {
public static void main(String[] args){
int[] inputOne = {4,8,9,12,7};
int[] inputTwo = {41,38,19,112,705};
System.out.println("Problem 1 is correct on test input = " + (16 == Problem1.addOdds(inputOne)));
System.out.println("Problem 2 is correct on test input = " + (686== Problem2.getRange(inputTwo)));
}
按照 T.J. 的建议,我尝试了以下方法:
公共课问题1 { 公共静态 void main(String args[]){
System.out.println("Your array is: "+input);
}
}
public static int addOdds(int[] input){
int[] input = new int[args.length]; //Begin T.J.'s segment
int n = 0;
for (String arg : args) {
input[n++] = Integer.parseInt(arg);
int sum =0; //Initializing sum as 0;
for(int i =0; i < inputOne.length; i++){
if(inputOne[i] % 2 !=0){
;
sum = sum + inputOne[i];
break;
}
if(inputOne[i] % 2 == 0){
i++;
break;
}
}
return sum; // placeholder for my answer, not zero should be returned
}
【问题讨论】:
-
您可以阅读
scanner.nextInt()并将其添加到列表中。从用户那里读取完所有整数后,请使用List.toArray() -
我认为除了使用
loop之外没有其他选择...... -
@Runcorn 为什么你认为她不能使用循环?
-
我是说她需要使用循环,别无他法......而且,我想我错了......