How can I get the user input in Java?

2018-12-31 02:52发布

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?

24条回答
荒废的爱情
2楼-- · 2018-12-31 03:36
import java.util.Scanner;

public class userinput {
    public static void main(String[] args) {        
        Scanner input = new Scanner(System.in);

        System.out.print("Name : ");
        String name = input.next();
        System.out.print("Last Name : ");
        String lname = input.next();
        System.out.print("Age : ");
        byte age = input.nextByte();

        System.out.println(" " );
        System.out.println(" " );

        System.out.println("Firt Name: " + name);
        System.out.println("Last Name: " + lname);
        System.out.println("      Age: " + age);
    }
}
查看更多
低头抚发
3楼-- · 2018-12-31 03:37
import java.util.Scanner;

public class Myapplication{
     public static void main(String[] args){
         Scanner in = new Scanner(System.in);
         int a;
         System.out.println("enter:");
         a = in.nextInt();
         System.out.println("Number is= " + a);
     }
}
查看更多
ら面具成の殇う
4楼-- · 2018-12-31 03:37

This is a simple code that uses the System.in.read() function. This code just writes out whatever was typed. You can get rid of the while loop if you just want to take input once, and you could store answers in a character array if you so choose.

package main;

import java.io.IOException;

public class Root 
{   
    public static void main(String[] args)
    {
        new Root();
    }

    public Root()
    {
        while(true)
        {
            try
            {
                for(int y = 0; y < System.in.available(); ++y)
                { 
                    System.out.print((char)System.in.read()); 
                }
            }
            catch(IOException ex)
            {
                ex.printStackTrace(System.out);
                break;
            }
        }
    }   
}    
查看更多
零度萤火
5楼-- · 2018-12-31 03:40
Scanner input=new Scanner(System.in);
int integer=input.nextInt();
String string=input.next();
long longInteger=input.nextLong();
查看更多
萌妹纸的霸气范
6楼-- · 2018-12-31 03:41

You can make a simple program to ask for user's name and print what ever the reply use inputs.

Or ask user to enter two numbers and you can add, multiply, subtract, or divide those numbers and print the answers for user inputs just like a behavior of a calculator.

So there you need Scanner class. You have to import java.util.Scanner; and in the code you need to use

Scanner input = new Scanner(System.in);

Input is a variable name.

Scanner input = new Scanner(System.in);

System.out.println("Please enter your name : ");
s = input.next(); // getting a String value

System.out.println("Please enter your age : ");
i = input.nextInt(); // getting an integer

System.out.println("Please enter your salary : ");
d = input.nextDouble(); // getting a double

See how this differs: input.next();, i = input.nextInt();, d = input.nextDouble();

According to a String, int and a double varies same way for the rest. Don't forget the import statement at the top of your code.

Also see the blog post "Scanner class and getting User Inputs".

查看更多
泪湿衣
7楼-- · 2018-12-31 03:42

You can get user input like this using a BufferedReader:

    InputStreamReader inp = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(inp);
    // you will need to import these things.

This is how you apply them

    String name = br.readline(); 

So when the user types in his name into the console, "String name" will store that information.

If it is a number you want to store, the code will look like this:

    int x = Integer.parseInt(br.readLine());

Hop this helps!

查看更多
登录 后发表回答