Results 1 to 4 of 4
- 11-08-2011, 07:03 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Program To Take Two Numbers and see if one is a factor of the other
I think this program compiles ok up to the point of my final if (where I say that lower is a factor of higher... Then it gives a lot of errors. I'm not sure why.
Once I've cracked this I have to add an extra line (using else) which says lower goes into higher X times, but with Y left over. I've been shown how to work out a remainder statement, but where does the remainder go or how do I store it? Is it enough to include % in that message?
Thanks in advance of your kindness!
Java Code:import javax.swing.JOptionPane; public class Factors { public static void main (String[] args) { String number1, number2; number1=JOptionPane.showInputDialog(null, "Enter first number:"); int num1 = Integer.parseInt(number1); number2=JOptionPane.showInputDialog(null, "Enter second number:"); int num2 = Integer.parseInt(number2); if (num1 < num2) { int higher = num2; int lower = num1; } else { int higher = num1; int lower = num2; } if (higher % lower == 0) { int division = higher / lower; JOptionPane.showMessageDialog(null, lower + " is a factor of " + higher + "( " + lower + " goes into " + higher + " exactly " + division + " times.)"); } System.exit(0); }
- 11-08-2011, 07:12 PM #2
Re: Program To Take Two Numbers and see if one is a factor of the other
Please post the full text of the errors if you need help correcting them.it gives a lot of errors.
One problem I see is that you have defined some variables inside of {} where other parts of the code can NOT see them.
Move the definitions out to be at the same level as where you want to use them in other pieces of code.
This is a problem in the scope of the definition. Variables inside of inner {} pairs are not known outside of those {}sLast edited by Norm; 11-08-2011 at 07:15 PM.
- 11-08-2011, 11:01 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Re: Program To Take Two Numbers and see if one is a factor of the other
I changed it as per your instructions but I still have problems.
It says
Factors.java:12: error: ' .class' expected
int higher=num2; (with an arrowhead under the h of higher)
Factors.java:12: error: not a statement
int higher=num2; (with an arrowhead under the i of int)
Factors.java:12: error: illegal start of expression
int higher=num2; (with an arrowhead under the = sign)
Factors.java:14: 'else' without 'if'
else (with an arrowhead under the e)
I have two questions - what am I doing wrong?
And I'm going to have to add a final statement which prints a message if the remainder is not 0 and says x goes into y so many times with this left over. I know % works out the remainder but how do I store this remainder?
Java Code:import javax.swing.JOptionPane; public class Factors { public static void main (String[] args) { String number1, number2; number1=JOptionPane.showInputDialog(null, "Enter first number:"); int num1 = Integer.parseInt(number1); number2=JOptionPane.showInputDialog(null, "Enter second number:"); int num2 = Integer.parseInt(number2); if (num1 < num2) int higher=num2; int lower=num1; else int higher=num1; int lower=num2; if (higher % lower == 0) { int division = higher / lower; JOptionPane.showMessageDialog(null, lower + " is a factor of " + higher + "( " + lower + " goes into " + higher + " exactly " + division + " times.)"); } System.exit(0); } }
- 11-08-2011, 11:06 PM #4
Re: Program To Take Two Numbers and see if one is a factor of the other
You should use {} with your if/else statements and indent nested code. When you do add the {}s you will see where the problem is.
Your code being all in a vertical column makes it hard to understand the nesting of statements with the if / else statements
Similar Threads
-
Factor listing issues
By skaterboy987 in forum New To JavaReplies: 10Last Post: 10-30-2011, 12:38 AM -
Program - Print row of 5 even numbers, 5 rows, last num 50
By An Alien in forum New To JavaReplies: 10Last Post: 12-20-2010, 12:47 PM -
need help with factor quadratic...
By frostkarrotor in forum New To JavaReplies: 3Last Post: 11-30-2010, 12:28 AM -
greatest prime factor
By java_prgr in forum New To JavaReplies: 2Last Post: 07-23-2010, 08:28 PM -
prime numbers program
By i contra i in forum New To JavaReplies: 9Last Post: 01-15-2009, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks