《Java语言程序设计(基础篇)》(第10版梁勇著)第十二章练习题答案.docxVIP

  • 310
  • 0
  • 约4.2万字
  • 约 46页
  • 2021-06-03 发布于天津
  • 举报

《Java语言程序设计(基础篇)》(第10版梁勇著)第十二章练习题答案.docx

}} } } } } Java 语言程序设计(基础篇) 》 (第 10 版 梁勇 著) 第十二章 练习题答案 12.1 public class Exercise12_01 { public static void main (String[] args) { int num1, num2, result = 0; if (args.length != 3) { System.out.println( please use java Exercise12_01 operand1 operator operand2 ); System.exit(1); } try { num1 = Integer.parseInt(args[0]); } catch (NumberFormatException ex) { System.out.println( Wrong Input: + args[0]); return ; } try { num2 = Integer.parseInt(args[2]); } catch (Exception ex) { System.out.println( Wrong Input: + args[2]); return ; } switch (args[1].charAt(0)) { case + : result = num1 + num2; break ; case - : result = num1 - num2;; break ; case . : result = num1 * num2;; break ; case / : result = num1 / num2;; } System.out.println(args[0]+ args[1] + + System.out.println(args[0] + args[1] + + args[2] = + result); 12.1 附加 import java.util.Scanner; import java.util.ArrayList; import java.io.*; public class Exercise12_01Extra { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); );System.out.print( Enter a file name for baby name ranking: ); File file = new File(input.nextLine()); if (!file.exists()) { System.out.println( File + file + does not exist ); System.exit(1); } // Open the file input = new Scanner(file); new ArrayListString();new ArrayListString();ArrayListString boyNames new ArrayListString(); new ArrayListString(); ArrayListString girlNames = while (input.hasNext()) { input.nextInt(); // Skip an integer for ranking boyNames.add(input.next()); input.nextInt(); // Skip an integer for number of boys girlNames.add(input.next()); input.nextInt(); // Skip an integer for number of girls } boyNames.retainAll(girlNames); // If you dont know how to use the retainAll method, write your own code to obtain the common elements in two array lists. System.out.println(boyNames.size() + names are used for both boys and girls ); System.out.println( They are ); for (String name: boyNames) { System.out.print(name + ); }

文档评论(0)

1亿VIP精品文档

相关文档