1 / 16

CSCI S-1 Section 5

CSCI S-1 Section 5. Deadlines for Problem Set 3. Part A Friday, July 10, 17:00 EST Parts B Tuesday, July 14, 17:00 EST Getting the code examples from lecture cp ~libs1/pub/ src /lectures/ leitner /July6*.java . The Scanner Class. A java library to take user input

shirin
Télécharger la présentation

CSCI S-1 Section 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI S-1Section 5

  2. Deadlines for Problem Set 3 • Part A • Friday, July 10, 17:00 EST • Parts B • Tuesday, July 14, 17:00 EST • Getting the code examples from lecture • cp ~libs1/pub/src/lectures/leitner/July6*.java .

  3. The Scanner Class • A java library to take user input • Not included automatically • Unlike lang.System, for System.out.print() • Need to tell the compiler where the library is • So IMPORT the library at the very top of the program file • Import java.util.scanner; // includes definition for scanner only • Import java.util.* // includes all definitions in util library

  4. Declaring Scanner Objects • Scanner scan = new Scanner(System.in); • “new” creates an instance (or “object”) of the java class Scanner • Now we can use predefined methods from the scanner class • Scan.nextInt(); // get an int • Scan.nextDouble(); // get a double • Scan.next() or scan.nextLine(); // get a string • Scan.nextShort(); // get a short int • Scan.nextByte(); // get a byte

  5. Example 1 - Nint // take an int from user and print it back out Class Nint { public static void main (String[] args) { } }

  6. Example 1 - Nint // take an int from user and print it back out import java.util.*; Class Nint { public static void main (String[] args) { System.out.print(“Please enter a number: ”) Scanner scan= new Scanner(System.in); int n= scan.nextInt(); System.out.printl(“You have entered the number ” + N + “.”); } }

  7. Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit Class Celsius { public static void main (String[] args) { } }

  8. Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit import java.util.*; Class Celsius { public static void main (String[] args) { System.out.print(“Please enter the temperature in Celsius outside:“ ) Scanner scan= new Scanner(System.in); double n= scan.nextDouble(); System.out.printl(“You current temperature in Fahrenheit is “ + (n*(9.0/5) + 32) + “.”); } }

  9. Example 3 - Born // Take a String and print it back out class Born { public static void main(String[] args) { } }

  10. Example 3 - Born // Take a String and print it back out import java.util.*; class Born { public static void main(String[] args) { System.out.print("Where were you born? "); Scanner scan = new Scanner(System.in); String s = scan.nextLine(); System.out.println("You were born in " + s + "! Wow, so was I!"); } }

  11. Example 4 - Bad // Don’t try this at home class Bad { public static void main(String[] args) { System.out.print("I need some vital input here: "); Scanner scan = new Scanner(System.in); String n = scan.nextLine(); System.out.println("Wow, " + n + " was some vital input!"); } }

  12. Conditional Statements • Use if-else conditions to create ‘branches’. Conditions evaluate to true or false. int x=3; if (x < 5) System.out.println("This is True"); if (x >= 5) System.out.println("This is False"); // alternatively if (x < 5) System.out.println("This is True"); else System.out.println("This is False");

  13. Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. print * for even digits class Condition1 { public static void main(String[] args) { } }

  14. Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. Print * for even digits import java.util.*; class Condition1 { public static void main(String[] args) { System.out.print("Enter a number. I am going to count odds: "); Scanner scan = new Scanner(System.in); int stop = scan.nextInt(); for (inti = 1; i <= stop; i++) if (i % 2 == 1) System.out.println(i); else System.out.println("*"); } }

  15. Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) class Smallest { public static void main(String[] args) { } }

  16. Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) import java.util.*; class Smallest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int smallest = 0; for (inti = 1; i <= 10; i++) { System.out.println("Enter a number greater than 0: "); int n = scan.nextInt(); if (n < smallest || smallest == 0) smallest = n; } System.out.println("The smallest number was " + smallest + "."); } }

More Related