Add code to test if r is negative and exit the program is it is.
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.
Add code to test if r is negative and exit the program is it is.
If you don't understand my answer, don't ignore it, ask a question.
mport java.util.Scanner;
public class a{
public static void main(String[] args){
for (double i = 0; i <9; i++) {
if (r < o)
System.out.println("r is negative");
else
System.out.println("positive");
Scanner scan=new Scanner(System.in);
double a,r,p;
System.out.println("Enter value of radius:");
r=scan.nextDouble();
a=3.14*r*r ;
p=2*3.14*r;
System.out.println("Area= "+a);
System.out.println("Perimeter = "+p);
}
}
}
?? where is my mistake >?
You need to understand that the statements in the program are executed in the same order as they are entered in the source. The first state is executed first, then the next one and then the next one.
At what statement in the source code is the r variable given a value typed in by the user? You can not test it for a negative value beFore the value has been entered. You must test its value after the value has been read in.
If you don't understand my answer, don't ignore it, ask a question.
ok where i have to but the if statement and why it is not working with my?
Did you understand what I wrote about the order that statements in a program are executed?
What will be the value of r when your if statement testing its value is executed?
What happens when you execute the program now? Copy the console and paste it here.
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
If you don't understand my answer, don't ignore it, ask a question.
import java.util.Scanner;
public class a{
public static void main(String[] args){
for (double i = 0; i <9; i++) {
Scanner scan=new Scanner(System.in);
double a,r,p;
System.out.println("Enter value of radius:");
r=scan.nextDouble();
a=3.14*r*r ;
p=2*3.14*r;
System.out.println("Area= "+a);
System.out.println("Perimeter = "+p);
if ( r < o)
System.out.println("r is negative");
else
System.out.println("positive");
}
}
}
am using Dr java .. i understand what you say but am confusing
What does the console look like when you execute the program? Please copy and paste it here.
Can you explain what the problem is with the way the program is executing?
If you don't understand my answer, don't ignore it, ask a question.
1 error found:
File: C:\Users\Quick age\Desktop\a.java [line: 21]
Error: C:\Users\Quick age\Desktop\a.java:21: cannot find symbol
symbol : variable o
location: class a
the console :
nothing appear because of the problem which is in the code
Define the variable o in class a so it can be used on line 21.
What code is on line 21?
If you don't understand my answer, don't ignore it, ask a question.
if ( r < o)
System.out.println("r is negative");
else
System.out.println("positive");
Why are you comparing the value of the variable r to the variable: o?
If you don't understand my answer, don't ignore it, ask a question.
because if it is less than zero it will equal negative
can you help me to done this step if you allow ?
i have to done the program
Check your keyboard. A zero(0) is different from the letter(o)
If you don't understand my answer, don't ignore it, ask a question.
thanks my frend but how i can stop the program when i unter negative number ?>
See post #12
If you don't understand my answer, don't ignore it, ask a question.
where i have to put System.exit(0);?
Put it at the place in the code where you want the program to stop executing.
If you don't understand my answer, don't ignore it, ask a question.
it is not working
import java.util.Scanner;
public class a{
public static void main(String[] args){
for (double i = 0; i <9; i++) {
Scanner scan=new Scanner(System.in);
double a,r,p;
System.out.println("Enter value of radius:");
r=scan.nextDouble();
a=3.14*r*r ;
p=2*3.14*r;
System.out.println("Area= "+a);
System.out.println("Perimeter = "+p);
if ( r < 0)
System.out.println("r is negative");
System.exit(0);
else
System.out.println("positive");
}
}
}
4 errors found:
File: C:\Users\Quick age\Desktop\a.java [line: 26]
Error: C:\Users\Quick age\Desktop\a.java:26: 'else' without 'if'
File: C:\Users\Quick age\Desktop\a.java [line: 27]
Error: C:\Users\Quick age\Desktop\a.java:27: <identifier> expected
File: C:\Users\Quick age\Desktop\a.java [line: 27]
Error: C:\Users\Quick age\Desktop\a.java:27: illegal start of type
File: C:\Users\Quick age\Desktop\a.java [line: 30]
Error: C:\Users\Quick age\Desktop\a.java:30: class, interface, or enum expected
You should always enclose code in if statements and loops in {}. Your code has left off the {} to enclose the code in the if statement.
If you don't understand my answer, don't ignore it, ask a question.
is it ok now ?
import java.util.Scanner;
public class a{
public static void main(String[] args){
for (double i = 0; i <9; i++) {
Scanner scan=new Scanner(System.in);
double a,r,p;
System.out.println("Enter value of radius:");
r=scan.nextDouble();
a=3.14*r*r ;
p=2*3.14*r;
System.out.println("Area= "+a);
System.out.println("Perimeter = "+p);
if ( r < 0){
System.out.println("r is negative");
System.exit(0);
}
else
System.out.println("positive");
}
}
}
Does it execute the way you want?
One suggestion: Use the Math class's PI variable for a more accurate value for pi.
If you don't understand my answer, don't ignore it, ask a question.
thecraetiveman (May 4th, 2012)
thank you very math my friend ok i will do it
it look show the result and close directly >>
Hi ,
One more thing that u can do is to use while loop and check for r being negative while(r>0) if so get the input from user and print the values of are a nad perimeter.
By the way ur area would come wrong.
Regards,
Devesh
thanks my dear i use your way and it successful thanks for you ...