Skip to content

Instantly share code, notes, and snippets.

@0x000000AC
Created November 30, 2012 20:50
Show Gist options
  • Save 0x000000AC/4178499 to your computer and use it in GitHub Desktop.
Save 0x000000AC/4178499 to your computer and use it in GitHub Desktop.
Ch. 2 of Book, explores using java.util.Scanner and checks for a favorite shape.
/***********************************************
* CirclePrompt.java
* Aaron P. Clark
*
* Created to understand java.util.Scanner and Ch 2.
* shows the area of a circle if you say it's your
* favorite shape when prompted. See P.32 in our
* text for the pseudocode
***********************************************/
import java.util.Scanner;
public class CirclePrompt
{
public static void main(String args[])
{
String favShape; // Declaring what types of variables this program will contain
float radius;
float area;
Scanner in = new Scanner(System.in); // Sets the scanner variable "in" to what is typed when prompted
System.out.println("Enter your favorite shape ");
favShape = in.nextLine();
if (favShape.equals("circle")) // I had to lookup how to use if with strings instead of numerics
{
System.out.println("Enter a radius value: ");
radius = in.nextFloat(); // You can also have in.nextInt or in.nextString based on variable type
area = (float) (3.14159*radius*radius); // I had to typecast this as a float to get it to work
System.out.println("The area of the circle is: " + area);
}
System.out.println("End of shape algorithm. Seeya!"); // If someone enters anything but a circle, they get here or when the if ends they get here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment