SlideServe
Menu
Préférences
© SlideServe · v2026

Simple Input

yan avatar
14 diapositives · 397 Views · Oct 14, 2014

Simple Input. Interactive Input - The Class Scanner Command-Line Arguments. Simple Input. Sometimes data is needed and obtained from the user at run time. Simple keyboard input requires: import java.util.Scanner; or import java.util.*; at the beginning of the file. Simple Input, cont.

Simple Input - slide 1
01 / 14Simple Input
1 / 17

Télécharger la présentation

Simple Input
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Simple Input • Interactive Input - The Class Scanner • Command-Line Arguments

  2. Simple Input • Sometimes data is needed and obtained from the user at run time. • Simple keyboard input requires: import java.util.Scanner; or import java.util.*; at the beginning of the file.

  3. Simple Input, cont. • A “Scanner” object must be initialized before inputting data: Scanner keyboard = new Scanner(System.in); • To input data: int eggsPerBasket; : eggsPerBasket = keyboard.nextInt(); which reads one int value from the keyboard and assigns it to the variable eggsPerBasket.

  4. Simple Input, cont. import java.util.Scanner; public class Cows { publicstaticvoid main (String[] args) { int barns, cowsPer, totalCows; Scanner kb = new Scanner(System.in); System.out.print(“Enter the number of barns: ”); barns = kb.nextInt(); System.out.print(“Enter the number of cows per barn: ”); cowsPer = kb.nextInt(); totalCows = barns * cowsPer; System.out.println(“Total number of cows is: “ + totalCows); System.out.println(“Suppose two cows per barn escape!”); cowsPer = cowsPer – 2; totalCows = barns * cowsPer; System.out.println(“Total number of cows is now: “ + totalCows); } } > javac Cows.java > java Cows Enter the number of barns: 10 Enter the number of cows per barn: 6 Total number of cows is: 60 Suppose two cows per barn escape! Total number of cows is now: 40

  5. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument *Notice Scanner is not imported! { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  6. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  7. Command-Line Arguments • Frequently input is provided to a program at the command-line. public class UseArgument { public static void main(String[] args) { System.out.print(“Hi, ”); System.out.print(args[0]); System.out.println(“. How are you?”); } } > javac UseArgument.java > java UseArgument Alice Hi, Alice. How are you? > java UseArgument Bob Hi, Bob. How are you?

  8. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow

  9. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow

  10. Command-Line Arguments • Frequently multiple values are provided at the command-line. public class Use3Arguments { public static void main(String[] args) { System.out.print(“The first word is ”); System.out.print(args[0]); System.out.print(“, the second is ”); System.out.print(args[1]); System.out.print(“, and the third is ”); System.out.println(args[2]); } } > javac Use3Arguments.java > java Use3Arguments dog cat cow The first word is dog, the second is cat, and the third is cow

  11. Command-Line Arguments • What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum = args[0] + args[1]; int prod = args[0] * args[1]; int quot = args[0] / args[1]; int rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } Hoping to see this > javac IntOps.java > java IntOps 1234 99 Sum = 1333 Prod = 122166 Quotient = 12 Remainder = 46

  12. Command-Line Arguments • What if you provide integers on the command-line? public class IntOps { public static void main(String[] args) { int sum = args[0] + args[1]; int prod = args[0] * args[1]; int quot = args[0] / args[1]; int rem = args[0] % args[1]; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java *** ERROR ***

  13. Command-Line Arguments • For the same reason, this does NOT work: public class IntOps { public static void main(String[] args) { int a = args[0]; int b = args[1]; int sum = a + b; int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java *** ERROR ***

  14. Command-Line Arguments • So, it looks ugly, but this is what we need to do: public class IntOps { public static void main(String[] args) { int a = Integer.parseInt(args[0]); // Notice the variable declaration int b = Integer.parseInt(args[1]); // Notice the Integer.parseInt int sum = a + b; // Notice the comments…lol int prod = a * b; int quot = a / b; int rem = a % b; System.out.println(“Sum = " + sum); System.out.println(“Product = " + prod); System.out.println(“Quotient = " + quot); System.out.println(“Remainder = " + rem); } } > javac IntOps.java > java IntOps 1234 99 Sum = 1333 Prod = 122166 Quotient = 12 Remainder = 46