Savitch Absolute Java Third Edition.
By Addison Wesley.
Source Code of any chapter of book.
Chapter3.FLOW OF CONTROL
Averager.javaimport java.util.Scanner;
public class Averager
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a list of nonnegative scores.");
System.out.println("Mark the end with a negative number.");
System.out.println("I will compute their average.");
double next, sum = 0;
int count = 0;
next = keyboard.nextDouble( );
while(next >= 0)
{
sum = sum + next;
count++;
next = keyboard.nextDouble( );
}
if (count == 0)
System.out.println("No scores entered.");
else
{
double average = sum/count;
System.out.println(count + " scores read.");
System.out.println("The average is " + average);
}
}
}
IncomeTax.javaimport java.util.Scanner;
public class IncomeTax
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double netIncome, tax, fivePercentTax, tenPercentTax;
System.out.println("Enter net income.\n"
+ "Do not include a dollar sign or any commas.");
netIncome = keyboard.nextDouble( );
if (netIncome <= 15000)
tax = 0;
else if ((netIncome > 15000) && (netIncome <= 30000))
//tax = 5% of amount over $15,000
tax = (0.05*(netIncome - 15000));
else //netIncome > $30,000
{
//fivePercentTax = 5% of income from $15,000 to $30,000.
fivePercentTax = 0.05*15000;
//tenPercentTax = 10% of income over $30,000.
tenPercentTax = 0.10*(netIncome - 30000);
tax = (fivePercentTax + tenPercentTax);
}
System.out.printf("Tax due = $%.2f", tax);
}
}
StringComparisonDemo.javapublic class StringComparisonDemo
{
public static void main(String[] args)
{
String s1 = "Java isn't just for breakfast.";
String s2 = "JAVA isn't just for breakfast.";
if (s1.equals(s2))
System.out.println("The two lines are equal.");
else
System.out.println("The two lines are not equal.");
if (s2.equals(s1))
System.out.println("The two lines are equal.");
else
System.out.println("The two lines are not equal.");
if (s1.equalsIgnoreCase(s2))
System.out.println("But the lines are equal, ignoring case.");
else
System.out.println("Lines are not equal, even ignoring case.");
String s3 = "A cup of java is a joy forever.";
if (s3.compareToIgnoreCase(s1) < 0)
{
System.out.println("\"" + s3 + "\"");
System.out.println("precedes");
System.out.println("\"" + s1 + "\"");
System.out.println("in alphabetic ordering");
}
else
System.out.println("s3 does not precede s1.");
}
}
SwitchDemo.javaimport java.util.Scanner;
public class SwitchDemo
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number of ice cream flavors:");
int numberOfFlavors = keyboard.nextInt( );
switch (numberOfFlavors)
{
case 32:
System.out.println("Nice selection.");
break;
case 1:
System.out.println("I bet it's vanilla.");
break;
case 2:
case 3:
case 4:
System.out.println(numberOfFlavors + " flavors");
System.out.println("is acceptable.");
break;
default:
System.out.println("I didn't plan for");
System.out.println(numberOfFlavors + " flavors.");
break;
}
}
}
WhileDemo.javapublic class WhileDemo
{
public static void main(String[] args)
{
int countDown;
System.out.println("First while loop:");
countDown = 3;
while (countDown > 0)
{
System.out.println("Hello");
countDown = countDown - 1;
}
System.out.println("Second while loop:");
countDown = 0;
while (countDown > 0)
{
System.out.println("Hello");
countDown = countDown - 1;
}
System.out.println("First do-while loop:");
countDown = 3;
do
{
System.out.println("Hello");
countDown = countDown - 1;
}while (countDown > 0);
System.out.println("Second do-while loop:");
countDown = 0;
do
{
System.out.println("Hello");
countDown = countDown - 1;
}while (countDown > 0);
}
}
導覽連結: 首頁 -> 標題: Chapter3.FLOW OF CONTROL
2007年10月11日 星期四
Chapter3.FLOW OF CONTROL
ARTICLE LABELS: Source Code
訂閱:
張貼留言 (Atom)
0 COMMENT:
張貼留言