Download presentation
Presentation is loading. Please wait.
Published byBritney Cropsey Modified over 10 years ago
1
Chapter 3A Review boolean fun = true; if(fun) System.out.print(“yeah!”);
2
Fix the errors! boolean fun = true; int x = 5, y = x % 3; if(x = 5 && y = 2); System.out.print(“right!”); else system.out.print(“wrong!”);
3
A corrected version: boolean fun = true; int x = 5, y = x % 3; if(x == 5 && y == 2) System.out.print(“right!”); else System.out.print(“wrong!”);
4
Fix the errors! Assume the user entered the name “bob” String name = “”; //get name from user if(name = ‘Bob’) System.out.print(“hi!”); else if(name = ‘bob’) System.out.print(name);
5
A corrected version: String name = “”; //get name from user if(name.equals(“Bob”)) System.out.print(“hi!”); else if(name.equals(“bob”)) System.out.print(name);
6
Fix & predict output int x = 6, y = 12; If x > 5 System.out.print(“Hi there”); System.out.println(“buddy!”); if( y % 2 = 0 ) System.out.println(“even!”); System.out.println(“cool!”); Else (y % 2 != 0) System.out.println(“odd!”);
7
A corrected version int x = 6, y = 12; if( x > 5 ) { System.out.print(“Hi there”); System.out.println(“buddy!”); } if( y % 2 == 0 ) { System.out.println(“even!”); System.out.println(“cool!”); } else if(y % 2 != 0) System.out.println(“odd!”); Hi therebuddy! even! cool! Output
8
Fix & predict output Assume the user entered the name “bob” char y = ‘a’; String x = JOptionPane.showInputDialog(“x: “); if (y = “A” and x = ‘bob’) System.out.print(“Hi bob! ”); System.out.print(“You get an A!”);
9
A corrected version: Assume the user entered the name “bob” char y = ‘a’; String x = JOptionPane.showInputDialog(“x: “); if (y == ‘A’ && x.equals(“bob”)) System.out.print(“Hi bob! ”); System.out.print(“You get an A!”); You get an A! Output
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.