1

The last do..while loop only prints the 2^n of the number entered and n is user input. It does not print all 2^n from 1 to what ever value user inputted.

/**
 * 
 * 
 * @author
 * @version
 */

//import Scanner utility
import java.util.Scanner;
public class Loops
{
    public static void main(String [] args)
    {
         // For loop counts from 1 to 10 and prints results. 
        for(int counter = 1; counter <= 10; counter++)
        {
            System.out.print(counter + " ");
        }
        System.out.println();//Skips a line
       // For loop that counts from 10 to 1 and prints results.
        System.out.println(); //Skips a line
        for(int counter = 10; counter >= 1; counter--)
        {
            System.out.print(counter + " ");
        }
        System.out.println();//Skips a line
        
         //For loop that counts from 0 to 20 and prints results.  
        System.out.println();
        for(int counter = 0; counter <= 20; counter++)
        {
            System.out.print(counter + " ");//Skips a line
        }
        System.out.println();
         //for loop to print all numbers in between user inputs
        Scanner myScanner = new Scanner(System.in);

        int start;
        int end;
        System.out.println(); //Skips a Line
        System.out.println("Enter start value: ");
        String answer = myScanner.nextLine();
        start = Integer.parseInt(answer);
        System.out.println("Enter end value: ");
        String answer1 = myScanner.nextLine();
        end = Integer.parseInt(answer1);
       //Counts from what user states as start and end. Then prints results.
        for (; start <= end; start++)
        {
            System.out.println(" " + start);
        }
         // while Loop calculate numbers from 1 to 10 then prints results.
        int x = 1;
        System.out.println(); //Skips a line
        while(x<=10)
        {
            System.out.println(" " + x);
            x++;
        }    
       //while Loop calculate numbers from 10 to 1 then prints results.
        int y = 10;
        System.out.println(); // Skips a line
        while(y>=1)
        {
            System.out.println(" " + y);
            y--;
        }
       //While loop to print all numbers in between user inputs
        Scanner myScannerZ = new Scanner(System.in);

        int startz;
        int endz;
        System.out.println();//Skips a line
        System.out.println("Enter start value: ");
        String answer2 = myScannerZ.nextLine();
        startz = Integer.parseInt(answer2);
        System.out.println("Enter end value: ");
        String answer3 = myScannerZ.nextLine();
        endz = Integer.parseInt(answer3);
        //Counts from what user states as start and end. Then prints results.
        //repeat until startz equals endz
        while (startz <= endz)
        {
            System.out.println(" " + startz);
            startz++;
        }
        //Program counts power of 2 then prints results
        Scanner myScannerY = new Scanner(System.in);
        int N;
        int i = 1;                // count from 0 to N-1
        int powerOfTwo = 2;       // the nth power of two
        System.out.println();//Skips a line
        System.out.println("Enter a number: ");//User enters a value
        String answer4 = myScannerY.nextLine();
        N = Integer.parseInt(answer4);
        // repeat until i equals N
        while (i <= N) 
        {
            System.out.println(i + " " + powerOfTwo);   // print out the power of two
            powerOfTwo = 2 * powerOfTwo;                // double to get the next one
            i = i + 1;
        }
        //Do...While program that displays number from 1 to 10.
        System.out.println(); //Skips a Line
        int d= 1;
        do
        {
         System.out.print(" " + d ); //prints out numbers from 1 to 10
         d++;
        }
        while( d <= 10 ); 
        //program that calculates 2^N. N is user inputed
        Scanner myScannerG = new Scanner(System.in);
        int f;
        int r = 1;                // count from 0 to N-1
        int power = 2;       // the nth power of two
        System.out.println(); //Skips a line
        System.out.println("Enter a number: "); //User inputs a value
        String answer5 = myScannerG.nextLine();
        f = Integer.parseInt(answer5);
        do
        {
            r = r + 2;
            power = 2 * power;
           
        }
        while (r <= f);
        
            System.out.println(r + " " + power);
          
            
        
        
    }
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by JamesCherrill
1

That's because the print statement is outside the loop. The loop goes from line 121 to line 127, and iterates without printing anything. When it exits the loop it goes on to line 129 where it prints the last result.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.