Java 打印已包含值的数组
我正在试验代码,我想知道如何打印已经包含值的数组?如果我要添加“一周的温度是:”之类的内容,然后将列表打印给用户。 这是我的代码:Java 打印已包含值的数组,java,arrays,Java,Arrays,我正在试验代码,我想知道如何打印已经包含值的数组?如果我要添加“一周的温度是:”之类的内容,然后将列表打印给用户。 这是我的代码: public static void main(String[] args) { Scanner keyboard=new Scanner(System.in); System.out.println("How many temperatures?"); int size=keyboard.nextInt(); int
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
System.out.println("How many temperatures?");
int size=keyboard.nextInt();
int temp[]=new int[size];
System.out.println("Please enter "+temp.length+" temperatures");
double sum=0;
for(int i=0;i<temp.length;i++){
temp[i]=keyboard.nextInt();
sum=sum+temp[i];
}
double average=sum/temp.length;
System.out.println("The average temperature is: "+average);
for(int i=0;i<temp.length;i++){
if (temp[i]>average){
System.out.println("Above average: "+temp[i]);
}
else if(temp[i]<average){
System.out.println("Below average: "+temp[i]);
}
else if(temp[i]==average){
System.out.println("Equal to average "+temp[i]);
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.println(“多少温度?”);
int size=keyboard.nextInt();
int temp[]=新的int[size];
系统输出打印项次(“请输入“+温度长度+”温度”);
双和=0;
对于(inti=0;i,您可以显式循环数组,也可以使用Arrays.toString方法
int [] arr = {1, 2, 3, 4, 5};
System.out.print("The temperatures of the week are: ");
for(int i = 0; i < arr.length; i++)
System.out.print(arr[i]+" ");
System.out.println();
您需要首先导入java.util.Arrays以使用Arrays类。现在确切的问题是什么??)当@turingcomplete回答您时,我不知道如何以一种方式打印出数组,即单独输入的值将彼此排成一行,这是现在解决的问题:)我没有导入数组类,我只是使用了一个for循环,但它给了我所需的输出。两者都将打印数组,唯一的区别是array.toStringg将打印由方括号包围的数组,如[1,2,3,4]。
System.out.println("The temperatures of the week are: " + Arrays.toString(arr));