jlopez3203
11-07-2008, 02:09 AM
hello I have been taking a course in java and doing horrible. Well, not horrible, I do the assigments, but ask tons of questions.
I am writing a program to return the conversion of meters to other units, I have the code part working, and was working on asthetics, I needed to break down into methods, thats where the problem came in.
I am using meters (double) and when I try to pass it off to another method it wont recognize the variable. Can someone please help?
thanks
import java.util.Scanner; //Needed for Scanner Class
/**
my name and infor here
*/
public class meterProblem
{
public static void main(String [] args)
{
double meters; // A number entered by the user
int number; //selection by the user
//Create Scanner object keyboard input
Scanner keyboard = new Scanner(System.in);
//Get number from user.
System.out.println("Enter a distance in meters: ");
meters = keyboard.nextDouble();
if (meters < 0)
{
System.out.println("Number has to be greater than 0, please enter meter: ");
meters = keyboard.nextDouble();
}
//Get Selection from user.
System.out.print("Enter your choice: ");
System.out.println("\n1. Convert to kilometers.\n" + "2. Convert to inches.\n" +
"3. Convert to feet.\n" + "4. Quit the program.");
number = keyboard.nextInt();
//Determine number entered.
switch (number)
{
case 1:
showKiloMeters();
break;
case 2:
showInches();
break;
case 3:
showFeet();
break;
case 4:
System.out.println("thanks, goodbye.");
application.shutdown();
default:
System.out.println("That's not 1,2,3, or 4.");
break;
}
}
public static void showKiloMeters()
{
double kiloMeters = 0.00;
kiloMeters = (meters * 0.001);
System.out.print(meters + " meters is " + kiloMeters + " Kilometers.");
}
public static void showInches()
{
double showInches = 0.00;
showInches = (meters * 39.37);
System.out.print(meters + " meters is " + showInches + " inches.");
}
public static void showFeet()
{
double feet = 0.00;
feet = (meters * 3.281);
System.out.print(meters + " meters is " + feet + " feet.");
}
}
I am writing a program to return the conversion of meters to other units, I have the code part working, and was working on asthetics, I needed to break down into methods, thats where the problem came in.
I am using meters (double) and when I try to pass it off to another method it wont recognize the variable. Can someone please help?
thanks
import java.util.Scanner; //Needed for Scanner Class
/**
my name and infor here
*/
public class meterProblem
{
public static void main(String [] args)
{
double meters; // A number entered by the user
int number; //selection by the user
//Create Scanner object keyboard input
Scanner keyboard = new Scanner(System.in);
//Get number from user.
System.out.println("Enter a distance in meters: ");
meters = keyboard.nextDouble();
if (meters < 0)
{
System.out.println("Number has to be greater than 0, please enter meter: ");
meters = keyboard.nextDouble();
}
//Get Selection from user.
System.out.print("Enter your choice: ");
System.out.println("\n1. Convert to kilometers.\n" + "2. Convert to inches.\n" +
"3. Convert to feet.\n" + "4. Quit the program.");
number = keyboard.nextInt();
//Determine number entered.
switch (number)
{
case 1:
showKiloMeters();
break;
case 2:
showInches();
break;
case 3:
showFeet();
break;
case 4:
System.out.println("thanks, goodbye.");
application.shutdown();
default:
System.out.println("That's not 1,2,3, or 4.");
break;
}
}
public static void showKiloMeters()
{
double kiloMeters = 0.00;
kiloMeters = (meters * 0.001);
System.out.print(meters + " meters is " + kiloMeters + " Kilometers.");
}
public static void showInches()
{
double showInches = 0.00;
showInches = (meters * 39.37);
System.out.print(meters + " meters is " + showInches + " inches.");
}
public static void showFeet()
{
double feet = 0.00;
feet = (meters * 3.281);
System.out.print(meters + " meters is " + feet + " feet.");
}
}