0

How to make it work?

i have a task to count the longest streak of ascending numbers: 1 2 3 4 -> streak of 4 1 2 3 4 2 3 4 5 6 -> longest streak is 5 (2 3 4 5 6) so i have one problem, the problem is that when i "break" the streak with a lower number then the one before, and then starting to build a larger streak - it wont count in the streak the first number that "broke" the last streak - cuz it just resets the streak... example: 1 2 3 4 5 4 5 6 7 8 9 -> outputs 5 instead of 6. (btw, the input will stop when entering 999 or -1) my code: import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter Percent:"); double percent = scan.nextDouble(); int streak = 0; int maxStreak = streak; double max = 0; while(percent != -1 && percent != 999){ if (percent > max){ max = percent; streak++; } if (percent < max){ streak = 0; max = 0; } if (streak > maxStreak){ maxStreak = streak; } System.out.println("Enter Percent:"); percent = scan.nextDouble(); } System.out.println("Longest Streak of days with high percentage: " + maxStreak); } }

1st Dec 2020, 7:45 PM
Yahel
Yahel - avatar
5 ответов
+ 1
Reset to 1 streak. The current number is also started streak. streak = 1 ;
1st Dec 2020, 9:04 PM
Jayakrishna 🇮🇳
+ 1
Yahel No. I mean reset in : if(percent <max) { streak=1; max = 0; }
1st Dec 2020, 9:18 PM
Jayakrishna 🇮🇳
+ 1
Oh. Set max = percent ; also.. Yahel if(percent <max) { streak=1; max = percent; } Actually make it as else part. Then I think it works better...
2nd Dec 2020, 7:28 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 so if I do 1 2 3 4 it will give me 5 instead of 4. So at the end- should I subtract the streak by 1?
1st Dec 2020, 9:14 PM
Yahel
Yahel - avatar
0
Jayakrishna🇮🇳 , but now if I do that: 6 5 4 -> it outputs 2.
2nd Dec 2020, 9:46 AM
Yahel
Yahel - avatar