Najlepsza odpowiedź
  
W java możesz użyć klasy Scanner do odczytu wejścia i System.out do wydrukowania wyjścia.
 Scanner scanner = new Scanner(System.in); 
 int n = scanner.nextInt(); // read an integer from input stream. 
 System.out.println(n) // print n 
Jednak czasami pojawia się komunikat Przekroczony limit czasu (TLE) ze względu na wydajność skanera. Zamiast tego możesz użyć BufferedReader.
 class InputReader { 
  public BufferedReader reader; 
  public StringTokenizer tokenizer; 
  public InputReader(InputStream stream) { 
  reader = new BufferedReader(new InputStreamReader(stream), 32768); 
  tokenizer = null; 
  tokenizer = null; 
  } 
  public String next() { 
  while (tokenizer == null || !tokenizer.hasMoreTokens()) { 
  try { 
  tokenizer = new StringTokenizer(reader.readLine()); 
  } catch (IOException e) { 
  throw new RuntimeException(e); 
  } 
  } 
  return tokenizer.nextToken(); 
  } 
  public int nextInt() { 
  return Integer.parseInt(next()); 
  } 
   
  public long nextLong() { 
  return Long.parseLong(next()); 
  } 
 } 
Odpowiedź
Spróbuję wyjaśnić za pomocą Zły profesor z HackerRank.
Poniższy kod czyta z System.in i drukuje wyjście, reszta jest oczywista:
 import java.io.*; 
 import java.util.*; 
 public class Solution { 
   
  public static void main(String[] args) { 
  Scanner in = new Scanner(System.in); 
  int t = in.nextInt(); 
  for (int i = 0; i < t; i++) { 
  int nstudent = in.nextInt(); 
  int threshold = in.nextInt(); 
  int[] arrival = new int[nstudent]; 
  for (int j = 0; j < nstudent; j++) { 
  arrival[j] = in.nextInt(); 
  } 
  algo(arrival, threshold); 
  } 
  } 
   
  public static void algo(int[] arrival, int threshold){ 
  //your implementation 
  } 
 }