Когда дело доходит до чтения и печати различных типов данных из стандартного ввода (stdin), разные языки программирования предлагают разные методы и синтаксис. В этой статье мы рассмотрим несколько языков программирования и продемонстрируем, как читать целое число, двойное число и строку из стандартного ввода, а затем печатать значения в соответствии с инструкциями. Давайте погрузимся!
-
Python:
# Reading an integer, a double, and a string from stdin in Python integer_input = int(input("Enter an integer: ")) double_input = float(input("Enter a double: ")) string_input = input("Enter a string: ") # Printing the values print("Integer:", integer_input) print("Double:", double_input) print("String:", string_input) -
Java:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Reading an integer, a double, and a string from stdin in Java System.out.print("Enter an integer: "); int integerInput = scanner.nextInt(); System.out.print("Enter a double: "); double doubleInput = scanner.nextDouble(); System.out.print("Enter a string: "); scanner.nextLine(); // Clear the newline character from the buffer String stringInput = scanner.nextLine(); // Printing the values System.out.println("Integer: " + integerInput); System.out.println("Double: " + doubleInput); System.out.println("String: " + stringInput); } } -
C++:
#include <iostream> #include <string> int main() { int integerInput; double doubleInput; std::string stringInput; // Reading an integer, a double, and a string from stdin in C++ std::cout << "Enter an integer: "; std::cin >> integerInput; std::cout << "Enter a double: "; std::cin >> doubleInput; std::cout << "Enter a string: "; std::cin.ignore(); // Clear the newline character from the buffer std::getline(std::cin, stringInput); // Printing the values std::cout << "Integer: " << integerInput << std::endl; std::cout << "Double: " << doubleInput << std::endl; std::cout << "String: " << stringInput << std::endl; return 0; } -
JavaScript (Node.js):
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Reading an integer, a double, and a string from stdin in JavaScript rl.question("Enter an integer: ", (integerInput) => { rl.question("Enter a double: ", (doubleInput) => { rl.question("Enter a string: ", (stringInput) => { // Printing the values console.log("Integer:", parseInt(integerInput)); console.log("Double:", parseFloat(doubleInput)); console.log("String:", stringInput); rl.close(); }); }); });
В этой статье мы рассмотрели различные методы чтения целого числа, двойного значения и строки из стандартного ввода и печати значений на различных языках программирования. Мы рассмотрели Python, Java, C++ и JavaScript. Следуя предоставленным примерам кода, вы сможете легко реализовать желаемую функциональность на предпочитаемом вами языке программирования. Не забудьте адаптировать код по мере необходимости для вашего конкретного случая использования. Приятного кодирования!