Input standard (stdin) boleh diwakili oleh System.in di Jawa. System.in ialah contoh kelas InputStream. Ini bermakna semua kaedahnya berfungsi pada bait, bukan rentetan. Untuk membaca sebarang data daripada papan kekunci kita boleh menggunakan kelas Kelas pembaca atau Pengimbas .
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
Atas ialah kandungan terperinci Di Jawa, bagaimana kita membaca data daripada input standard?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!