1、Java程序设计作业一Java程序设计作业一一、 5.1(1) 题目:(统计正数和负数的个数然后计算这些数的平均值)编写程序,读入未指定个数的整数,判断读入的正数有多少个,读入的负数有多少个,然后计算这些输入值的总和及其平均值(不对0计数)。当输入为0时,表明程序结束。将平均值以浮点数显示。(2) 代码import java.util.Scanner;public class home1 public static void main(String args) int number,count1=0,count2=0,total=0; double a=0; System.out.printl
2、n(Enter an integer,the input ends if it is 0:);Scanner input = new Scanner (System.in); do number=input.nextInt(); if(number0) count1+; if(number100&i1000;i+) if(i%5=0&i%6=0) count+; System.out.printl(i+); if(count%10=0) System.out.println(n); (3) 运行结果The number is:120 150 180 210 240 270 300 330 36
3、0 390 420 450 480 510 540 570 600 630 660 690 720 750 780 810 840 870 900 930 960 990三、 5_40(1) 题目:(模拟:正面或反面)编写程序,模拟硬抛币一百万次,显示出现正面和反面的次数(2) 解题思路 输入/输出设计: 输入内容:随机产生0和1两个数 输出内容:正面次数,反面次数 变量/常量设计: int count=(int)(Math.random() * 2);/随机产生0,1两位数 int i;/控制抛币次数int positive=0;/正面次数int negative=0;/反面次数 数据处理步
4、骤设计和方法设计:程序处理步骤如下:第一步:初始化i,positive,negative的值;第二步: 控制抛币次数,进入循环;第三步:随机产生0,1两个数赋给count;第四步:如果count=0,negative+,如果count=1,positive+第五步:输出positive negative 测试设计1分:产生随机数0和1(3) 代码public class home3 public static void main(String args) int i,positive=0,negative=0; System.out.println(The negative is:); Sys
5、tem.out.println(The positive is:); for(i=1;i=ch1&i=ch2;i+) System.out.print(char)i+ ); count+; if(count%10=0) System.out.println(n); (3) 运行结果The number is:1 2 3 4 5 6 7 8 9 : ; ? A B C D E F G H I J K L M N O P Q R S T U V W X Y Z六、 7_4(1) 题目:(分析成绩)编写一个程序,读入个数不确定的考试分数,并且判断有多少个分数是大于或等于平均分,多少个分数是低于平均分
6、的,输入一个负数表示输入结束,假设最高分为100(2) 代码import java.util.Scanner;public class home6 public static void main(String args)int number,count1=0,count2=0,total=0; double a=0;System.out.println(Enter your score:); Scanner input = new Scanner (System.in); do number=input.nextInt(); if(number=a) count1+; if(numbera)
7、count2+; /if(number=0&number=aversge +count1); System.out.println(The number =aversge2The number average1The average is:59.0七、 7_8(1) 题目:编写两个重载的方法,使用下面两个方法头,返回一个数组的平均数(2) 代码public static int average(int array)public static double average(double array)public class home7 public static void main(String
8、 args) int a=new int10; double d=new double10; Scanner input = new Scanner (System.in); System.out.println(Please intput ten double number); for( int x=0;xd.length;x+) dx=input.nextDouble(); System.out.println(The doubler average is:+average(d);public static double average(double array)double d_tota
9、l=0;double d_aver=0;for(int x=0;xarray.length;x+)d_total=d_total+arrayx;d_aver=d_total/array.length; return d_aver;public static int average(int array) int total=0,aver=0;for(int i=0;iarray.length;i+)total=total+arrayi;aver=total/array.length;return aver;(3) 运行结果Please intput ten double number1.2 3.
10、4 3.4 6.7 3.5 4.0 3 5 6 7The doubler average is:4.32八、 7_9(1) 题目:(找出最小数)编写一个方法,使用下面的方法头求出一个整数数组中的最小元素public static double min(double array)编写测试程序,提示用户输入十个数字,调用这个方法返回最小值,显示其最小值(2) 代码import java.util.Scanner;public class home8 public static void main(String args) double d=new double10; Scanner input =
11、 new Scanner (System.in); System.out.println(Please intput ten number); for( int x=0;xd.length;x+) dx=input.nextDouble(); System.out.println(The min number is:+min(d); public static double min(double array) double a=array0; for(int i=0;iarray.length;i+) if(arrayia) a=arrayi; return a; (3) 运行结果:(4) Please intput ten number7.0 6.0 7.6 4 5 6 7 3 2 6The min number is:2.0
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1