Number doubling program

Discussion in 'Mac Programming' started by soccersquirt82, Aug 25, 2008.

  1. macrumors 6502

    Joined:
    Mar 11, 2008
    #1
    I get an error: "The local variable y may not have been initialized." How would I initialize it?

    Code:
    package numberdoub;
    import java.util.*;
    public class Numberdoub
    {
    	static double redouble (double x, double y){
    		
    		//equation
    		while (y < 1000){
    		y = 2*x;}
    		return x;
    	}
    	
    	public static void main(String[] args) {
    		
    		//defines variable
    		double x, y;
    		
    		Scanner sc = new Scanner (System.in);
    		
    		//user input
    		System.out.print("x = ");
    		x = sc.nextDouble();
    
    		//answers
    		System.out.println("The answer is: " + y);		
    	}
    }
    
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    y = 0;
     
  3. macrumors 6502

    Joined:
    Jul 25, 2006
    #3
    The problem is, you never set y to anything in main() before you print it out. What do you expect the program to print out there?
     
  4. thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #4
    That would make the answer 0 which isn't what I want.
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I think i understand what you're supposed to do, which is read in a value from the user, and if that number is less than 1000, double it. If the result is less than 1000, double it again, and so on. Once you've gotten a value that is greater than or equal to 1000, display it.

    Your redouble function only needs to take one argument, the original value.

    You're pretty close on this one. After you've read a number from your scanner, you just need to pass it to your function.

    Your function to do the redoubling is almost there, but it needs some tweaks. It only needs to deal with a single value that will eventually be returned.

    In your main function, you can either print the result of that thing, or store it and print that.

    -Lee
     
  6. thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #6
    I tried to only have one variable, but then the answer is the same as what I put in. Here is the code:
    Code:
    package numberdoub;
    import java.util.*;
    public class Numberdoub
    {
    	static double redouble (double x){
    		
    		//rd method
    		while (x < 1000){
    		x = 2*x;}
    		return x;
    	}
    	
    	public static void main(String[] args) {
    		
    		//defines variable
    		double x;
    		
    		Scanner sc = new Scanner (System.in);
    		
    		//user input
    		System.out.print("x = ");
    		x = sc.nextDouble();
    
    		//answers
    		System.out.println("The answer is: " + x);		
    	}
    }
    
     
  7. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
     
  8. thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #8
    I think I used redouble in the right way. The answer is still the same, though.
    Code:
    package numberdoub;
    import java.util.*;
    public class Numberdoub
    {
    	static double redouble (double x){
    		
    		//rd method
    		while (x < 1000){
    		x = 2*x;}
    		return x;
    	}
    	
    	public static void main(String[] args) {
    		
    		double redouble = 0;
    		double x;
    		
    		Scanner sc = new Scanner (System.in);
    		
    		//user input
    		System.out.print("x = ");
    		redouble = sc.nextDouble();
    
    		//answers
    		System.out.println("The answer is: " + x);		
    	}
    }
    
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
     
  10. thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #10
    Like this?

    Code:
    package numberdoub;
    import java.util.*;
    public class Numberdoub
    {
    	static double redouble (double x){
    		
    		//rd method
    		while (x < 1000){
    		x = 2*x;}
    		return x;
    	}
    	
    	public static void main(String[] args) {
    		
    		double redouble = 0;
    		double x;
    						
    		Scanner sc = new Scanner (System.in);
    		
    		//user input
    		System.out.print("x = ");
    		redouble = sc.nextDouble();
    
    		x = redouble(x);
    		
    		//answers
    		System.out.println("The answer is: " + redouble(x));		
    	}
    }
    
     
  11. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #11
     
  12. thread starter macrumors 6502

    Joined:
    Mar 11, 2008
    #12
    IT WORKS!!!! YAY!! Thanks so much. Just one more to go!
    Code:
    package numberdoub;
    import java.util.*;
    public class Numberdoub
    {
    	static double redouble (double x){
    		
    		//rd method
    		while (x < 1000){
    		x = 2*x;}
    		return x;
    	}
    	
    	public static void main(String[] args) {
    		
    		double x;
    						
    		Scanner sc = new Scanner (System.in);
    		
    		//user input
    		System.out.print("x = ");
    		x = sc.nextDouble();
    
    		//answers
    		System.out.println("The answer is: " + redouble(x));		
    	}
    }
    
     
  13. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
     
  14. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #14
    No it wouldn't. When you declare a variable such as:

    double y;

    you have no idea what it contains. It is garbage, so what you do is initialise the variable so that it contains a safe value like so:

    double y = 0;

    that way you know what it contains from the start. Then you just put the new value that you want it to contain in as normal. It is good practice to always initialise variables to a safe value when you declare them.
     

Share This Page