Sie sind auf Seite 1von 9

Example 1.

1
if (radius >= 0) { area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + "is " + area); } The flowchart as shown
Boolean Expression true Statement(s) false false

(radius >= 0)

true area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area);

(A)

(B)

Example 1.4
if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F';

Equivalent

Dangling-else Problem

The Java compiler always associate an else with the immediately preceding if unless told to do otherwise by the placement of braces ({}). This behavior can led to what is referred to as the dangling-else problem. For example:

int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B");


(A)

Equivalent

int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B");


(B )

Example 1.5

Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B"); This statement prints B.

Example 1.8
System.out.println(studentGrade >= 60 ? "Passed" : "Failed" ); also, if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; and if (num % 2 == 0) System.out.println(num + is even); else System.out.println(num + is odd); is equivalent to System.out.println( (num % 2 == 0)? num + is even : num + is odd);

Example 1.10:
Using while Loops Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. The program import javax.swing.JOptionPane; public class TestWhile { /** Main method */ public static void main(String[] args) { // Read an initial data String dataString = JOptionPane.showInputDialog(null, "Enter an int value, \nthe program exits if the input is 0", "Example 1.2 Input", JOptionPane.QUESTION_MESSAGE); int data = Integer.parseInt(dataString); // Keep reading data until the input is 0 int sum = 0; while (data != 0) { sum += data; // Read the next data dataString = JOptionPane.showInputDialog(null, "Enter an int value, \nthe program exits if the input is 0", "Example 1.2 Input", JOptionPane.QUESTION_MESSAGE); data = Integer.parseInt(dataString); } JOptionPane.showMessageDialog(null, "The sum is " + sum, "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); } }

The program execution:

Example 1.11
Rewrite example 1.10 as follows: //TestDo.java: Test the do-while loop import javax.swing.JOptionPane; public class TestDo { /** Main method */ public static void main(String[] args) { int data; int sum = 0; // Keep reading data until the input is 0 do { // Read the next data String dataString = JOptionPane.showInputDialog(null,"Enter an int value, \nthe program exits if the input is 0","TestDo", JOptionPane.QUESTION_MESSAGE); data = Integer.parseInt(dataString); sum += data; } while (data != 0); JOptionPane.showMessageDialog(null, "The sum is " + sum,"TestDo", JOptioPane.INFORMATION_MESSAGE); System.exit(0); } }

Example 1.13
Using for Loops Write a program that sums a series that starts with 0.01 and ends with 1.0. The numbers in the series will increment by 0.01, as follows: 0.01 + 0.02 + 0.03 and so on. The program: import javax.swing.JOptionPane; public class TestSum { /** Main method */ public static void main(String[] args) { // Initialize sum float sum = 0; // Add 0.01, 0.02, ..., 0.99, 1 to sum for (float i = 0.01f; i <= 1.0f; i = i + 0.01f) sum += i; // Display result JOptionPane.showMessageDialog(null, "The sum is " + sum, "Example 1.3 Output", JOptionPane.INFORMATION_MESSAGE); } } The output:

Example 1.16:
TestBreak.java public class TestBreak { /** Main method */ public static void main(String[] args) { int sum = 0; int number = 0; while (number < 20) { number++; sum += number; if (sum >= 100) break; } System.out.println("The number is " + number); System.out.println("The sum is " + sum);

} }

Example 1.17:
TestContinue.java public class TestContinue { /** Main method */ public static void main(String[] args) { int sum = 0; int number = 0; while (number < 20) { number++; if (number == 10 || number == 11) continue; sum += number; }

} }

System.out.println("The sum is " + sum);

Example 1.18
Finding the Greatest Common Divisor Problem: Write a program that prompts the user to enter two positive integers and finds their greatest common divisor. Solution: Suppose you enter two integers 4 and 2, their greatest common divisor is 2. Suppose you enter two integers 16 and 24, their greatest common divisor is 8. So, how do you find the greatest common divisor? Let the two input integers be n1 and n2. You know number 1 is a common divisor, but it may not be the greatest commons divisor. So you can check whether k (for k = 2, 3, 4, and so on) is a common divisor for n1 and n2, until k is greater than n1 or n2. import javax.swing.JOptionPane; public class GreatestCommonDivisor { /** Main method */ public static void main(String[] args) { // Prompt the user to enter two integers String s1 = JOptionPane.showInputDialog(null, "Enter an integer", "Example 1.7 Input", JOptionPane.QUESTION_MESSAGE); int n1 = Integer.parseInt(s1); String s2 = JOptionPane.showInputDialog(null, "Enter an integer", "Example 1.7 Input", JOptionPane.QUESTION_MESSAGE); int n2 = Integer.parseInt(s2); int gcd = 1; int k = 1; while (k <= n1 && k <= n2) { if (n1 % k == 0 && n2 % k == 0) gcd = k; k++; } String output = "The greatest common divisor for " + n1 + " and " + n2 + " is " + gcd; JOptionPane.showMessageDialog(null, output, "Example 1.7 Output", JOptionPane.INFORMATION_MESSAGE); } }

Example Methods

public class testmax { public static void main(String[] args) { int i=5; int j=2 int k= max(i,j);

System.out.println("The maximum between " + i + "and" +j+ "is" +k); } Public static int max(int num1 , int num2){ Int result; if(num1 > num2) result = num1; else result = num2; return result; } }

Das könnte Ihnen auch gefallen