JAVA语言程序设计基础课后习题第六章

//exercise 6.1
package 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.2
package 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.3
package 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-4
package 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-5
package 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-7
package 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-8
package 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-9
package 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-10
package 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;
    }

}
时间: 2024-10-07 04:40:27

JAVA语言程序设计基础课后习题第六章的相关文章

JAVA语言程序设计基础课后习题第四章

//exercise 4.1 package fourthchapterexercise1; import java.util.Scanner; public class first { public static void main(String[] args) { // TODO Auto-generated method stub int integer=0,sum=0,positive=0,negative=0; Scanner in=new Scanner(System.in); Sy

JAVA语言程序设计基础课后习题第五章

//exercise 5.1 package fivechapterexercise1; public class first { public static void main(String[] args) { // TODO Auto-generated method stub final int NUMBER_OF_PENTAGONAL_PER_LINE=10; for(int i=1;i<=100;i++){ System.out.print(getpentagonalnumber(i)

JAVA语言程序设计基础课后习题第三章

//exercise 3.1 package thirdchapterexercise1; import java.util.Scanner; public class first { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in =new Scanner(System.in); System.out.print("Enter a,b,c:"); double

java语言程序设计基础课后习题第二章

1 //exercise 2.2 2 package secondchapterexercise1; 3 4 public class first01 { 5 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 double miles=100; 9 double KILOMETERS_PER_MILE=1.609; 10 double kilometers=miles*KILOMET

Java语言程序设计基础篇 数组(六)

Java语法之数组 数组的定义 数组是对象. 如:int [ ]  x = new int[100];或 :int x [ ]  = new int[100];(这种方式主要是为了适应C/C++程序员) 声明一个数组变量:int [ ] x;并不会在内存中给数组分配任何空间,仅创建一个引用数组的存储地址. 数组创建后,其元素赋予默认值,数值型基本数据类型默认值为0,char类型为'\u0000',boolean类型为false. 数组的静态初始化 如:int [ ] x = new int [

java语言的科学与艺术 第六章 课后编程

欢迎大家转载,为保留作者成果,转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也可以下载!如果你没有积分,可以联系我索要! 1. package SixthCharter; /* * File: Poker.java * --------------------------- * 这是第6章的第一题 * Author luoriver */ import acm.program.*; import acm.util.*; public class

Java语言程序设计基础篇 循环(四)

①打印:***** **** *** ** * for(int x=1; x<=5; x++) { for(int y=x; y<=5; y++) { System.out.print("*"); //向下一般的格式for(int y=x; y<=5; y++) } System.out.println(); } ②打印:* ** *** **** ***** for (int x=1; x<=5 ;x++ ) { for (int y=1;y<=x ;y

Java语言程序设计基础篇 方法(五)

生成随机字符 生成随机字符就是生成0到65535之间的一个随机整数,因为0<=Math.random()<1.0,必须在65535+1 (int) (Math.random() * (65535+1)) 随机生成小写字母 public class RandomCharacter { public static char getRandomCharacter(char ch1,char ch2){ return (char)(ch1 +Math.random() * (ch2 - ch1 + 1

Java语言程序设计基础篇 循环(四)练习

*4.21(计算不同利率下的贷款)编写程序,让用户输入贷款总额及以年为单位的贷款期限,以1/8为递增量,显示从5%到8%的利率下每月支付额和总偿还额.假设输入贷款总量为10 000,还贷期限为5年,所显示的输出如下: 贷款总额:to 000 年数:5 利率月支付额总偿还额 5%188 .71   11322.74 5 .125%189.28   11357.13 5 .25%189.85   11391.59 ... //Exercise3_26.java: displays the month