我是java的新手,感谢这个网站弄清楚如何从列表中获取最低值,但我仍然在努力解决如何让相同的代码为最高价值工作。我在过去的两个小时里一直在努力。再次感谢任何帮助
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class LargenSmall {
public static void main(String[] args) throws IOException {
String filename; //Numbers file
double lowest = Double.POSITIVE_INFINITY;//lowest number in list
double highest; //highest number in list
//Open the file
File file = new File("Numbers.txt");
Scanner inputFile = new Scanner(file);
//Set lowest to zero
//Read all the values in Numbers file and find the lowest value
while (inputFile.hasNext()) {
//Read the numbers in the file and compare each value to find lowest value
double number = inputFile.nextDouble();
if (number < lowest) lowest = number;
}
//Set highest to zero
highest = 0.0;
//Read all the values in Numbers file and find the highest value
while (inputFile.hasNext()) {
//Read the numbers in the file and compare each value to find highest value
double number = inputFile.nextDouble();
if (number > highest) highest = number;
}
//Close file
inputFile.close();
//Print out the lowest value in the list
System.out.println("The lowest number in your file called, " +
"Numbers.txt is " + lowest + ".");
//Print out the highest value in the list
System.out.println("The highest value in the list is " + highest);
}
}
我尝试了一些变化,最高值继续回归0.0
答案 0 :(得分:0)
首先,你应该在循环之前声明变量以提高速度:
double number;
while (inputFile.hasNext())
{
number = inputFile.nextDouble();
if (number > highest) highest = number;
}
其次,似乎你没有在开始找到最高值之前从文件的开始读取,因此,这个块可能没有被执行(因为你已经到达文件的末尾):
while (inputFile.hasNext())
{
double number = inputFile.nextDouble();
if (number > highest) highest = number;
}
答案 1 :(得分:0)
你几乎就在那里。您不需要两个While循环。第二 虽然循环不适用于你的情况。你可以包括条件 你的第一个while循环如下。
double lowest = Double.POSITIVE_INFINITY;
double highest = 0d;
// Read all the values in Numbers file and find the lowest value
while (inputFile.hasNext()) {
// Read the numbers in the file and compare each value to find
// lowest value
double number = inputFile.nextDouble();
if (number < lowest)
lowest = number;
else if (number > highest)
highest = number;
}
答案 2 :(得分:0)
在学习语言时(以及在学习之后),有一部分代码很容易编写,测试和维护。例如,比较一个数字并记住可以移动到另一个类并进行测试。
我无意纠正您的编程错误。我想谈谈良好的编程实践to write better code。
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class LargenSmall {
BigHolder bigHolder= new BigHolder();
SmallHolder smallHolder = new SmallHolder();
public static void main(String[] args) throws IOException {
new LargenSmall().doSearch(args[0]);
}
void doSearch (String filename) throws IOException {
//Open the file
File file = new File(filename);
Scanner inputFile = new Scanner(file);
iterateAndFetchNumbers(inputFile);
inputFile.close();
printResult();
testNumbers();
}
private void iterateAndFetchNumbers(Scanner inputFile) {
//Read all the values in Numbers file and find the highest value
while (inputFile.hasNext()) {
//Read the numbers in the file and compare each value to find highest value
double number = inputFile.nextDouble();
smallHolder.accept(number);
bigHolder.accept(number);
}
}
private void printResult() {
//Print out the lowest value in the list
System.out.println("The lowest number in your file called, " +
"Numbers.txt is " + smallHolder.small + ".");
//Print out the highest value in the list
System.out.println("The highest value in the list is " + bigHolder.big);
}
//
private static class SmallHolder {
double small= Double.POSITIVE_INFINITY;
public void accept(double newNumber) {
if ( newNumber < small) {
small = newNumber ;
}
}
}
private static class BigHolder {
double big= 0;
public void accept(double newNumber) {
if ( newNumber > big ) {
big = newNumber ;
}
}
}
//self test example - should be in junit class
public void testNumbers() {
BigHolder b = new BigHolder();
b.accept(1);
b.accept(3);
b.accept(8);
b.accept(0);
b.accept(100);
assert b.big == 0;//junit assert really
}
}