Printf问题或程序问题?

时间:2014-02-16 21:11:04

标签: java for-loop while-loop printf output

尝试使用数字的arraylist并打印出以下内容......

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

我似乎无法弄清楚我的程序出错了,或者我在尝试打印数据时犯了一些愚蠢的错误。感谢你的时间,非常感谢任何和所有的帮助。

1 个答案:

答案 0 :(得分:6)

的printf

调用printf的方式表明您可能对其工作方式存在一些误解。 printF NOT 连接传递给方法的每个参数。它需要包含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");