Help with an assignmen please.
Question:Write a program that gets two numbers from the user, a and b, you may assume that a<b. It then prints the squares of all the numbers in the inclusive range [a, b].
This is the question for my assignment and I am completely lost, I learned some of the concepts but I just don't know how to apply it to this.
my code:
Code :
import java.util.*;
import javax.swing.*;
public class Ch4Q11 {
public static void main (String[] args) {
int n;
int m;
int accumulator;
final int high = m, low = n;
String valuea, valueb;
valuea = JOptionPane.showInputDialog("Write first number here: ");
n = Integer.parseInt(valuea);
int amount = high - low + 1;
while (accumulator >= 0) {
valueb = JOptionPane.showInputDialog("Write second number here: ");
m = Integer.parseInt(valueb);
System.out.print(accumulator + " ");
System.out.println(Math.sqrt(accumulator) + " " );
n--;
}
}
}
as you can see I'm not even close.. I would really appreciate it if someone could help me with this :)
Thank you,
Harris
Re: Help with an assignmen please.
accumulator? I haven't seen that since I was doing stuff with assembly :P
Ok, the first issue I see is that you're declaring final integers high and low, and assigning them the values of m and n, which haven't been initialized yet. In Java (unlike other languages), the Java compiler will always complain if you don't initialize your variables.
Now, the program logic should look something like this:
1. Get the a and b.
2. for all integers i starting at a and going to b (inclusive), print out i*i
Here's how to get the for-loop part:
Code :
for (int i = a; i <= b; i++)
{
System.out.println(i + " squared is " + i*i);
}
Re: Help with an assignmen please.
Thank you very much helloworld922, your post was really helpful :)
I'm actually taking a high school course so it's nothing complicated but that is the way they want us to do it.
Regards,
Harris