קוד PHP:
import java.util.*;
public class ProblemForMoveOrDiE {
static Scanner scanner = new Scanner(System.in);
public static void main(String args[]) {
int temp, max;
max = 0;
temp = 1;
boolean three = false;
while(temp != 0) {
System.out.println("Enter a number"); // Sending Message
temp = scanner.nextInt(); // Recive input
if(temp > max) // Check if temp is the largest one
max = temp;
if(three) // if three is set for true, set false. and the oppesite
three = false;
else
three = true;
}
if(three == false && max != 0) {
// if three is false that means that the member entered at least 3 digits or didnt entered even 1.
// But if max != 0, the user must have entered at least 3
System.out.println("The biggest number is: " + max);
}
else {
System.out.println("You didn't entered at least 3 digits");
}
}
}
כפי שאמרת, הכל מתקיים.אסביר לך את הלוגיקה מאחורי זה.
כדי שנדע מתי להפסיק, על הקלט להיות 0. לכן הwhile נגמר, כאשר temp שווה ל0.
את הmax איפסתי כי אחרת הוא לא ידע איך להשוות ביניהם.
כל פעם שהקלט גדול מmax, יוחלף הערך של max בגדול ביותר.
המשתנה הבוליאני שלנו הוא מעין משתנה שבודק העם מספר הפעמים הוא זוגי.
אנחנו צריכים שהוא יהיה אי זוגי(כי הרי שלוש הוא אינו זוגי).
אבל אי זוגי יכול להיות גם 1, כלומר המשתמש לא הכניס כלום.
לכן, נבדוק עם max הוא 0. הרי כדי לצאת מהלולאה צריך להכניס 0. והערך ההתחלתי של max הוא 0. לכן אם הוכנס 0. מקס ישאר על 0.
מקווה שהבנת. אם לא, אני פה לכל שאלה.