《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.println("Enter an integer,the input ends if it is 0:");
Scanner input = new Scanner (System.in);
do
{
number=input.nextInt();
if(number>0)
count1++;
if(number<0)
count2++
total=total+number;
a=total/(count1+count2);
}while(number!=0);
System.out.println("The number of positives is:"+count1); System.out.println("The number of negatives is:"+count2); System.out.println("The total is:"+total);
System.out.println("The average is:"+a);
}
}
(3)运行结果
Enter an integer,the input ends if it is 0:
5 6 7 8 9 0
The number of positives is:5
The number of negatives is:0
The total is:35
The average is:7.0
二、5-10
(1)题目:(找出能被5和6整除的数)编写程序,显示从100到1000之间所有能被5和6整除的数,每行显示10个,数字之间用一个空格字符隔开
(2)代码
public class home2 {
public static void main(String[] args){
int i,count=0;
System.out.println("The number is:");
for(i=101;i>100&&i<1000;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 360 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;//反面次数
●数据处理步骤设计和方法设计:
程序处理步骤如下:
第一步:初始化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:");
System.out.println("The positive is:");
for(i=1;i<=1000000;i++)
{
int count=(int)(Math.random() * 2);
if(count==0)
negative++;
if(count==1)
positive++;
}
System.out.println(negative);
System.out.println(positive);
}
}
(4)运行结果
The negative is:
The positive is:
500164
499836
四、6_2
(1)题目:(求一个整数各位数字之和)编写一个方法,计算一个整数各位数字之和
(2)解题思路
●输入/输出设计:
输入内容:输入一个数number;
输出内容:各项数之和即输出total;
●变量/常量设计:
int k=0,total=0;//k取一个数的各位total计算各位数之和;
int number=input.nextInt();;//输入一个number数
●数据处理步骤设计和方法设计:
程序处理步骤如下:
第一步:初始化k,total的值;
第二步:提示用户输入number;
第三步:如果number不是个位数则进入循环利用%10来取个位数并且赋给k,利用/10去掉个位数并且赋给新的number循环此操作,并且计算total;
第四步:如果number是个位数则total=total+number;
第五步:输出total;
●测试设计1分:
利用循环取余和除法来得到各个位数并相加
(3)代码
import java.util.Scanner;
public class home4 {
public static void main(String[] args){
int k=0,total=0;
Scanner input = new Scanner (System.in);
System.out.println("Please input a number:"); int number=input.nextInt();
System.out.println("the total is:");
while(number/10!=0)
{
k=number%10;
number=number/10;
total=total+k;
}
total=total+number;
System.out.println(total);
}
}
(4)运行结果
Please input a number:
456
the total is:
15
五、6_12
(1)题目:编写一个打印字符的方法
(2)代码
public class home5 {
public static void main(String[] args){
int ch1=(int)'1';
int ch2=(int)'Z';
int count=0;
System.out.println("The number is:");
for( int i=ch1;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)题目:(分析成绩)编写一个程序,读入个数不确定的考试分数,并且判断有多少个分数是大于或等于平均分,多少个分数是低于平均分的,输入一个负数表示输入结束,假设最高分为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<0)
break;
if(number>=a)
count1++;