I was wondering how I would go about retrieving a value assigned to int (or any primitive data type) in one ... object I think (I wouldn't mind some clairifacation on that either) to another.
I'm writing a program to calculate error for my physics labs.
Here if is :
/*
* author : Dawson Reid
* date : Feb 9th, 2010
* problem : To clalculate error with in labs
*/
package ErrorCalculator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Tittle
System.out.println("Welcome to the Error Calculator");
//Input type of error calculation required
System.out.println("Would you like to calculate : ");
System.out.println("1) avg error");
System.out.println("2) + or - error");
System.out.println("3) * or / error");
System.out.println("\nInput a number : ");
//Choice
int a = sc.nextInt();
}
/*
* Calculations to fallow
*/
public static void calc(String[] args) {
if (a == 1) {
}
}
}
In the "public static void calc" how would I make it recognize the value put into the "public static void main" for the value of a?
p.s - How do I place my code into the scroll box thingy others use?
well, about the a's value, just make calc to get integer parameter and call calc with a.
for the second question, select your code and click on the '#' button in the message box
Do you think you or someone else would be able to put up a short example of what you mean please.
You have to call the method "calc" and provide it with the a variable as a parameter to the method. I have bolded the portions of your code that I changed to make it clear for you.
Code:* author : Dawson Reid * date : Feb 9th, 2010 * problem : To calculate error with in labs */ package ErrorCalculator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Tittle System.out.println("Welcome to the Error Calculator"); //Input type of error calculation required System.out.println("Would you like to calculate : "); System.out.println("1) avg error"); System.out.println("2) + or - error"); System.out.println("3) * or / error"); System.out.println("\nInput a number : "); //Choice int a = sc.nextInt(); //You call the method "calc" here calc(a); } /* * Calculations to fallow */ //the (int a) portion of the method tells your program that this method always expects an int when called. //You provide it with the int a you received from the user. public static void calc(int a) { //You can now use "a" in your code below. if (a == 1) { } } }
Cool, thank you. Really helped.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks