简体   繁体   English

使用扫描仪类获取输入时遇到麻烦

[英]Have trouble getting an input using Scanner Class

I'm going through my Java text book and for some reason I cannot compile the following code. 我正在阅读Java教科书,由于某种原因,我无法编译以下代码。

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);
  }
}

I am getting the following error: 我收到以下错误:

 /tmp/java_H98cOI/ComputeAreaWConsoleInput.java:8: error: ';' expected
   Scanner sc = New Scanner(System.in);
                   ^
1 error  

What shall be done to compile the code? 怎样编译代码?

You've written: 你写了:

New Scanner(System.in);  

You N in New is capital. New N是资本。

The actual keyword is new and not New . 实际关键字是new而不是New

Solution: 解:

Change your line of code to: 将您的代码行更改为:

new Scanner(System.in);  

And there is another error. 还有另一个错误。

It should be: 它应该是:

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

and not 不是

sc.nextdouble(); 

Two changes to your program. 对程序进行两项更改。

Change New to new.Change the line 将New更改为new。

Scanner sc = New Scanner(System.in);

to

Scanner sc = new Scanner(System.in);

and other error in the program is scanning a double. 程序中的其他错误正在扫描双精度。 Please change double to Double.So change the below line 请将double更改为Double。因此请更改以下行

double radius = sc.nextdouble();

to

double radius = sc.nextDouble();

It should work fine! 它应该工作正常!

See my comment: Here is the fixed version of your code: 查看我的评论:这是您的代码的固定版本:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM