Add code to test if r is negative and exit the program is it is.
Printable View
Add code to test if r is negative and exit the program is it is.
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.
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.
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?
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 ( 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?
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)
thanks my frend but how i can stop the program when i unter negative number ?>
See post #12
where i have to put System.exit(0);?
Put it at the place in the code where you want the program to stop executing.
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.
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.
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 ...