Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Individual Problem: Read this about flags. Write a program that will read 5 numb

ID: 3679898 • Letter: I

Question

Individual Problem:

Read this about flags.

Write a program that will read 5 numbers from the keyboard and tell the user some fact about the numbers. Use this design.

Input the first number (int) from the user and save it in a variable (prev)

Loop four more times

       get another number from the keyboard (curr)

      compare the two numbers and decide if prev is smaller than the curr number.
      if prev is smaller,
          set a flag variable to False.

      Put the curr value in the prev variable.

(After the loop exits, test the flag variable)
if value of flag variable is True
    output "yes"
else
    output "no"

Hint: there is one step missing from this design. You will have to figure out where and what. It is not something like "def main():"! it is a logical step which must be done. Check the reading about flags. Make SURE this step is in your final code!

Add a label to the output at the end, that explains what the "yes" means. Add one for the "no" also.

Sample output:

Your program should work for any 5 numbers entered, not just these.

Submit this program with your name at the top of the file using the link at the top of the page. Use the Lab 6 and Code menu choices.

Log off properly - you don't want your account misused by someone else!

Remember NOT to leave files on the local hard drives in this lab or anywhere else on campus! Make sure you save your projects onto a portable storage device you take with you!

Explanation / Answer

package com.he.capillary.chegg1;

import java.util.Scanner;

public class TestMenu {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       testInput(sc);
   }

   public static void testInput(Scanner sc) {
       boolean flag = true;
       System.out.println("Enter number : ");
       int num = sc.nextInt();
       int var = 0;
       for (int i = 0; i < 4; i++) {
           System.out.println("Enter number : ");
           var = sc.nextInt();
           if (num < var) {
               flag = false;
           }
           num = var;
       }
       if (flag) {
           //when numbers are in decreasing order.
           System.out.println("yes");
       } else {
           //when numbers are not in decreasing order.
           System.out.println("no");
       }
   }
}

Let me know if you need any modification. I have added only condition when we have to set flag false according to question.