我需要将min和max值存储在给定的数组中,然后使用特定字符打印出来(+表示最小值,“ - ”表示最小值,“*”表示所有其余值)。 / p>
我认为除了适当的存储值之外,我已经完成了大部分操作,因此所有值都不是“++++++++++”....就像它们当前打印出来一样。
有什么想法吗?非常感谢帮助。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int numbers[] = new int[24];
int min = Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
int maxhour = 0;
int minhour = 0;
int total = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
char currentchar = 0;
for(int i=0; i< numbers.length; i++){
numbers[i] = keyboard.nextInt();
total = total + numbers[i];
if(numbers[i]<min){
min = numbers[i];
minhour = i;
currentchar = MIN;
}else if (numbers[i]>max){
max = numbers[i];
maxhour = i;
currentchar = MAX;
}
}
for(int i=0; i< numbers.length; i++){
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : " + max + " at hour " + maxhour);
System.out.println("Average Number of hits is : " + (total/24) + " per hour");
System.out.println("Smallest Number of hits is : " + min + " at hour " + minhour);
}
public static void printTimes(char c, int times) {
if (times >= 70) {
for(int i=0; i< 69; i++){
System.out.print(c);
} System.out.print(">");
} else if (times < 70) {
for(int i=0; i< times; i++)
System.out.print(c);
}
}
}
当前输出示例:
42 29 36 7 5 3 10 13 33 40 51 49 22 58 63 102 65 58 48 24 36 48 52 42
小时0:++++++++++++++++++++++++++++++++++++++++++++++++++++++ p>
第1小时:++++++++++++++++++++++++++++++++++++
第2小时:+++++++++++++++++++++++++++++++++++++++++
第3小时:+++++++
第4小时:+++++
第5小时:+++
第6小时:+++++++++++++++++++++
第7小时:++++++++++++++++++++
第8小时:+++++++++++++++++++++++++++++++++++++++
第9小时:++++++++++++++++++++++++++++++++++++++++++++++
...
最大点击次数是:小时15点
平均点击次数为:每小时39次
最小点击次数为:3时5点
答案 0 :(得分:1)
只需更改上一个for
:
for (int i = 0; i < numbers.length; i++) {
System.out.print("Hour " + i + ":");
if (numbers[i] == min)
currentchar = MIN;
else if (numbers[i] == max)
currentchar = MAX;
else
currentchar = MIDDLE;
printTimes(currentchar, numbers[i]);
System.out.println("");
}
答案 1 :(得分:0)
我会按如下方式更新你的代码 -
public static void main(String[] args) {
int numbers[] = new int[24];
int total = 0;
System.out.println("Enter 24 integers please");
Scanner keyboard = null;
try {
keyboard = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
numbers[i] = keyboard.nextInt();
total += numbers[i];
}
} finally {
keyboard.close();
}
Integer min = null;
Integer max = null;
int maxhour = 0;
int minhour = 0;
char MAX = '+', MIN = '-', MIDDLE = '*';
for (int i = 0; i < numbers.length; i++) {
if (min == null || numbers[i] < min) {
min = numbers[i];
minhour = i;
} else if (max == null || numbers[i] > max) {
max = numbers[i];
maxhour = i;
}
}
for (int i = 0; i < numbers.length; i++) {
char currentchar = MIDDLE;
if (i == minhour) {
currentchar = MIN;
} else if (i == maxhour) {
currentchar = MAX;
}
System.out.print("Hour " + i + ":");
printTimes(currentchar, numbers[i]);
System.out.println("");
}
System.out.println("Largest Number of hits is : "
+ max + " at hour " + maxhour);
System.out.println("Average Number of hits is : "
+ (total / 24) + " per hour");
System.out.println("Smallest Number of hits is : "
+ min + " at hour " + minhour);
}