Try It Yourself: Console I/O

  1. Write a program that computes the sum of any number of integers given by the user. The program should prompt the user for the next number until the user enters 'q'. The program should then report the sum of the numbers.

    Example execution:
    Enter an integer: 20
    Enter an integer: 5
    Enter an integer: -10
    Enter an integer: q
    The sum is 15
    


  2. Write a code fragment that repeatedly prompts the user for a directory and prints out the number of items in the directory and the contents of the directory. For each item contained in the directory, if the item is itself a directory, the directory name should be printed (with a slash, '\', at the end of it) followed by the number of items in that directory. If the item is a file, the file name should be printed unless the file name starts with a '.' (a dot, or period).

    Example execution:
    Please enter a directory: U:\private
    
    Contents of U:\private  6 items
    	test.txt
    	Notes\  10 items
    	CodeWarrior\  5 items
    	temp.html
    
    Again? y
    Please enter a directory: U:\private\temp.html
    U:\private\temp.html is not a directory
    Please enter a directory: U:\private\Notes
    
    Contents of U:\private\Notes  10 items
    	Ch1.doc
    	Ch2.doc
    	Ch3.doc
    	Ch4.doc
    	Ch5.doc
    	Ch6.doc
    	Ch7.doc
    	Ch8.doc
    	Ch9.doc
    	Ch10.doc
    
    Again? y
    Please enter a directory: U:\private\CodeWarrior\HeroesQuest
    
    Contents of U:\private\CodeWarrior\HeroesQuest  5 items
    	Written4.txt
    	AppClasses.jar
    	HeroesQuest.java
    	HeroesQuest.mcp
    	HeroesQuest_Data\  3 items
    
    Again? n