Monday, December 19, 2022

Read from and write to console in java, python 3, c++ and javascript

Here is how you can read user input and print to the console in each of the languages you mentioned:

Java:

import java.util.Scanner; 
public class Main
    public static void main(String[] args)
        Scanner scanner = new Scanner(System.in); 
        System.out.print("Enter a string: "); 
        String input = scanner.nextLine(); 
        System.out.println("You entered: " + input); 
   
}

Python 3:

input_string = input("Enter a string: "
print("You entered: " + input_string)

C++:

#include <iostream> 
#include <string> 
int main()
    std::cout << "Enter a string: "
    std::string input; 
    std::getline(std::cin, input); 
    std::cout << "You entered: " << input << std::endl
    return 0
}

JavaScript:

const readline = require('readline'); 
const rl = readline.createInterface(
    input: process.stdin
    output: process.stdout 
}); 
rl.question('Enter a string: ', (answer) =>
    console.log(`You entered: ${answer}`); 
    rl.close(); 
});

I hope this helps! Let me know if you have any questions. 

No comments:

Post a Comment

meta.ai impression

Meta.ai is released by meta yesterday, it is super fast you can generate image while typing! You can ask meta.ai to draw a cat with curvy fu...