sobota, 29 października 2011

Java – konsola

Wczytywanie pojedynczych znaków z konsoli:
package kkk;

import java.io.*;
class BRReadLines{
    public static void main(String args[]) throws IOException {
 char c;
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Wpisz znaki, 'q' to quit.");
 do {
     c = (char) br.read();
     System.out.println(c);
 } while (c != 'q');
    }
}
Wczytywanie łańcuchów znaków z konsoli:
package kkk;

import java.io.*;
class Main {
    public static void main(String args[]) throws IOException {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String str;
 System.out.println("Enter lines of text.");
 System.out.println("Enter 'stop' to quit.");
 do {
     str = br.readLine();
     System.out.println(str);
 } while (!str.equals("stop"));
    }
}
Znalezione na http://math.hosted.pl/math_2/programowanie_obiektowe/wyklad10.pdf
import java.util.Scanner;

public class Czytanie {
    public static void main(String args[]) {
 Scanner s = new Scanner(System.in);
 System.out.print("Imię i nazwisko: ");
 String i = s.next();
 String n = s.next(); // wczytywanie słów
 System.out.println(i+" "+n);
 System.out.println("Pseudo:");
 String r = s.next(); 
 System.out.println(r);
 System.out.print("Wiek: ");
 int w = s.nextInt(); // wczytwanie liczby całkowitej
 System.out.print("Zarobki: ");
 double z = s.nextDouble(); // rzeczywistej
 System.out.print("Opis: ");
 String o = s.nextLine(); // wiersza
    }
}
Znalezione na: http://student.pwsz.elblag.pl/~jopi/?co=obiektowe&id=021&p=1

Brak komentarzy:

Prześlij komentarz