【发布时间】:2012-03-17 19:02:30
【问题描述】:
该程序不会在 netBeans 上运行或在终端中编译...我认为我本身没有任何错误.. 这个程序需要显示一个圆的面积 不能有任何无效输入(字母、&^#、..等)
请并感谢您的宝贵时间;)
**import java.util.Scanner;
/**
*
* @author Omar Sugule
*/
public class AreaCircle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
try {
double r = sc.nextDouble();
}
catch( NumberFormatException e ) {
System.err.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
}**
【问题讨论】:
-
所以您根本没有看到任何编译错误?当你运行它时会发生什么?您可能应该告诉我们更多有关您的问题的重要细节。
-
当你说它“不会编译”时,你的意思是它给你一个编译错误吗?如果是这样 - 您不认为发布该错误会有所帮助吗?
-
您似乎忘记发布您收到的错误消息
-
如果您的代码发布开头和结尾的“**”实际上在您的文件中,那么这就是您的答案。
-
对我有用,只是在我输入一个数字之前它不会提示输入,该数字被忽略,然后提示输入半径。那么它工作正常。