首页 > 代码库 > JAVA语言程序设计基础课后习题第六章
JAVA语言程序设计基础课后习题第六章
//exercise 6.1package second;import java.util.Scanner;public class first { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter the number of students:"); int number=in.nextInt(); System.out.print("Enter "+number+" scores:"); int []score=new int[number]; getscores(score); int best=max(score); for(int i=0;i<number;i++){ System.out.println("Student "+i+" score is "+score[i]+" and grade is "+ grade(score[i],best)); } } public static void getscores(int []score){ Scanner in=new Scanner(System.in); for(int i=0;i<score.length;i++){ score[i]=in.nextInt(); } } public static int max(int[] score){ int max=score[0]; for(int i=0;i<score.length;i++){ if(max<score[i]) max=score[i]; } return max; } public static char grade(int score,int max){ if(score>=max-10) return ‘A‘; else if(score>=max-20) return ‘B‘; else if(score>=max-30) return ‘C‘; else if(score>=max-40) return ‘D‘; else return ‘F‘; }}//exercise 6.2package second;import java.util.Scanner;public class second { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []number=get(); reverseprint(number); } public static int[] get(){ Scanner in=new Scanner(System.in); int[] number=new int[10]; System.out.println("input 10 number:"); for(int i=0;i<number.length;i++){ number[i]=in.nextInt(); } return number; } public static void reverseprint(int[]Array){ for(int i=Array.length-1;i>=0;i--){ System.out.print(Array[i]+" "); } }}//exercise 6.3package second;import java.util.Scanner;public class third { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int[] newarray=new int[100]; int temp; for(int i=0;i<newarray.length;i++){ newarray[i]=0; } System.out.print("Enter the integers between 1 and 100:"); while((temp=in.nextInt())!=0){ newarray[temp]++; } resultprint(newarray); } public static void resultprint(int []array){ for(int i=0;i<array.length;i++){ if(array[i]==1) System.out.println(i+" occurs "+array[i]+" time"); if(array[i]!=0&&array[i]!=1) System.out.println(i+" occurs "+array[i]+" times"); } }}//exercise 6-4package first;import java.util.Scanner;public class first { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int []score=new int[100]; int i=0,sum=0,count=0; System.out.print("input integer:"); while((score[i]=in.nextInt())!=-1){ sum+=score[i++]; count++; } int average=sum/count; int big=0,small=0; for(int j=0;j<count;j++){ if(score[j]<average) small++; else big++; } System.out.println("average is "+average); System.out.println("better than average is "+big); System.out.println("small than average is "+small); }}//exercise 6-5package first;import java.util.Scanner;public class second { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.print("Enter ten numbers:"); int []integer=new int[10]; int count=0; for(int i=0;i<10;i++){ boolean judge=false; int temp=in.nextInt(); for(int j=0;j<count;j++){ if(temp==integer[j]){ judge=true; } } if(!judge){ integer[count++]=temp; } } System.out.print("input integer:"); for(int i=0;i<count;i++){ System.out.print(integer[i]+" "); } }}//exercise 6-7package first;public class third { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int []counts=new int[10]; for(int i=0;i<100;i++){ int random=(int)(Math.random()*10); counts[random]++; } for(int i=0;i<10;i++){ System.out.print(i+" "); } System.out.println(); for(int i=0;i<10;i++){ System.out.print(counts[i]+" "); } }}//exercise 6-8package first;public class fourth { /** * @param args */ public static int average(int[]array){ int sum=0,count=0; for(int i=0;i<array.length;i++){ sum+=array[i]; count++; } return sum/count; } public static double average(double []array){ double sum=0; int count=0; for(int i=0;i<array.length;i++){ sum+=array[i]; count++; } return sum/count; }}//exercise 6-9package first;public class fifth { /** * @param args */ public static double min(double []array){ double min=array[0]; for(int i=0;i<array.length;i++){ if(min>array[i]) min=array[i]; } return min; } }//exercise 6-10package first;public class seventh { /** * @param args */ public static int indexOfSmallestElement(double[] array){ double min=array[0]; for(int i=0;i<array.length;i++){ if(min>array[i]) min=array[i]; } for(int i=0;i<array.length;i++){ if(min==array[i]) return i; } return 0; }}
JAVA语言程序设计基础课后习题第六章
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。