我有一个学生班,有: 以及他们的吸气剂和吸气剂。 我也有一个StudentTest类,该类具有三种不同的方法:1.向用户询问数组的大小,然后创建类型为Student的数组,2.向用户询问使用名称和ID号填充数组的时间就像数组一样,3.显示数组的内容。 到目前为止,我的代码是; ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我想从用户那里获取10个数字并将其输入到新数组中。 如何将[]放入输出中?
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num = 10;
List<Integer> inputList = new ArrayList<>(num);
while (num-- > 0) {
inputList.add(scnr.nextInt());
}
List<Integer> goofyArray = new ArrayList<>(inputList.size());
for (int i = inputList.size() - 1; i >= 0; i--) {
if(inputList.get(i) % 7 == 0){continue;}
if(inputList.get(i) < 0){
goofyArray.add((inputList.get(i) * -1) * 2);
} else {
goofyArray.add(inputList.get(i));
}
}
for (int number : goofyArray) {
System.out.print( number + " ");
}
}
}
不确定将[]放到我的输出中是什么意思。试试看:
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int num = 10;
List<Integer> inputList = new ArrayList<>(num);
while (num-- > 0) {
inputList.add(scnr.nextInt());
}
List<Integer> goofyArray = new ArrayList<>(inputList.size());
//Reverse the array.
for (int i = inputList.size() - 1; i >= 0; i--) {
if(inputList.get(i) % 7 == 0){continue;} //doesn't add numbers that are multiples of 7.
if(inputList.get(i) < 0){
goofyArray.add((inputList.get(i) * -1) * 2); // Change any numbers that are negative to be positive and twice their value.
} else {
goofyArray.add(inputList.get(i));
}
}
int[] output = new int[goofyArray.size()];
output = goofyArray.toArray(output);
for (int number : output) {
System.out.print( number + " ");
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int NUM_OF_INPUTS = 10;
int [ ] inputArray = new int [NUM_OF_INPUTS];
for (int index = 0; index < NUM_OF_INPUTS; index++){
inputArray[index] = scnr.nextInt();
}
int[] goofyArray = new int[inputArray.length];
int[] arrayWithoutSevensAndReversed; // make a new array because static arrays don't have a remove
int multiplesOfSeven = 0;
// for loop 1: reverse and check how many multiples of 7
for (int i = 0; i < goofyArray.length; i++) {
goofyArray[i] = inputArray[goofyArray.length - 1 - i]; // Reverse the array.
if (goofyArray[i] % 7 == 0)
multiplesOfSeven++;
}
// for loop 2: use the new array with that will not contains multiples of seven the array
arrayWithoutSevensAndReversed = new int[goofyArray.length - multiplesOfSeven];
for (int x = 0, y = 0; x < goofyArray.length; x++) {
if (goofyArray[x] % 7 != 0) { // is not multiple of 7, then add it to the array!
arrayWithoutSevensAndReversed[y] = goofyArray[x];
if (arrayWithoutSevensAndReversed[y] < 0) { // make it positive then double!
arrayWithoutSevensAndReversed[y] = arrayWithoutSevensAndReversed[y] * -1 * 2;
}
y++;
}
}
for (int number : arrayWithoutSevensAndReversed) {
System.out.print(number + " ");
}
}
}
如何将[]放入输出中?
使用util包中的Arrays.toString(int array []) 。
System.out.println(Arrays.toString(goofyArray));
据我了解,您想将数组打印为输出。
您要做的就是代替此:
for (int number : goofyArray) {
System.out.print( number + " ");
}
做这个:
System.out.print(goofyArray.toString());
与其将其称为删除,不如将其移至第i + 1个词
if ( goofyArray[i] % 7 == 0){
for(int x=i;x<goofyArray.length-1;x++){
goofyArray[x]=goofyArray[x+1];
}
在这里,第i项被删除。
要从数组中删除元素,可以使用org.apache.commons.lang包中的ArrayUtils.remove(int array [],int index) 。
int num[]=new int[] {10,20,30,40,50};
num = ArrayUtils.remove(num, 2);
如果数组中有任何42,我希望我的代码删除所有其他数字,并使goofyArray包含1个元素42。在此示例中,我们有两个42,其余为其他数字。 因此,它将删除除42之外的所有其他数字。
public static void main(String[] args) {
int[] inputArray = { 5, -2, 42, 45, -6, 8, 42, -9, 10, 7 };
int[] tempArray = new int[inputArray.length];
int count=0;
for (int i = 0; i < inputArray.length; i++) {
if(inputArray[i]==42) {
tempArray[count]=42;
count++;
}
}
int[] goofArray=Arrays.copyOf(tempArray, count);
for(int i=0;i<goofArray.length;i++)
System.out.print(goofArray[i]+" ");
}
在编辑问题之前,您需要:反转数组。 如果数组包含任何负数,则先使其为正数,然后使其两次(-9将变为18)。 如果数组包含一个可被7整除的元素,则将其删除。 本示例将满足您的要求。
public static void main(String[] args) {
int[] inputArray = { 5, -2, 28, 45, -6, 8, 42, -9, 10, 7 };
int length = inputArray.length;
int[] goofyArray = new int[length];
int count=0;
for (int i = 0; i < goofyArray.length; i++) {
if (inputArray[length - 1 - i] % 7 != 0) {
goofyArray[count] = inputArray[length - 1 - i]; // Reverse the array.
if (goofyArray[count] < 0) {
goofyArray[count] = Math.abs(goofyArray[count]) * 2;
}
count++;
}
}
int[] goofArray2=Arrays.copyOf(goofyArray, count);
for(int i=0;i<goofArray2.length;i++)
System.out.print(goofArray2[i]+" ");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.