(3) - screen output & console input

2405 단어 JavaJava

Screen output

println 줄바꿈
print 다붙여씀

printf : format str + output str
%f - float %d - integer %s - string %e

System.out.print("$");
System.out.printf("%10.4f", 23.788888); 
System.out.println("each");
System.out.printf("Start%-8.2fEnd%n", 12.123);
System.out("$%6.2f_%d for each %s.%n", 19.8, 3, "apple"); 
System.out.printf("%2s%n","zzzzz");
System.out.printf("%7c%n","z");
System.out.printf("%12.5e", 123456.1234567);

//$___23.7889each
Start12.12___End
$_19.80_3 for each apple.
zzzzz 
______z
__1.2346e+05

M.Nf => M자리차지 N소수점아래
string은 생략 안함
e 디폴트: 1.123456e+0n 소수점6개

System.out
Class system
an object used for sending output to the screen

println
method that System.out object performs

Money Formats
import java.text.NumberFormat
getCurrencyInstance(나라, 디폴트는 현재나라);

import java.text.NumberFormat; 
import java.util.Locale; 

NumberFormat moneyFormater = NumberFormat.getCurrencyInstance();
System.out.println(moneyFormater.format(23.5));
NumberFormat dollarFormater = NumberFormat.getCurrencyInstance(Locale.US);
System.out.println(dollarFormater.format(23.5));

Packages

libraries in Java
collections of classes
java.lang 은 자동import됨

import java.text.NumberFormat
impott java.text.* // text 전부 import 

console input

import java.util.Scanner;

Scanner scannerObject = new Scanner(System.in);
Scanner scannerObj2 = new Scanner(System.in);
keyboard2.useDelimiter("##"); //scannerObj2에는 delimiter 부여, space는 delimiter 아님 

int n1; 
n1 = scannerObject.nextInt(); //21
double d1;
d1 = scannerObject.newDouble(); //2.1

String word1 = scannerObject.next(); //apple 
String line = scannerObject.nextLine(); // sentence

int numm = scannerObj.nextInt();
String junk = scannerObj.nextLine();
String s1 = scannerObj.nextLine();
String s2 = scannerObj.nextLine();
String 
String junk2 = scannerObj.nextLine();

String word1 = scannerObj2.next();
String word2 = scannerObj2.next();

//input: aa bb##cc dd##
//word1 : aa bb
//word2 : cc dd

in interactive program,
print prompt recommended before user input
& always echo all input!

좋은 웹페이지 즐겨찾기