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

The main function should consist of a processing loop that will call an input fu

ID: 652623 • Letter: T

Question

The main function should consist of a processing loop that will call an input function that reads in an integer value between 0 and 10. If the function returns back a value between 1 and 10 the main function should call a display function that will output that number of $ sign characters. If the input function returns back a 0 the loop should stop processing, otherwise the program should continue the loop and read in the next input value.

The input function reads in the input value and verifies that it is between 0 and 10. If it is not in this range you need to display an error message and prompt again for the input value. This must be done in a loop. You won

Explanation / Answer

There are several ways to implement it. The key points are

1. Accept user input

2. Validate user input for the given range of acceptable values

3. Print the character "$" for the particular number of times

I chose the programming language as Java. The program is very basic. You may enhance it to suit your needs

package com.test.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Display {

   public static void main(String[] args) {
       //Call a method to accept user input
       getUserInput();      
      
   }
  
   public static void getUserInput() {
       try {
           System.out.println(" ");
           System.out.println("Enter a number : ");
          
           //Read console input
           BufferedReader inBuf = new BufferedReader(new InputStreamReader(System.in));
           int counter = Integer.parseInt(inBuf.readLine());
          
           //Call the display character function
           displayCharacter(counter);
          
              
      
       } catch (IOException e) {
           System.out.println("An error occured while reading input : "+e.getMessage());
           e.printStackTrace();
       }
   }
  
   public static void displayCharacter(int times) {
      
       //Validate the input value for the range of acceptable values
      
       if(times == 0){
           System.out.println("You entered 0. The program will exit");
           System.exit(0);
       } else if(times > 10) {
           System.out.println("You entered an invalid number. Please enter a value between 0 and 10");
           getUserInput();
       }
      
       //Display the $ character according to the number of times
       do {
           for(int i = 0; i<times; i+=1) {
               System.out.print("$");
           }
           getUserInput();
       } while(times != 0);
      
   }

}

The output is:

--------------------

Enter a number :
2
$$

Enter a number :
3
$$$

Enter a number :
7
$$$$$$$

Enter a number :
11
You entered an invalid number. Please enter a value between 0 and 10


Enter a number :
10
$$$$$$$$$$

Enter a number :
0
You entered 0. The program will exit