我远离完全理解阵列,我真的在努力解决这个问题。这是问题所在:我需要操作一组int。我的程序采用逗号分隔的两个值;第一个值是数组的大小,第二个值是转换的类型。定义一个表示数组大小的常量MAXSIZE,并初始化为包含0到MAXSIZE之间的值。根据输入,程序应打印出每个转换:减半,双,绝对和累积。我认为我的代码甚至没有达到案例方法。我的代码的最后几行不希望因某种原因而被格式化,我用return arrayList完成;
public class Game {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
input.useDelimiter("[,\n]");
System.out.println("Enter size of array and case, separated by comma: ");
final int MAXSIZE = input.nextInt();
String transformation = input.next();
int arrayList[] = new int[MAXSIZE +1];
for (int i = 0; i < arrayList.length; i++) {
arrayList[i] = i;
}
for(int i = 1; i<arrayList.length; i++) {
System.out.print(arrayList[i] + "\n");
}
System.out.print(transformation);
cases(transformation, arrayList);
}
public static void caseDouble(String transformation, int[] arrayList) {
for(int i = 1; i<arrayList.length; i++) {
arrayList[i] = arrayList[i]*2;
}
for(int i = 1; i<arrayList.length; i++) {
System.out.print(arrayList[i] + "\n");
}
}
public static void caseAccumulate(String transformation, int[] arrayList) {
int total = 0;
for (int i = 1; i<arrayList.length; i++) {
total += arrayList[i]/arrayList.length;
System.out.println(arrayList[total]);
}
}
public static void caseAbsolute(String transformation, int[] arrayList) {
for(int i = 1; i<arrayList.length;i++) {
System.out.print(Math.abs(arrayList[i]));
}
}
public static void caseHalve(String transformation, int[] arrayList) {
for(int i = 1; i<arrayList.length;i++) {
System.out.print(arrayList[i]/2);
}
}
public static int[] cases(String transformation, int[] arrayList) {
switch(transformation) {
case "absolute":
caseAbsolute(transformation, arrayList);
break;
case "halve":
caseHalve(transformation, arrayList);
break;
case "accumulate":
caseAccumulate(transformation, arrayList);
break;
case "double":
caseDouble(transformation, arrayList);
答案 0 :(得分:0)
在输入时,应避免在逗号和转换名称之间添加空格。
E.g。
类型
10,absolute
代替
10, absolute
。