使用Scanner Class获取输入时遇到问题

时间:2015-06-20 20:46:02

标签: java

我正在阅读我的Java教科书,由于某种原因,我无法编译以下代码。

urlbase

我收到以下错误:

import java.util.*; 
public class ComputeAreaWConsoleInput
{

  public static void main (String [] args)
  {
   //Create Scanner Obj
   Scanner sc = New Scanner(System.in);

   //Get Radius
   System.out.print("Please Enter the Radius: ");
   double radius = sc.nextdouble();
   //determine area
   double area = 3.14159 * radius * radius;
   //display results
   System.out.println("The Area of the Circle w/ radius(" + radius +") is: " 
   + area);
  }
}

编译代码应该怎么做?

3 个答案:

答案 0 :(得分:1)

您已写过:

New Scanner(System.in);  

N中的New是资本。

实际关键字为new而不是New

解决方案:

将您的代码行更改为:

new Scanner(System.in);  

还有另一个错误。

应该是:

sc.nextDouble();  // with 'D' capital  

不是

sc.nextdouble(); 

答案 1 :(得分:1)

对您的计划进行两项更改。

将新内容更改为新内容。更改行

Scanner sc = New Scanner(System.in);

Scanner sc = new Scanner(System.in);

程序中的其他错误是扫描双倍。请将double更改为Double.So更改以下行

double radius = sc.nextdouble();

double radius = sc.nextDouble();

它应该可以正常工作!

答案 2 :(得分:1)

请参阅我的评论:以下是您的代码的固定版本:

public static void main (String [] 
{
//Create Scanner Obj
Scanner sc = new Scanner(System.in);

//Get Radius
System.out.print("Please Enter the Radius: ");
double radius = sc.nextDouble();
//determine area
double area = 3.14159 * radius * radius;
//display results
System.out.println("The Area of the Circle w/ radius(" + radius +") is: " + area);
sc.close();  // DO NOT forget this
}