Explore BrainMass

Explore BrainMass

    Two Java programs showing iteration

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    1. Write a program that ask the user for starting value and an ending value and then writes all the integers (inclusive) between those two values

    Sample output:
    Enter Start:
    5
    Enter End:
    9

    5 6 7 8 9

    2. Write a program that ask the user to enter a word. The program will then repeat word for as many time as it has characters:

    Sample output:

    Enter a word:
    Hello

    Hello
    Hello
    Hello
    Hello
    Hello

    © BrainMass Inc. brainmass.com December 24, 2021, 8:25 pm ad1c9bdddf
    https://brainmass.com/computer-science/java/two-java-programs-showing-iteration-275832

    SOLUTION This solution is FREE courtesy of BrainMass!

    This posting was asking for two different Java problems. Both of the problems demonstrate how to use a for loop.

    The first problem asks the user to enter two integer values and then prints all of the integers between them. Asking for user input is done with the Scanner class as follows:

    <pre>
    Scanner in = new Scanner(System.in);
    System.out.print("Enter Start: ");
    start = in.nextInt();
    </pre>

    Once this is done for both inputs the input is checked for validity
    then a for loop is run to print all of the values.

    <pre>
    if (stop < start) {
    System.out.println("ERROR: stop cannot be less than start");
    } else {
    for (int i=start;i<=stop;i++) {
    System.out.print(i + " ");
    }
    System.out.println();
    }
    </pre>

    The second problem is similar. The user is asked to enter a word and then the length of the word is used as the stop condition for the for loop.

    The source code to both programs is attached to this posting.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    © BrainMass Inc. brainmass.com December 24, 2021, 8:25 pm ad1c9bdddf>
    https://brainmass.com/computer-science/java/two-java-programs-showing-iteration-275832

    Attachments

    ADVERTISEMENT