gpt4 book ai didi

java - Printf 问题还是程序问题?

转载 作者:行者123 更新时间:2023-12-01 08:05:44 24 4
gpt4 key购买 nike

尝试获取数字数组并打印出以下内容...

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");

但是,如果 String 不包含任何动态内容(意味着它不需要占位符),您实际上不需要使用 printF,因此您可以使用纯文本System.out.println(),它将为您添加换行符:

System.out.println("MOST POPULAR NUMBERS");

关于java - Printf 问题还是程序问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21816684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com