Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Finding square root

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finding square root

    anyone know how to make a program that will ask the user to enter a number and also a choice (1 for square or 2 for cube). It will then output the result? This is all I got so far and am lost:
    public static void main(String[] args) {
    		new Scanner(System.in);  
     
    		Scanner input = new Scanner(System.in);
    		float square;
     
    		System.out.println("Enter a number.");
    		System.out.println("Select 1 for square or 2 for cube: ");
    		square = input.nextFloat();
     
    		if(square>= 2)
    	{
    			System.out.println(square*2);
    		}
    	else 		{
    			System.out.println(square + " 	
    		}
     
     
    	}
    Last edited by helloworld922; October 18th, 2010 at 06:45 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Finding square root

    I have put some questions in your code. Answer those for me and I can help you from there. Who knows, maybe answer those questions will clear some things up for you while you answer them.

    Also, please put your code in java tags. Read my signature to figure out how.

    Here is the commented code, please answer these questions for me and we can proceed from there:
    public static void main(String[] args) 
    {
    	//This is a variable that you immediately garbage collecting
    	new Scanner(System.in);
     
    	//Why do you have the previous line if you have this?
    	Scanner input = new Scanner(System.in);
    	//Why a float?
    	float square;
     
    	System.out.println("Enter a number.");
    	//Where do you get the number the user enters?
    	System.out.println("Select 1 for square or 2 for cube: ");
    	//Is this supposed to be 1 or 2 or the number the user entered?
    	square = input.nextFloat();
     
    	//Why greater than or equal to 2?
    	if(square>= 2)
    	{
    		//Why multiply by 2?
    		System.out.println(square*2);
    	}
    	else 
    	{
    		//Why didn't you finish this line of code?
    		System.out.println(square + "
    	}
    }
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Finding the Average
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 16th, 2010, 07:58 PM
  2. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM
  3. [SOLVED] Finding acm package
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 12th, 2010, 11:50 AM
  4. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM
  5. Replies: 13
    Last Post: May 6th, 2009, 09:27 AM