-->

2007年10月11日 星期四

Chapter2.CONSOLE INPUT AND OUTPUT

Savitch Absolute Java Third Edition.
By Addison Wesley.
Source Code of any chapter of book.
Chapter2.CONSOLE INPUT AND OUTPUT

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

public class CurrencyFormatDemo
{
public static void main(String[] args)
{
System.out.println("Without formatting:");

System.out.println(19.8);
System.out.println(19.81111);
System.out.println(19.89999);
System.out.println(19);
System.out.println( );

System.out.println("Default location:");
NumberFormat moneyFormatter =
NumberFormat.getCurrencyInstance( );

System.out.println(moneyFormatter.format(19.8));
System.out.println(moneyFormatter.format(19.81111));
System.out.println(moneyFormatter.format(19.89999));
System.out.println(moneyFormatter.format(19));
System.out.println( );

System.out.println("US as location:");
NumberFormat moneyFormatter2 =
NumberFormat.getCurrencyInstance(Locale.US);

System.out.println(moneyFormatter2.format(19.8));
System.out.println(moneyFormatter2.format(19.81111));
System.out.println(moneyFormatter2.format(19.89999));
System.out.println(moneyFormatter2.format(19));
}

}

DecimalFormatDemo.java
import java.text.DecimalFormat;

public class DecimalFormatDemo
{
public static void main(String[] args)
{
DecimalFormat pattern00dot000 = new DecimalFormat("00.000");
DecimalFormat pattern0dot00 = new DecimalFormat("0.00");

double d = 12.3456789;
System.out.println("Pattern 00.000");
System.out.println(pattern00dot000.format(d));
System.out.println("Pattern 0.00");
System.out.println(pattern0dot00.format(d));

double money = 19.8;
System.out.println("Pattern 0.00");
System.out.println("$" + pattern0dot00.format(money));

DecimalFormat percent = new DecimalFormat("0.00%");

System.out.println("Pattern 0.00%");
System.out.println(percent.format(0.308));

DecimalFormat eNotation1 =
new DecimalFormat("#0.###E0");//1 or 2 digits before point
DecimalFormat eNotation2 =
new DecimalFormat("00.###E0");//2 digits before point

System.out.println("Pattern #0.###E0");
System.out.println(eNotation1.format(123.456));
System.out.println("Pattern 00.###E0");
System.out.println(eNotation2.format(123.456));

double smallNumber = 0.0000123456;
System.out.println("Pattern #0.###E0");
System.out.println(eNotation1.format(smallNumber));
System.out.println("Pattern 00.###E0");
System.out.println(eNotation2.format(smallNumber));
}
}

DelimiterDemo.java
import java.util.Scanner;

public class DelimiterDemo
{
public static void main(String[] args)
{
Scanner keyboard1 = new Scanner(System.in);
Scanner keyboard2 = new Scanner(System.in);
keyboard2.useDelimiter("##");
//Delimiter for keyboard1 is whitespace.
//Delimiter for keyboard2 is ##.


String word1, word2;
System.out.println("Enter a line of text:");
word1 = keyboard1.next( );
word2 = keyboard1.next( );
System.out.println("For keyboard1 the two words read are:");
System.out.println(word1);
System.out.println(word2);
String junk = keyboard1.nextLine( ); //To get rid of rest of line.

System.out.println("Reenter the same line of text:");
word1 = keyboard2.next( );
word2 = keyboard2.next( );
System.out.println("For keyboard2 the two words read are:");
System.out.println(word1);
System.out.println(word2);
}
}

PrintfDemo.java
public class PrintfDemo
{
public static void main(String[] args)
{
String aString = "abc";

System.out.println("String output:");
System.out.println("START1234567890");
System.out.printf("START%sEND %n", aString);
System.out.printf("START%4sEND %n", aString);
System.out.printf("START%2sEND %n", aString);
System.out.println( );

char oneChracter = 'Z';

System.out.println("Character output:");
System.out.println("START1234567890");
System.out.printf("START%cEND %n", oneChracter);
System.out.printf("START%4cEND %n", oneChracter);
System.out.println( );

double d = 12345.123456789;

System.out.println("Floating-point output:");
System.out.println("START1234567890");
System.out.printf("START%fEND %n", d);
System.out.printf("START%.4fEND %n", d);
System.out.printf("START%.2fEND %n", d);
System.out.printf("START%12.4fEND %n", d);
System.out.printf("START%eEND %n", d);
System.out.printf("START%12.5eEND %n", d);
}
}

ScannerDemo.java
import java.util.Scanner;

public class ScannerDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the number of pods followed by");
System.out.println("the number of peas in a pod:");
int numberOfPods = keyboard.nextInt( );
int peasPerPod = keyboard.nextInt( );

int totalNumberOfPeas = numberOfPods*peasPerPod;

System.out.print(numberOfPods + " pods and ");
System.out.println(peasPerPod + " peas per pod.");
System.out.println("The total number of peas = "
+ totalNumberOfPeas);
}
}

ScannerDemo2.java
import java.util.Scanner;

public class ScannerDemo2
{
public static void main(String[] args)
{
int n1, n2;
Scanner scannerObject = new Scanner(System.in);

System.out.println("Enter two whole numbers");
System.out.println("seperated by one or more spaces:");

n1 = scannerObject.nextInt( );
n2 = scannerObject.nextInt( );
System.out.println("You entered " + n1 + " and " + n2);

System.out.println("Next enter two numbers.");
System.out.println("Decimal points are allowed.");

double d1, d2;
d1 = scannerObject.nextDouble( );
d2 = scannerObject.nextDouble( );
System.out.println("You entered " + d1 + " and " + d2);

System.out.println("Next enter two words:");

String word1 = scannerObject.next( );
String word2 = scannerObject.next( );
System.out.println("You entered \"" +
word1 + "\" and \"" + word2 + "\"");

String junk = scannerObject.nextLine( ); //To get rid of '\n'

System.out.println("Next enter a line of text:");
String line = scannerObject.nextLine( );
System.out.println("You entered: \"" + line + "\"");
}
}

SelfService.java
import java.util.Scanner;

public class SelfService
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter number of items purchased");
System.out.println("followed by the cost of one items.");
System.out.println("Do not use a dollar sign.");

int count = keyboard.nextInt( );
double price = keyboard.nextDouble( );
double total = count*price;

System.out.printf("%d items at $%.2f each.%n", count, price);
System.out.printf("Total amount due $%.2f.%n", total);

System.out.println("Please take your merchandise.");
System.out.printf("Place $%.2f in an envelope %n", total);
System.out.println("and slide it under the office door.");
System.out.println("Thank you for using the self-service line.");
}
}

0 COMMENT: