尝试获取数字数组列表并打印以下内容...
MOST POPULAR NUMBERS
The following numbers were picked 263 times: 41
LEAST POPULAR NUMBERS
The following numbers were picked 198 times: 20
AVERAGE
The Average was 228.545455 times.
The following numbers were picked 228 times: 5 22
The following numbers were picked 229 times: 2 7 12 40
我的代码...
import java.util.*;
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
public class Hmwk {
public static void main(String[] args) throws FileNotFoundException {
Scanner input=new Scanner (new File ("input.txt"));
int counter = 0;
ArrayList<Integer> numberList = new ArrayList<Integer>(45);
while(input.hasNextInt()){
int in = input.nextInt();
numberList.add(in);
counter++;
}
mostPopular(numberList,counter);
leastPopular(numberList,counter);
average(numberList,counter);
}
public static void mostPopular(ArrayList<Integer> list, int total){
Collections.sort(list);
int popular = 0;
int counter = 0;
int counterTwo = 0;
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
}
if(counter > counterTwo){
counterTwo = counter;
popular = i;
}
}
System.out.printf("MOST POPULAR NUMBERS");
System.out.printf("The following number was picked",counterTwo,"times:", popular);
}
public static void leastPopular(ArrayList<Integer> list, int total){
Collections.sort(list);
int unpopular=0;
int counter = 0;
int counterTwo = 0;
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
if(counter < counterTwo){
counterTwo = counter;
unpopular = i;
}
}
}
System.out.printf("LEAST POPULAR NUMBERS");
System.out.printf("The following number was picked",counterTwo,"times:", unpopular);
}
public static void average(ArrayList<Integer> list, int total){
int sum = 0;
int counter = 0;
ArrayList<Integer> average = new ArrayList<Integer>(45);
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter++;
i++;
if(i == total-1) break;
}
average.add(counter);
}
for (int i = 0; i <average.size(); i++){
sum+= average.get(i);
}
double average2 = sum/total;
System.out.printf("AVERAGE");
System.out.printf("The Average was",average,"times.");
double ceiling = Math.ceil(average2) ;
double floor = Math.floor(average2);
int counter2 = 0;
Collections.sort(list);
for (int i=0; i<total-1; i++){
while(list.get(i) == list.get(i+1)){
counter2++;
i++;
if(i == total-1) break;
}
if(counter2 == ceiling){
System.out.printf("The following number was picked", ceiling,"times:",i);
}
if (counter2 == floor){
System.out.printf("The following number was picked", floor,"times:",i);
}
}
}
我的输出当前是...
MOST POPULAR NUMBERSThe following number was pickedLEAST POPULAR NUMBERSThe following number was pickedAVERAGEThe Average was
我似乎无法弄清楚我的程序出了什么问题,或者在尝试打印数据时犯了一些愚蠢的错误。非常感谢您提供的所有帮助。
最佳答案
打印
调用printf
的方式表明您可能对它的工作方式有一些误解。 printF
不会将传递给方法的每个参数连接在一起。它使用包含占位符的String
作为第一个参数,然后将连续的参数分配给每个占位符。
要调用printf
,您需要在初始字符串中添加占位符,并为每个占位符提供参数,如下所示:
System.out.printf("The following number was picked %s times %s",counterTwo, popular);
该代码当前仅打印第一个
String
参数,该参数没有任何占位符。然后,将忽略提供给该方法的多余参数,因为不存在用于分配这些参数的占位符。这是一个简单的例子,以帮助
String stringForFormatting = "Argument %s Argument %s";
String argument1 = "1";
String argument2 = "2";
System.out.printf(stringForFormatting, argument1, argument2); //any other args would be ignored
//outputs Argument 1 Argument 2
新线
您似乎还希望输出出现在不同的行上。这可以通过两种方式完成。
首先,您可以将
\n
添加到String
:System.out.printf("MOST POPULAR NUMBERS\n");
但是,如果
printF
不包含任何动态内容(这意味着它不需要占位符),则实际上不需要使用String
,因此可以使用普通的System.out.println()
,它将为您添加换行符:System.out.println("MOST POPULAR NUMBERS");