《accp6.0java1第九章上机练习(Accp6.0java1 ninth chapter practice on the computer)》由会员分享,可在线阅读,更多相关《accp6.0java1第九章上机练习(Accp6.0java1 ninth chapter practice on the computer)(12页珍藏版)》请在金锄头文库上搜索。
1、accp6.0java1第九章上机练习(Accp6.0java1 ninth chapter practice on the computer)Exercise 1: maintain an orderly list of booksImport java.util.Scanner;/ * * insert books in sequence* /Public class Book Public static void main (String args) String books = new String, Computer, Hibernate, Java, Struts;String n
2、ewBooks = new Stringbooks.length+1; / / the book into the arrayString = book; / / keep a book from the user nameInt index = books.length; / / save new insertion position/ / output into the results beforeSystem.out.print (array before insertion:);For (int i = 0; I books.length; i+) System.out.print (
3、booksi+ );/ / copy in the books array elements to the array of newBooksFor (int i = 0; I books.length; i+) NewBooksi = booksi;The input / book nameScanner input = new Scanner (System.in);System.out.print (n, enter a new book name:);Book = input.nextLine ();Find new location / /For (int i = 0; I 0) I
4、ndex = i;Break;For (int i = newBooks.length-1; I index; i-) NewBooksi = newBooksi-1; the element beginning with the /index subscript moves one position laterNewBooksindex = book; / / new elements in the index position/ / output after insertion of the resultsSystem.out.print (insert array: );For (int
5、 i = 0; I newBooks.length; i+) System.out.print (newBooksi+ );Exercise 2: calculate the average score of a classImport java.util.Scanner;/ * *Calculate the average score of a class* /Public class AvgScore Public static void main (String args) Int score = new int4; / / array performanceFloat sum = 0.
6、0F; / / the sum of scoresFloat average = 0.0F; / / average value/ / loop input student achievementScanner input = new Scanner (System.in);System.out.println (please enter the results of 4 participants);For (int i = 0; I score.length; i+) System.out.print (+ + (i+1) + students achievement:;Scorei = i
7、nput.nextInt ();Sum = sum + scorei; / / cumulative scoreAverage = sum / score.length; / / averageSystem.out.println (the average score of participants is: + average);Exercise 3: enter the number of lines, print right triangleImport java.util.Scanner;/ * * enter the number of lines and print right tr
8、iangles* /Public class RTriAngle Public static void main (String args) Int rows = 0; / / triangle line numberSystem.out.print (enter the right triangle row number:);Scanner input = new Scanner (System.in);Rows = input.nextInt ();Print / right triangleFor (int i = 1; I rows; i+) For (int j = 1; J = 1, -)for (int i = 1, j = n-1; i + +) system.o