
标准输入(stdin)在Java中可以用System.in来表示。 System.in 是InputStream 类的一个实例。这意味着它的所有方法都适用于字节,而不是字符串。要从键盘读取任何数据,我们可以使用 Reader 类或 Scanner 类。
import java.io.*;
public class ReadDataFromInput {
public static void main (String[] args) {
int firstNum, secondNum, result;
<strong> BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
</strong> try {
System.out.println("Enter a first number:");
firstNum = Integer.parseInt(br.readLine());
System.out.println("Enter a second number:");
secondNum = Integer.parseInt(br.readLine());
result = firstNum * secondNum;
System.out.println("The Result is: " + result);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}Enter a first number: 15 Enter a second number: 20 The Result is: 300
import java.util.*;
public class ReadDataFromScanner {
public static void main (String[] args) {
int firstNum, secondNum, result;
<strong> Scanner scanner = new Scanner(System.in);
</strong> System.out.println("Enter a first number:");
firstNum = Integer.parseInt(scanner.nextLine());
System.out.println("Enter a second number:");
secondNum = Integer.parseInt(scanner.nextLine());
result = firstNum * secondNum;
System.out.println("The Result is: " + result);
}
}Enter a first number: 20 Enter a second number: 25 The Result is: 500
以上就是在Java中,我们如何从标准输入读取数据?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号