0

So what this program does is take two numbers as input using the Scanner class, and calculates the lowest common multiple of those two numbers. Everything seems to be working, except the lcm method won't return anything. I may have messed something up with my 'break' statements, but I didn't know any other way to get out of the if statement that is nested in a while loop. One more question: Is using a while(True) loop good practice or bad practice? Because I've seen lots of mixed opinions on it. If anyone has any better alternatives to while(True) loops I'd be happy to hear them. Thank you!

// LCM Calculator
// Author: Ethan Houston
// Language: Java
// Date: 2013-12-27

import java.io.*;
import java.util.Scanner;

public class lcm {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("This is a LCM calculator\n");
        System.out.println("Enter your first number: ");
        int firstNumber = scanner.nextInt();
        System.out.println("Enter your second number: ");
        int secondNumber = scanner.nextInt();
        lcm(firstNumber, secondNumber);

    }

    public static int lcm(int one, int two) {
        int counter = Math.min(one, two);
        int initialCounter = counter;
        boolean running = true;
        while (running) {
            if (counter % one == 0 && counter % two == 0) {
                break;

            } else {
                counter += initialCounter;
            }

        }
        return counter;
    }
}
ethanzh
  • 133
  • 2
  • 5
  • 19
  • `while (true)` will run forever. You will have to change the value of `running` at some point of time. – Shashank Kadne Dec 27 '13 at 11:37
  • 1
    @ShashankKadne There's a break inside. :) – Leri Dec 27 '13 at 11:38
  • 1
    You're doing nothing with the value returned by the `lcm` method. Try to print it : `System.out.println(lcm(firstNumber, secondNumber));` – Alexis C. Dec 27 '13 at 11:39
  • @Leri : Thanks,I think I need some coffee..:) – Shashank Kadne Dec 27 '13 at 11:39
  • `If anyone has any better alternatives to while(True) loops I'd be happy to hear them` There is but not alternative: `while (!yourConditionForBreak) { counter += initialCounter; }` The correct construct of `while` loop is `while (conditionWhenLoopShouldExecute) {}` – Leri Dec 27 '13 at 11:43

3 Answers3

3

You do return something, you're just not printing it. Just try:

System.out.println(lcm(firstNumber, secondNumber));
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • Okay so I actually have to print it? So it isn't like Python where you can just 'return' a value? – ethanzh Dec 27 '13 at 15:37
  • If you want it output to the screen you need to print it. Java programs don't output a return value by default. – Keppil Dec 27 '13 at 15:40
2

You are not printing the returned value

System.out.println(lcm(firstNumber, secondNumber));
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

As you had written,

lcm(firstnumber, secondnumber);

this method will return an int type value, but in your code, you are not obtaining the returned value in any variable.

So, you should write like this :

int variable=lcm(firstnumber, secondnumber);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Puspam
  • 2,137
  • 2
  • 12
  • 35