Created
November 30, 2012 20:50
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************** | |
* 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