南京郵電大學java程序設計作業在線編程第五次作業


王利國的"Java語言程序設計第5次作業(2018)"詳細

總分:100

選擇題得分:50

 1. 以下哪一個工具是Java的編譯器?( )
A.javac.exe
B.java.exe
C.javap.exe
D.javadoc.exe
 2. 以下哪一個數據類型不屬於Java的基本數據類型?( )
A.boolean
B.char
C.int
D.String
 3. 假設有如下類的定義: public class test{ public static void main(String[] args){ int a= 3, b = 4; swap(a,b); System.out.println("a="+a + " b=" + b); } public static void swap(int a,int b){ int tmp = a; a = b; b = tmp; } } 程序運行后結果為( )
A.a=4 b=3
B.a=3 b=4
C.a=a b=b
D.無結果輸出
 4. 執行如下代碼后,b的值是( ) int a=0, b=0; do{ --b; a = a-1; }while(a>0);
A.0
B.1
C.-1
D.死循環
 5. 下列關於Java中的數組的說法,錯誤的是( )。
A.數組中的元素的類型必須相同
B.數組中的元素是有順序的
C.數組對象,屬於引用類型
D.數組的大小可以任意改變
 6. 在循環體中,如果想結束本次循環,可以使用哪個語句?( )。
A.break
B.continue
C.final
D.finally
 7. 下列標識符中,哪一個是非法標識符?( )
A.statics
B.static_10
C.10static
D.$statics10
 8. 設有數組的定義int[] a = new int[3],則下面對數組元素的引用錯誤的是( )。
A.a[0]
B.a[a.length-1]
C.int i=0;a[i]
D.a[a.length]-1
 9. int a=new int[2][3],則該數組包含( )個數組元素。
A.2
B.3
C.6
D.不確定
 10. 下面的代碼段執行之后count的值是什么( ) int count = 1; for (int i = 1; i <= 5; i++) { count += i; } System.out.println(count);
A.5
B.1
C.15
D.16

編程題得分:50

 數字加密  得分:10 / 10
