Thank you for your help in advance!
I'm new to programming and decided that I would make a basic console based ohms law calculator as practice. The way my code works currently is that you have to answer at least one value as "0" to find for that value... I've been trying to find a way to allow a user to skip past the desired value during the input phase, but have not come up with a way to do that and still use a "double" variable. Is there a way to allow the user to just press [Enter] to continue on to the next input value and still tell the program to find for that value?
Also, I've noticed that when I am finding for voltage and any other value I get a dummy voltage value (examples shown below)... anyone know where I went wrong? Can you point me in the right direction to research this issue?
Again, Thank you for your help.
Shaun
My Code:
package ohmslaw; import java.util.Scanner; public class Ohmslaw { public static void main(String[] args) { Scanner value = new Scanner (System.in); //declaring base variables for voltage (v), current (i), resistance (r), and power (p). double v; double i; double r; double p; //declaring v,i,r,p formula names. double voltage; double pivoltage; double prvoltage; double current; double pvcurrent; double prcurrent; double resistance; double vpresistance; double piresistance; double power; double irpower; double vrpower; //scanner inputs to initialize base variables for formulas. System.out.println("Voltage?"); v = value.nextDouble(); System.out.println("Current?"); i = value.nextDouble(); System.out.println("Resistance?"); r = value.nextDouble(); System.out.println("Power?"); p = value.nextDouble(); //v,i,r,p formulas. voltage = i*r; pivoltage = p/i; prvoltage = Math.pow(p*r, 0.5); current = v/r; pvcurrent = p/v; prcurrent = Math.pow(p/r, 0.5); resistance = v/i; vpresistance = (v*v)/p; piresistance = p/(i*i); power = v*i; irpower = (i*i)*r; vrpower = (v*v)/r; // print statements and formula logic. //voltage if(v==0){ System.out.println("Voltage: " + voltage); } if(v==0 && r==0){ System.out.println("Voltage: " + pivoltage); } if(v==0 && i==0){ System.out.println("Voltage: " + prvoltage); } //current if(i==0){ System.out.println("Current: " + current); } if(i==0 && r==0){ System.out.println("Current: " + pvcurrent); } if(i==0 && v==0){ System.out.println("Current: " + prcurrent); } //resistance if(r==0){ System.out.println("Resistance: " + resistance); } if(r==0 && i==0){ System.out.println("Resistance: " + vpresistance); } if(r==0 && v==0){ System.out.println("Resistance: " + piresistance); } //power if(p==0){ System.out.println("Power: " + power); } if(p==0 && v==0){ System.out.println("Power: " + irpower); } if(p==0 && i==0){ System.out.println("Power: " + vrpower); } } }
Example of double output (testing for voltage and resistance):
Voltage?
0
Current?
2.5
Resistance?
0
Power?
15
Voltage: 0.0
Voltage: 6.0
Resistance: 0.0
Resistance: 2.4