Hello, I would like to make a code that chooses 1 if user wants to compute area
ID: 3886712 • Letter: H
Question
Hello, I would like to make a code that chooses 1 if user wants to compute area or 2 to compute circumference
so I wrote my code but I don't know how to do first part- choose 1 or 2
Like, To compute area put 1, to compute circumference put 2
what code should I need to add? I will put my code below.
package classproject;
/**
*
* @author cheggquestion
*/
import java.util.Scanner;
public class Classproject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
double radius;
double PI = 3.14;
System.out.println("Enter the Radius :");
Scanner input = new Scanner(System.in);
radius = input.nextDouble();
if(radius <=0){
System.out.println("The value entered for radius is invalid. Please enter a positive interger");
}else{
double area = PI*radius*radius;
System.out.println("Area:"+area);
double circumference = PI*2*radius;
System.out.println("The circumference of the circle is" +circumference);
}
}
}
Explanation / Answer
Hi
I have updated the code and highlighted the code changes below.
Classproject.java
import java.util.Scanner;
public class Classproject {
public static void main(String[] args) {
// TODO code application logic here
double radius;
double PI = 3.14;
System.out.println("Enter the Radius :");
Scanner input = new Scanner(System.in);
radius = input.nextDouble();
if(radius <=0){
System.out.println("The value entered for radius is invalid. Please enter a positive interger");
}else{
System.out.println("Choose your choice 1.Compute Area 2.Compute Curcumference Enter your choice: ");
int choice = input.nextInt();
if(choice == 1) {
double area = PI*radius*radius;
System.out.println("Area:"+area);
} else if(choice == 2) {
double circumference = PI*2*radius;
System.out.println("The circumference of the circle is" +circumference);
}
}
}
}
Output:
Enter the Radius :
1
Choose your choice 1. Compute Area 2. Compute Curcumference
Enter your choice:
1
Area:3.14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.