Thanks. though I think the problem should have been more clear on what difference was, it felt like a very unclear tip. I had to use more maths here then coding since i knew that the absolute value is the distance between a point and origo.
I've created a program for something called eudoxus recursive series which is like this:
http://mathbin.net/56527
and this is the code
Code:
import javax.swing.*;
public class eudoxesseries {
public static void main(String[] arg) {
String k = JOptionPane.showInputDialog(null,"För vilket n?");
if (k == null)
System.exit(0);
long v = Long.parseLong(k);
long n = 0;
long an = 0;
long bn = 0;
long divison = 0;
long anneg1 = 1;
long bnneg1 = 1;
while (n < v ) {
an = anneg1 + bnneg1;
bn = an + anneg1;
bnneg1 = bn;
anneg1 = an;
n = n+1;
}
divison = bn/an;
JOptionPane.showMessageDialog(null, "För n = " + k + ": " + "\n" + "an= " + an + "\n" + "bn= " + bn + "\n"
+ "an/bn= " + divison);
System.exit(0);
}
}
however when I use long and when it calculates the divison it won't come out with decimals. i've tried putting divison = (double) bn/an;
but eclipse won't let me, it says cannot convert from double to long :S
So what do you guys think, Is the program fine or can i make changes to it to make it look cleaner?
But yeah primarily, how do i get division to work with long and can I improve the program in any way?
edit: about the swedish, it's only to type in for what n you wish the program to calculate for.
public class eudoxesseries {
public static void main(String[] arg) {
String k = JOptionPane.showInputDialog(null,"För vilket n?");
if (k == null)
System.exit(0);
long v = Long.parseLong(k); //kommer verka som gränsen för hur långt loopen skall gå.
long n = 0;
long an = 0;
long bn = 0;
double divison;
long anneg1 = 1; //startvärdet för an
long bnneg1 = 1; //startvärdet för bn
while (n < v ) {
an = anneg1 + bnneg1; //formeln an = an-1 + bn-1
bn = an + anneg1; // formeln bn = an + an-1
bnneg1 = bn; // görs om för att verka som bn-1 i nästa fall
anneg1 = an; // görs om för att verka som an-1 i nästa fall.
n = n+1; // räknare som håller reda på vilket n vi är på.
}
divison = (double) bn/an; // utför divisionen bn/an
JOptionPane.showMessageDialog(null, "För n = " + k + ": " + "\n" + "an= " + an + "\n" + "bn= " + bn + "\n"
+ "an/bn= " + divison); // skriver ut resultatet.
System.exit(0);
}
}
just made division as a double instead and since an and bn are of the type long i just put (division) infront. thus division = (double) bn/an;
But yeah, i wanna use long so an and bn don't come out as for example: 6.134535E18.
So yeah, been some time since I posted in this thread but I thought i'd ask what books you can recommend on learning java?
I've learned a bit now, like how to use loops, arrays, a bit about objects etc so I have a pretty much basic understanding of java. Now I feel like I need a good book and I have no idea which ones that are good. Any ideas?
Signature/Avatar nuking: none (can be changed in your profile)
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum