Question:
Why won't my script let me use these as variables without setting a value?
2011-09-24 20:55:26 UTC
*`~*`~ The variables that i'm having trouble with are F and D, eclipse is telling me that I need to initialize a value for them both, but for some reason not d. And my issue with this is that I want it to be interactive, and if it's asking for an initial value ... i don't understand how It's possible to make an interactive calculator. If there's a way around it ... BTW i'm fairly new to programming so if the reason is obvious please just tell me ... I would REALLY appreciate feed back *`~*`~

import java.util.Scanner;

public class practice4
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);

double F, d, D;
String A, B, C, x;

d = (1 / ((1/F) - (1/D)));
F = (1 / ((1/D) + (1/d)));
D = (1 / ((1/F) - (1/d)));

System.out.print("Do you want to solve for Focal(A), DistanceImmage(B) or DistanceObject?(C):");
x = s.nextLine();

if(x == "A")
{
System.out.print("Distance of image: ");
D = s.nextDouble();
System.out.print("Distance of object: ");
d = s.nextDouble();
System.out.print("Focal length= " + F);
}
if(x == "B")
{
System.out.print("Focal length: ");
F = s.nextDouble();
System.out.print("Distance of image: ");
d = s.nextDouble();
System.out.print("Distance of object= " + D);
}
if(x == "C")
{
System.out.print("Focal length: ");
F = s.nextDouble();
System.out.print("Distance of object: ");
D = s.nextDouble();
System.out.print("Distance of image= " + d);
}
}
}
Three answers:
Ratchetr
2011-09-24 21:16:52 UTC
You're making a mistake I see fairly often here.

These 3 lines:



d = (1 / ((1/F) - (1/D)));

F = (1 / ((1/D) + (1/d)));

D = (1 / ((1/F) - (1/d)));



As Silent already said, what do you think the values will be when the variables haven't been assigned a value yet?



My guess is that you (and the many others that make this mistake) are thinking that it works like a spreadsheet. d F and D will get recalculated anytime you change one of the variables in the formula.



No...it doesn't work that way in programming. You need to read the input variables FIRST. THEN apply the formula.



So, for x == "A" (Hum...you need x.equals("A") here, don't you? == for strings is broken in Java).

AFTER you read D and d, then, AND ONLY THEN can you compute F = (1 / ((1/D) + (1/d)));



Also: Java allows way more than 1 character variable names. DON'T USE 1 CHARACTER VARIABLE NAMES!!! (Except for loop counters, i, j, k are OK).



Better names would be:

imageDistance

objectDistance

focalLength



Having d and D as 2 variables is WAY too confusing. Don't do that.
Silent
2011-09-25 04:04:34 UTC
Well, take a look at this line:



d = (1 / ((1/F) - (1/D)));



You have not yet given values to the variables F and D. What would you expect this code to do? You can't compute 1/F until you know what F is. You need to move these computations to later points in the code, after you've gotten the values from the user.
Grant
2011-09-25 04:12:37 UTC
You are doing 1/D and 1/F but the computer doesn't know what D or F are so it cant go on... I would recommend just setting them to '1' first so it can use them. Hope this helps...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...