简体   繁体   English

使用扫描仪从用户输入中读取整数

[英]Reading integer from user input using scanner

In the program i am creating i am creating a menu for the user made by using integer variables, when the user selects one they will activate that option in the menu.在我正在创建的程序中,我正在为使用整数变量的用户创建一个菜单,当用户选择一个时,他们将激活菜单中的该选项。 I am trying to use readnextInt but it is not working and i am not sure why.我正在尝试使用 readnextInt 但它不起作用,我不知道为什么。 The code for the scanner is not working.扫描仪的代码不起作用。

import java.util.Scanner;


 public class username
      {
    public static void main(String[] args)
{


    {   
       int Floor;
       Floor = 0;

       int Destination;
       Destination = 0;

       int UseLift;
       int AuditReport;
       int ExitLift;
       int a;


       UseLift = 1;
       AuditReport = 2;
       ExitLift = 3;
       a = read.NextInt;


       Scanner b = new Scanner(System.in);
       int a = in.nextInt();

       if (a == 1)
      System.out.println(" Enter your name ");

       else if (a == 2)
    System.out.println("");

       else if(a == 3)
    System.out.println(" Please Exit Lift ");

{

This line has your error.这一行有你的错误。

Scanner b = new Scanner(System.in);
int a = in.nextInt();

Your scanner is called b, not in. Use b.nextInt() instead.您的扫描仪名为 b,而不是 in。请改用b.nextInt()

Scanner b = new Scanner(System.in);
int a = b.nextInt();

Also you have an errant Read.nextInt(), and you haven't defined Read.您还有一个错误的 Read.nextInt(),并且您还没有定义 Read。 I don't think that line is trying to do anything, so just delete it.我不认为该行试图做任何事情,所以只需删除它。

First, what is read ?首先,什么是read I don't see it defined as a Scanner anywhere.我在任何地方都没有看到它被定义为Scanner

Second, once read is defined ( Scanner read = new Scanner(System.in); ), then you can call nextInt() , not NextInt .其次,一旦定义了readScanner read = new Scanner(System.in); ),那么您就可以调用nextInt() ,而不是NextInt All methods need those parentheses when trying to call them.所有方法在尝试调用它们时都需要这些括号。

Errors :错误

The statement该声明

a = read.NextInt;

seems redundant.似乎多余。 variable read is undefined.变量read未定义。

a = read.NextInt;

Scanner b = new Scanner(System.in);
int a = in.nextInt();

Variable in is undefined but used to read nextInt() .变量in未定义但用于读取nextInt()

int a = in.nextInt();

It must be pointing to instance of Scanner class.它必须指向Scanner类的实例。 Replace it with b .b替换它。

You have defined a twice within the same scope.您在同一范围内定义a两次。 And hence your IDE must be showing a red line under因此,您的 IDE 必须在下面显示一条红线

int a;
...
...
Scanner b = new Scanner(System.in);
int a = in.nextInt();

And, lastly, not an error but your code does not follow JAVA naming conventions for variable names and class names.最后,不是错误,而是您的代码不遵循变量名和类名的 JAVA 命名约定。 For more details Read Java Naming Conventions有关更多详细信息,请阅读Java 命名约定

Corrections suggested :更正建议

  1. Remove a = read.nextInt;删除a = read.nextInt; as it seems redundant.因为这似乎是多余的。
  2. Remove int a;删除int a; as you are redefining it after Scanner instantiation.当您在Scanner实例化后重新定义它时。
  3. Change int a = in.nextInt();更改int a = in.nextInt(); to int a = b.nextInt();int a = b.nextInt();

Remaining code is correct just change this line "int a = in.nextInt();"剩下的代码是正确的,只需更改这一行“int a = in.nextInt();” to "int a = b.nextInt();"到“int a = b.nextInt();” as b is the name of your scanner and nextInt() is method belongs to Scanner class "in.nextInt()" is incorrect.因为 b 是您的扫描仪的名称,而 nextInt() 是属于 Scanner 类的方法“in.nextInt()”是不正确的。

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

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