简体   繁体   English

如何通过键盘在Java中引入数字

[英]how introduce numbers by keyboard in java

What do I have to do in order to introduce numbers by keyboard in Java on eclipse which are the libraries? 为了在eclipse上的Java中通过键盘引入数字,我该怎么做? What is the command to read that? 阅读该命令是什么? Do I have to write libraries or eclipse do it automatically? 我是否必须编写库或Eclipse自动执行?

This is simple program to get user input import java.util.Scanner; 这是获取用户输入的简单程序import java.util.Scanner;

class GetInputFromUser
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string");
      s = in.nextLine();
      System.out.println("You entered string "+s);

      System.out.println("Enter an integer");
      a = in.nextInt();
      System.out.println("You entered integer "+a);

      System.out.println("Enter a float");
      b = in.nextFloat();
      System.out.println("You entered float "+b);   
   }
}

Sounds like you're asking how to get a number input from the keyboard in Java...I think...It's hard to understand what you're asking. 听起来您在问如何从Java键盘中获取数字输入...我认为...很难理解您的要求。 If that's what you're asking, well, try this: 如果这是您要的,那么请尝试以下操作:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;

class InputNumber
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner in = new Scanner (System.in); 
        int number; 
        System.out.println ("enter a number"); 
        number=in.nextInt(); 
        System.out.println("You entered: " + number);
    }
}

You can use the IO library provided by Java. 您可以使用Java提供的IO库。 You need to import java.io.* for this. 为此,您需要import java.io.*

Create a BufferedReader which can take String as input. 创建一个可以将String作为输入的BufferedReader Once a String is input, you can parse it to whatever suits you, with a corresponding parser from the wrapper class. 输入String ,您可以使用包装类中的相应解析器将其解析为适合您的内容。

eg 例如

BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
String input = in.readLine();
int num = Integer.parseInt(input);

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

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