Hack Forums
[Java] GCD java code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] GCD java code (/showthread.php?tid=4468569)



GCD java code - jroh97 - Sep 29, 2014

Here is my code.

import java.util.*;
public class GCD
{
public static void main (String args [] )
{
Scanner reader = new Scanner(System.in);
int a, b, x, d = 0, c = 0;
String response;
do
{
System.out.println("Please enter first number to find GCD.");
a = reader.nextInt();
System.out.println("Please enter second number to find GCD.");
b = reader.nextInt();

for (x = a-1; x>=1; x--)
{
c = a%x;
if (c==0)
{
d = b%x;
if (d==c)
{
break;
}
}
}

System.out.println(x);

System.out.println("Would you like to go again?");
response = reader.nextLine();
response = reader.nextLine();
}while ( response.equals("yes") );


System.out.println("Have a nice day.");
}
}


Why doesn't it work. We haven't learned the methods so we have to use loops. Can anyone help me out?


* - koolkid - Sep 29, 2014

Firstly OP please edit ur post and use the Code Tags whicj makes ur code readable


* - Kody1169 - Sep 29, 2014

What do you mean it "doesn't work"?

I think you should fix this for some reason...it's like a pet peeve.

Code
while ( response.equals("yes"));
I'd make that ignorecaps...or...

Code
response = reader.nextLine().toLowerCase();

That way instead of
Code
Would you like to go again?
YeS
Have a nice day.
It does...
Code
Would you like to go again?
YeS
Please enter first number to find GCD.