java - What am I doing wrong with my while loop? -
i working on program takes in user input (temperatures) , puts in array. confused on last method have create. need go through array , print them least greatest. need use while loop this.
the thing is need index stay temperature values. index represents day temperature taken. have method finds lowest value in array , prints along index.
can use method have , use in new method? if have no idea how call method while doing while loop. going try , find lowest value , print both value , index , change value "uninitialized" variable can find next lowest value , on.
the last method in code "leasttogreatest" attempt @ doing not work. have been working on trying figure out hours on own. have no idea need or how need organize work.
here have:
public class weather { static int lowesttemp; static int lowestday; private static final int uninitialized = -999; public static void main(string[] args) { // todo auto-generated method stub int[] high = new int[32]; findlowesttempinarray(low); system.out.println("\n" + "the lowest low is: " + lowesttemp + " degrees." + "\n" + "this temperature recorded on day: " + lowestday); leasttogreatest(low); system.out.println("\n" + lowestday + " " + lowesttemp + "\n"); } public static int findlowesttempinarray(int[] t) { // returns index of lowest temperature in array t lowesttemp = uninitialized; lowestday = 0; (int day = 0; day < t.length; day++) { if (t[day] != uninitialized && (t[day] < lowesttemp || lowesttemp == uninitialized)) { lowesttemp = t[day]; lowestday = day; } arrays.aslist(t).indexof(lowestday); } return lowestday; } public static void leasttogreatest(int[] t) { lowesttemp = uninitialized; lowestday = 0; while (lowestday >= 0 && lowestday <= 31) { (int day = 0; day < t.length; day++) { if (t[day] != uninitialized && (t[day] < lowesttemp || lowesttemp == uninitialized)) { lowesttemp = t[day]; lowestday = day; } } } }
}
yes, can reuse other method in here.
public static void leasttogreates(int[] temps) { // copying old array temps newtemparr int[] newtemparr = new int[temps.length]; (int = 0; < temps.length; i++) newtemparr[i] = temps[i]; int days = 0; while (days < temps.length) { int lowest = findlowesttempinarray(newtemparr); if (newtemparr[lowest] > uninitialized) system.out.println("temp: " + newtemparr[lowest] + ", on day: " + lowest); // setting temperature of current lowest day "uninitialized" // (so it's not lowest temperature anymore) newtemparr[lowest] = uninitialized; days++; } }
what here is:
- copy temperatures array (in order able change values in it) without affecting original array
- print lowest temperature in array
- set temperature of index "uninitialized"
- repeat
Comments
Post a Comment