1

私はクラスの宿題をしていて、完全な解決策ではなく、役立つ指針を探しています。基本的には、テキストファイルを読み込んで情報を1行ずつ一覧表示し、行番号を一覧表示し、最後に最大値と最小値、およびそれぞれに関連する年数を出力するJavaプログラムを作成する必要があります。テキストファイルには、年とその年の気温が含まれています。つまり、「1900050.9」のようなものがリストされています。アレイやスキャナーを使用するつもりはありません。これは割り当ての一部です。私はすでにプログラムを毎年正常に印刷し、対応する温度を行数とともに行ごとに印刷することができました。私は言われ、whileループを使用しました。今、私の唯一の問題は、すべての温度から何とか区別できる方法でテキストファイルにアクセスすることです。これは最大で、最小で、それぞれが発生した年です。自分で理解できるようにしたかったので、今まで助けを求めていませんでしたが、割り当てはもはやクレジットの価値がありません遅いペナルティに。私はまだこれを解決したいので、どんな助けでも本当にありがたいです。ありがとう。

これは私が持っているものです。

public class main {

/**
 * @param args the command line arguments
 */

public static void main(String[] args) throws Exception {


File temps = new File ("temps.txt"); //Creates path to temps.txt file
FileReader textReader = new FileReader (temps); //Input information from temps.txt file into file reader
BufferedReader kb = new BufferedReader (textReader); //Use buffered reader to hold temps.txt file info from the file reader




String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts


System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute


    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}




}

}

これまでの出力は次のとおりです。

Line 0: Year 1900: Temp 50.9
Line 1: Year 1901: Temp 49
Line 2: Year 1902: Temp 49.7
Line 3: Year 1903: Temp 49.5
Line 4: Year 1904: Temp 47.1
Line 5: Year 1905: Temp 49.1

などまでずっと...

Line 99: Year 1999: Temp 52.7
BUILD SUCCESSFUL (total time: 0 seconds)
4

3 に答える 3

1

これを行う方法は次のとおりです。

String tempList; //Create string variable named tempList
int lineCount = 0; //Create integer variable named lineCount
String sep = ": Temp "; //Create string variable named sep (short for separation) and set it equal to the literal string ":"
String space = " "; //Create string variable named space and set it equal to an actual space between texts

String maxValueYear = "";
String minValueYear = "";
double maxValue = 0;
double minValue = Double.MAX_VALUE;
System.out.println("The following is the provided information from the file input. ");
while ((tempList = kb.readLine()) !=null) { //while loop stating that as long as the text file still has values to read (is not null), continue to execute

    String year = tempList.substring(0, tempList.indexOf(space));
    double temp = Double.valueOf(tempList.substring(tempList.indexOf(space), tempList.length()));

    if (temp > maxValue) {
        maxValue = temp;
        maxValueYear = year;
    }
    if (temp < minValue) {
        minValue = temp;
        minValueYear = year;
    }
    System.out.println("Line " + lineCount++ + ": Year " + tempList.replace(space, sep)); //Prints out the line number (lineCount++), the info from the temps.txt file with a ":" between the year and the number (tempList.replace (space,sep)

}

System.out.println("The minimum temp occured in year " + minValueYear + " and was " + minValue);
System.out.println("The maximum temp occured in year " + maxValueYear + " and was " + maxValue);
于 2013-03-20T20:53:58.110 に答える
0

最小温度と最大温度を追跡するには、いくつかの変数を使用する必要があります。
より高い(より低い)温度が来るたびに、変数を更新します。

ループの外側の非常に高い最大値と非常に低い最小値から始めましょう。
ループ内に(より高い)より低い温度が表示されたらすぐに、変数を調整します。
ループの後、要約します。

于 2013-03-20T20:31:12.897 に答える
0
Highest Seen So Far: -Infinity
    (or any really low number so that any number you see next will be higher)
Lowest Seen So Far: Infinity
    (or any really high number so that any number you see next will be lower)

Walk through each data point "d":
    is d higher than your latest "highest seen so far"?
        -> if yes, your new highest seen so far is now d
    is d lower than your latest "lowest seen so far"?
        -> if yes, your new lowest seen so far is now d

Your highest seen so far is now the highest data point
Your lowest seen so far is now the lowest data point

擬似コードの場合:

highest = -inf
lowest = inf

for d in dataset:
  if d > highest:
    highest = d
  if d < lowest:
    lowest = d

print "highest: " + highest
print "lowest: " + lowest

ここに例があります

データセットが5284であるとしましょう

Step 0
    Highest: -inf
    Lowest: inf

Step 1
  See d = 5...that's higher than highest -inf, so new highest is 5.
  See d = 5...that's lower than lowest -inf, so new lowest is 5
    Highest: 5
    Lowest: 5

Step 2:
  See d = 2...that's not higher than highest 5...highest is still 5
  See d = 2...that is lower than lowest 5...new lowest is 2
    Highest: 5
    Lowest: 2

Step 3:
  See d = 8...that's higher than highest 5...new highest is 8
  See d = 8...that's not lower than lowest 2...lowest is still 2
    Highest: 8
    Lowest: 2

Step 4:
  See d = 4...that's not higher than highest 8...highest is still 8
  See d = 4...that's not lower than lowest 2...lowest is still 2
    Highest: 8
    Lowest: 2

Result:
    Highest: 8
    Lowest: 2
于 2013-03-20T20:54:42.500 に答える