0

Like the title suggests. I wrote the code and it works perfect with the while loop. But when i add in the other 2 loops they don't all 3 print. I do not understand why the other 2 loops will not print but Java throws no red flags. So i assume the code is written good enough for it to accept but not correctly. the code is as below

package thebrain;


import java.util.Scanner;

public class TheBrain {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter the starting mile: ");
    int start = keyboard.nextInt();
    System.out.print("Enter the ending mile: ");
    int end = keyboard.nextInt();
    System.out.println();

    while (start <= end){
        double km = start * 1.609;
        System.out.println("miles: " + (start) + ", " + 
                "kilometers: " + (km));
        start += 1;
    }
    System.out.println();

    do { 
        double km = start * 1.609;
        start += 1;
        System.out.println("miles: " + (start) + ", " + 
                "kilometers: " + (km));

    } while (start <= end);

    for (int s=start; s <= end; s++){
        double km = start * 1.609;
        System.out.println("miles: " + (s) + ", " + 
                "kilometers: " + (km));
    }
  }

}
Frakcool
  • 10,088
  • 9
  • 41
  • 71

3 Answers3

0

It is because you're adding +1 each iteration on your loop to start, so when you get to this line:

while (start <= end);

start is already higher than end, so, it never enters but once (do-while enters once always) but in the for loop, it never enters.

You could create an auxiliary variable:

int aux = start; //Do this after you've read start from the user input

On your loops you'll need to increment aux instead of start:

aux += 1; //You can also write it as aux++; (Which I prefer to use)

Then before the do-while restart aux to the start value

aux = start;
do {
    //Your code here
} while (...);

And the same before the for loop


Aside note, does your class compiles?

public class the brain {

Java doesn't accepts spaces between variables / class names. Please follow the Java naming conventions, class names should start with an UpperCamelCase.

Also this might not be happening now, but could be related in future problems, be sure to check it out.

Community
  • 1
  • 1
Frakcool
  • 10,088
  • 9
  • 41
  • 71
0

By the time you get to the 2nd loop, start will already have exceeded end, so the conditions for the 2nd and 3rd loops will always be false.

Try this:

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter the starting mile: ");
int start = keyboard.nextInt();
System.out.print("Enter the ending mile: ");
int end = keyboard.nextInt();
System.out.println();

int wstart = start; //working variable;

while (wstart <= end){
    double km = start * 1.609;
    System.out.println("miles: " + (wstart) + ", " + 
            "kilometers: " + (km));
    wstart += 1;
}
System.out.println();

wstart = start; //resetting working variable

do { 
    double km = start * 1.609;
    wstart += 1;
    System.out.println("miles: " + (wstart) + ", " + 
            "kilometers: " + (km));

} while (start <= end);


//no resetting needed since you already have your iteration variable s here
for (int s=start; s <= end; s++){
    double km = start * 1.609;
    System.out.println("miles: " + (s) + ", " + 
            "kilometers: " + (km));
}
5tingr4y
  • 380
  • 1
  • 10
0

becose you're updating the value of variable start on the 3 loops by writing start++ ;

Save the value of start on an another variable, and use this new var before even loop to restaure the old value of start;

Updated code :

import java.util.Scanner;

public class brain {

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

        System.out.print("Enter the starting mile: ");
        int start2 = keyboard.nextInt();
        int start = start2;
        System.out.print("Enter the ending mile: ");
        int end = keyboard.nextInt();

        System.out.println();
        System.out.println(" A" + start);
        while (start <= end) {
            double km = start * 1.609;
            System.out.println("miles: " + (start) + ", "+ "kilometers: " + (km));
            start += 1;
        }


        start = start2;
        System.out.println();        
        System.out.println(" B" + start);
        do {
            double km = start * 1.609;            
            System.out.println("miles: " + (start) + ", "+ "kilometers: " + (km));
            start += 1;
        } while (start <= end);


        start = start2;
        System.out.println();
        System.out.println(" C" + start);
        for (int s = start; s <= end; s++) {            
            double km = s * 1.609;
            System.out.println("miles: " + (s) + ", "+ "kilometers: " + (km));
        }

    }
}
bilelovitch
  • 1,455
  • 14
  • 22