Untitled
unknown
java
a year ago
2.9 kB
9
Indexable
package org.example;
import java.util.Arrays;
import java.util.List;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) {
int[][] temperatures = {
{15, 18, 20, 22, 19, 21, 23, 17, 16, 18},
{30, 32, 31, 29, 28, 33, 34, 30, 31, 32, 29},
{-5, 0, 3, -2, -1, 1, 2, -3, 0, 1},
{25, 27, 26, 28, 29, 30, 27, 26, 25},
{10, 12, 15, 14, 13, 11, 12, 14, 15, 13, 12},
{35, 37, 36, 34, 33, 32, 31, 30, 36, 35, 34, 33},
{5, 7, 10, 8, 6, 9, 11, 7, 8, 6},
{0, 2, 5, 3, 1, 4, 6, 2, 3, 1},
{28, 30, 27, 29, 31, 32, 30, 28, 29, 27}
};
String[] cityNames = new String[]{"Tokyo", "New York", "Moscow", "Sydney", "London", "Dubai", "Toronto", "Berlin", "Rome"};
System.out.println("avg = " + avgTemp(temperatures[5]));
System.out.println("high = " + getMaxValue(temperatures[5]));
System.out.println("low = " + getMinValue(temperatures[5]));
//Print data in aligned columns
int cityColumnWidth = 15;
int avgTempColumnWidth = 15;
int highTempColumnWidth = 15;
int lowTempColumnWidth = 15;
int ClimateColumnWidth = 15;
System.out.printf("%-" + cityColumnWidth + "s%-" + avgTempColumnWidth + "s%-" + highTempColumnWidth + "s%-" + lowTempColumnWidth + "s%-" + ClimateColumnWidth + "s%n", "City", "Avg Temp", "High Temp", "Low Temp", "Climate");
System.out.println("*".repeat(cityColumnWidth + avgTempColumnWidth + highTempColumnWidth + lowTempColumnWidth + ClimateColumnWidth));
for (int i = 0; i < temperatures.length; i++) {
String columnCity = String.format("%-" + cityColumnWidth + "s", cityNames[i]);
System.out.println(columnCity);
}
}
public static double avgTemp(int[] input) {
int sum = 0;
int length = input.length;
for (int i = 0; i < input.length; i++) {
sum += input[i];
}
return (double) sum / length;
}
public static int getMaxValue(int[] numbers){
int maxValue = numbers[0];
for(int i=1;i < numbers.length;i++){
if(numbers[i] > maxValue){
maxValue = numbers[i];
}
}
return maxValue;
}
public static int getMinValue(int[] numbers) {
int minValue = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] < minValue) {
minValue = numbers[i];
}
}
return minValue;
}
}Editor is loading...
Leave a Comment