如何获取Java用户输入?

本文翻译自:How can I get the user input in Java?

I attempted to create a calculator, but I can not get it to work because I don't know how to get user input . 我尝试创建一个计算器,但由于不知道如何获得用户输入而无法使它正常工作。

How can I get the user input in Java? 如何获取Java用户输入?


#1楼

参考:https://stackoom.com/question/MBWs/如何获取Java用户输入


#2楼

Add throws IOException beside main() , then main()旁边添加throws IOException ,然后

DataInputStream input = new DataInputStream(System.in);
System.out.print("Enter your name");
String name = input.readLine();

#3楼

Here, the program asks the user to enter a number. 在此,程序要求用户输入一个数字。 After that, the program prints the digits of the number and the sum of the digits. 之后,程序将打印数字的位数和数字的总和。

import java.util.Scanner;

public class PrintNumber {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = 0;
        int sum = 0;

        System.out.println(
            "Please enter a number to show its digits");
        num = scan.nextInt();

        System.out.println(
            "Here are the digits and the sum of the digits");
        while (num > 0) {
            System.out.println("==>" + num % 10);
            sum += num % 10;
            num = num / 10;   
        }
        System.out.println("Sum is " + sum);            
    }
}

#4楼

Here is your program from the question using java.util.Scanner : 这是您使用java.util.Scanner

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        int input = 0;
        System.out.println("The super insano calculator");
        System.out.println("enter the corrosponding number:");
        Scanner reader3 = new Scanner(System.in);
        System.out.println(
            "1. Add | 2. Subtract | 3. Divide | 4. Multiply");

        input = reader3.nextInt();

        int a = 0, b = 0;

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter the first number");
        // get user input for a
        a = reader.nextInt();

        Scanner reader1 = new Scanner(System.in);
        System.out.println("Enter the scend number");
        // get user input for b
        b = reader1.nextInt();

        switch (input){
            case 1:  System.out.println(a + " + " + b + " = " + add(a, b));
                     break;
            case 2:  System.out.println(a + " - " + b + " = " + subtract(a, b));
                     break;
            case 3:  System.out.println(a + " / " + b + " = " + divide(a, b));
                     break;
            case 4:  System.out.println(a + " * " + b + " = " + multiply(a, b));
                     break;
            default: System.out.println("your input is invalid!");
                     break;
        }
    }

    static int      add(int lhs, int rhs) { return lhs + rhs; }
    static int subtract(int lhs, int rhs) { return lhs - rhs; }
    static int   divide(int lhs, int rhs) { return lhs / rhs; }
    static int multiply(int lhs, int rhs) { return lhs * rhs; }
}

#5楼

Use the System class to get the input. 使用System类获取输入。

http://fresh2refresh.com/java-tutorial/java-input-output/ : http://fresh2refresh.com/java-tutorial/java-input-output/

How data is accepted from keyboard ? 如何从键盘接受数据?

We need three objects, 我们需要三个对象,

  1. System.in 系统输入
  2. InputStreamReader InputStreamReader
  3. BufferedReader 缓冲读取器

    • InputStreamReader and BufferedReader are classes in java.io package. InputStreamReader和BufferedReader是java.io包中的类。
    • The data is received in the form of bytes from the keyboard by System.in which is an InputStream object. System.in是InputStream对象,它从键盘接收字节形式的数据。
    • Then the InputStreamReader reads bytes and decodes them into characters. 然后,InputStreamReader读取字节并将其解码为字符。
    • Then finally BufferedReader object reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. 然后,最终BufferedReader对象从字符输入流中读取文本,对字符进行缓冲,以提供对字符,数组和行的有效读取。
InputStreamReader inp = new InputStreamReader(system.in);
BufferedReader br = new BufferedReader(inp);

#6楼

It is very simple to get input in java, all you have to do is: 在Java中获取输入非常简单,您要做的就是:

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);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值