1、Java语言程序设计基础篇第10版 梁勇 著第十二章练习题答案Java语言程序设计(基础篇)(第10版 梁勇 著)第十二章 练习题答案12.1public 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.
2、parseInt(args0); catch (NumberFormatException ex) System.out.println(Wrong Input: + args0); return; try num2 = Integer.parseInt(args2); catch (Exception ex) System.out.println(Wrong Input: + args2); return; switch (args1.charAt(0) case +: result = num1 + num2; break; case -: result = num1 - num2; br
3、eak; case .: result = num1 * num2; break; case /: result = num1 / num2; System.out.println(args0 + + args1 + + args2 + = + 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 Scann
4、er 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); ArrayList boyNames = new ArrayList();
5、 ArrayList girlNames = new ArrayList(); 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(girlNam
6、es); / 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 + ); 12.2impo
7、rt java.util.Scanner;public class Exercise12_02 public static void main(String args) Scanner input = new Scanner(System.in); boolean done = false; int number1 = 0; int number2 = 0; / Enter two integers System.out.print(Enter two integers: ); while (!done) try number1 = input.nextInt(); number2 = inp
8、ut.nextInt(); done = true; catch (Exception ex) ex.printStackTrace(); System.out.print(Incorrect input and re-enter two integers: ); input.nextLine(); / Discard input System.out.println(Sum is + (number1 + number2); 12.2附加import java.util.*;import java.io.*;public class Exercise12_02Extra private st
9、atic ArrayList names = new ArrayList(); private static void readNames() try for (int i = 0; i 10; i+) String filename; if (i = 9) filename = Babynamesranking2010.txt; else filename = Babynamesranking200 + (i + 1) + .txt; Scanner input = new Scanner(new File(filename); while (input.hasNext() input.ne
10、xtInt(); / Skip the ranking String name = input.next(); if (!names.contains(name) names.add(name); input.nextInt(); / Skip the number of boys name name = input.next(); if (!names.contains(name) names.add(name); input.nextInt(); / Skip the number of girls name catch (Exception ex) ex.printStackTrace(
11、); /* Main method */ public static void main(String args) readNames(); Collections.sort(names); / If you dont know how to use this method, write your own method to sort the elements in an array list writeNames(); private static void writeNames() try PrintWriter output = new PrintWriter(AllBabyNamesS
12、ortedDuplicatesNotAllowed.txt); for (int i = 1; i = names.size(); i+) if (i % 10 = 0) output.println(names.get(i - 1); else output.print(names.get(i - 1) + ); output.close(); System.out.println(Done); catch (Exception ex) ex.printStackTrace(); 12.3 import java.util.*;public class Exercise12_03 publi
13、c static void main(String args) int data = new int100; / Initialize array for (int i = 0; i 100; i+) datai = (int)(Math.random() * 10000); Scanner input = new Scanner(System.in); System.out.print(Enter an index: ); int index = input.nextInt(); try System.out.println(The element is + dataindex); catc
14、h (Exception ex) System.out.println(Index out of bound); 12.3附加import java.util.*;import java.io.*;public class Exercise12_03Extra private static String names = new String1000 * 10 * 2; private static void readNames() try int j = 0; for (int i = 0; i 10; i+) String filename; if (i = 9) filename = Babynamesranking2010.txt; e
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1