import java.util.Scanner;
/** * @Author liguo * @Description 輸入一個四位數,將其加密后輸出。 * 方法是將該數每一位上的數字加9,然后除以10取余,做為該位上的新數字, * 最后將千位和十位上的數字互換,百位和個位上的數字互換,組成加密后的新四位數。 * 例如輸入1257,經過加9取余后得到新數字0146,再經過兩次換位后得到4601。 * 輸入描述 * 輸入在一行中給出一個四位的整數x,即要求被加密的數。 * 輸出描述 * 在一行中按照格式“The encrypted number is V”輸出加密后得到的新數V。 * 樣例輸入1: * 1257 * 樣例輸出1: * The encrypted number is 4601 * @Data 2018-04-27 */ public class Main { public static String jiaMi(int a) { int temp = String.valueOf( a ).length(); int b[] = new int[temp]; //整數轉化為一個從高位到低位的數組 for (int i = temp - 1; i >= 0; i--) { b[i] = a % 10; a = a / 10; } // for ( int number : b) // System.out.print( number +" "); //將各個位數加上9並除以10取余 for (int i =0 ;i <b.length;i++) { b[i] = ((b[i] + 9) % 10); } //交換千分位和十分位 b[0]千分位,b[2]十分位 b[0] += b[2]; b[2] = b[0] - b[2]; b[0] -= b[2]; //交換百分位和個分位 b[1]百0分位,b[3]個位 b[1] += b[3]; b[3] = b[1] - b[3]; b[1] -= b[3]; // 轉化為數組轉為string類型的整數 StringBuffer str = new StringBuffer(); for(int i=0;i<b.length;i++){ str.append(b[i]); } return str.toString(); // for ( int number : b) // System.out.println( number ); } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int temp = in.nextInt(); String result = jiaMi(temp); System.out.println("The encrypted number is "+result); } }
 數列排序  得分:10 / 10
import java.util.Arrays;
import java.util.Scanner;


/** * @Author liguo * @Description 2.數列排序 題目描述 將一個具有20個元素的數組中的中間10個元素按從大到小順序排序,要求使用冒泡法排序。 輸入描述 在一行中輸入20個小於50的整數,數據之間只能用1個空格間隔。 輸出描述 直接輸出變化后的數組,每個數輸出占4列列寬。 樣例輸入1: 5 4 3 2 1 8 5 2 9 6 3 1 4 7 4 1 2 3 4 5 樣例輸出1: 5 4 3 2 1 9 8 7 6 5 4 3 2 1 4 1 2 3 4 5 * @Data 2018-04-27 */ public class Main { public static void maopaoSort(int n[]){ for (int i = 1; i <=n.length-1; i++) { for (int j = 0; j <= n.length-1-i; j++) { //后面大值就進行值交換把他們換到前面 if (n[j]<=n[j+1]){ n[j]+=n[j+1]; n[j+1] = n[j] - n[j+1]; n[j]-=n[j+1]; } } } } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int []array1 = new int[20]; int []array2; // for (int value:array1) { // value = in.nextInt(); // } for (int i = 0; i <array1.length ; i++) { array1[i]=in.nextInt(); } array2= Arrays.copyOfRange(array1,5,15); //復制的數組從5-14.沒有15 // System.out.println(Arrays.toString( array2 )); maopaoSort( array2 ); // System.out.println(Arrays.toString( array2 )); // arraycopy( array2,0,array1,5,10); for (int i =0 ,j = 5; i < array2.length; i++,j++) { array1[j] = array2[i]; } for (int value:array1) System.out.printf("%4d", value); } }
 6-4-4 打印楊輝三角形  得分:10 / 10
import java.util.Scanner;

/** * @Author liguo * @Description 輸入一個整數,表示該三角形的行數 輸出描述 對應行數的楊輝三角型,三角形中的每個元素請使用“5d”的格式字符串打印輸出 樣例輸入1: 5 樣例輸出1: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 樣例輸入2: 9 樣例輸出2: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 * @Data 2018-04-27 */ public class Main{ public static void yangHui(int n){ int a [][] = new int [n ][n]; a[0][0] = 1; for (int i = 1; i < n; i++) { a[i][0]= 1; a[i][i]=1; for (int j = 1; j < i; j++) { a[i][j] = a[i-1][j-1]+a[i-1][j]; } } //打印二維數組.顯示楊輝三角形 for (int i = 0; i < n; i++) { for (int j = 0; j <=i ; j++) { System.out.printf("%5d",a[i][j]); } System.out.println(); } } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int n = in.nextInt(); yangHui( n ); } }
 6-4-3 構造指定的數列  得分:10 / 10
import java.util.ArrayList;
import java.util.Scanner;

/** * @Author liguo * @Description 題目描述 編寫函數fun,求出a到b之內能被7或者11整除,但不能同時被7和11整除的所有正數,並將他們放在數組中,函數返回這些數的個數。 編寫main函數,輸入a,b的值並調用函數進行運算。 輸入描述 從鍵盤輸入a,b的值(1<=a<=b<1000),用以下格式字符串輸入a,b的值: 在C語言中使用:scanf("%d%d",&a,&b); 在Java語言中使用Scanner對象的nextInt()方法獲取a,b的值。 輸出描述 用以下格式字符串輸出數組中的元素的值:"%d "(注意:%d后面有一個空格) 樣例輸入1: 1 20 <回車>ii 樣例輸出1: 7 11 14 樣例輸入2: 50 100 <回車> 樣例輸出2: 55 56 63 66 70 84 88 91 98 99 * @Data 2018-04-28 21:11 */ public class Main { public static void fun(int a,int b){ boolean flag= true; int count = 0; // int []arr = new int[b-a]; ArrayList <Integer> arr = new ArrayList <>( b-a ); for (int i = a; i <= b; i++) { flag = (i%7==0||i%11==0); if (i%77==0) flag = false; if (flag) { arr.add( i ); } } arr.trimToSize(); arr.toArray( ); for (int value:arr) System.out.printf("%d ",value); } public static void main(String[] args) { Scanner in = new Scanner( System.in ); int a = in.nextInt(); int b = in.nextInt(); fun( a,b ); } }
 6-4-1 求平均值  得分:10 / 10
import java.util.Arrays;
import java.util.Scanner;
/** * @Author liguo * @Description * 編程從鍵盤上輸入20個整數,求去掉最大值和最小值以后那些元素的平均值 輸入描述 連續輸入20個整數,用空格作為分隔 輸出描述 C語言和Java語言中,用以下格式字符串輸出結果:"count=%d,average=%.2f\n" Python語言中,用以下格式字符串輸出結果:"count={},average={:.2f}" 樣例輸入1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 樣例輸出1: count=18,average=10.50 樣例輸入2: 90 80 70 100 50 60 70 100 75 85 85 90 80 70 65 50 60 70 80 90 樣例輸出2: count=16,average=76.25 * @Data 2018-04-29 0:12 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner( System.in ); int []arr = new int[20]; int count=20; int sum=0; double average = 0; //錄入數據 for (int i = 0; i < 20; i++) { arr[i]=in.nextInt(); } Arrays.sort( arr ); //去掉出去除兩頭的最大最小值 for (int i = 1; i < 19; i++) { if (arr[i] == arr[0] | arr[i] == arr[19]) { arr[i] = 0; count--; } } //去掉兩個首尾的最大值最小值 arr[0] = 0; arr[19]=0; count-=2; for (int value:arr) sum+= value; average = (double) sum/count; System.out.printf( "count=%d,average=%.2f\n",count,average); } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM