试着写一个方法告诉我数字是否在数组中.到目前为止我有..
public static boolean inList(double number, double[] list){
for (double i : list)
{
if (i == number){
return false;
}
}
return true; //if its not in the array I want the if statement to run.
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这对我没有用,由于某种原因它无法识别阵列中的某些数字是否存在.
问题:编写一个读取实数列表的程序.程序结束后,它应该只打印出唯一的数字.也就是说,只有列表中出现一次的数字.如果列表中有超过50个唯一编号,那么您应该只打印前50个.
import java.util.*;
import java.io.*;
import java.util.Arrays;
public class Hmwk {
public static void main(String[] args) throws FileNotFoundException {
Scanner input=new Scanner (new File ("input.txt"));
int n = 0;
final int MAX_SIZE = 50;
double[] numbersArray;
while (input.hasNextDouble() && n<MAX_SIZE){
double in = input.nextDouble();
if (inList(in,numbersArray))
numbersArray[n]=in;
n++;
}
printReport(numbersArray);
}
public static boolean inList(double number, double[] list){
for (double i : list) {
if (i == number){
return false;
}
else
return true;
}
}
public static void printReport(double[] …Run Code Online (Sandbox Code Playgroud) 试图采用数字的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
Run Code Online (Sandbox Code Playgroud)
我的代码......
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; …Run Code Online (Sandbox Code Playgroud) java ×3
boolean ×2
for-loop ×2
methods ×2
while-loop ×2
arrays ×1
if-statement ×1
output ×1
printf ×1