文章目录
- 1.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
- 2.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
- 3.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
- 4.给定两个浮点数,输出它们的和、差、积和商。
- 5.编写一个程序,要求将一个浮点数强制转化成整型后再输出。
- 6.计算身体质量指数(BMI)。BMI 指数(Body Mass Index)是国际上常用的衡量人体胖瘦程度的一个标准,其计算方法是用体重(公斤)数除以身高(米)的平方得出的数据。我国的标准如表 2-1 所示。
- 7.打印如下图形:
- 8.计算出 3 至 1000 范围内最大的十个素数,放入数组中,并计算出其累加和。
- 9.水仙花数是三位数,它的各位数字的立方和等于这个三位数本身,例如:371=33+73+13,371 就是一个水仙花数。找出所有水仙花数。
- 10.编写程序读取一组正数,找出它们的最大数,然后计算该数的出现次数,输入是以 0结束。比如:输入 3 5 2 5 5 5 0,程序找出最大数是 5,它出现的次数是 4。
【实验目的】
1.掌握 Java 语言的标识符、保留字、基本数据类型。
2.掌握 Java 语言中各种运算符表达式的基本用法。
3.熟练掌握使用 if 语句进行单分支结构和双分支结构程序设计的基本用法。
4.熟练掌握使用 if 语句嵌套及 switch 语句进行多分支结构程序设计的基本用法
5.熟练掌握 while、do-while 和 for 三种语句组成循环结构的基本用法。
【实验内容】
1.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
public class SY11{
public static void main(String args[]){
char x,y,z;
x='W';
y='\\';
z='\116';
System.out.print(x);
System.out.print(y);
System.out.print(z);
}
}
运行结果: W\N
2.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
public class SY12{
public static void main(String args[]){
boolean x,y,z;
int a=6,b=24;
x=(a<b);
y=(a==b);
z=(a+b!=26);
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("z="+z);
}
}
运行结果:
x=true
y=false
z=true
3.仔细读下面的 Java 语言源程序,自己给出程序的运行结果。
public class SY13{
public static void main(String args[]){
int x,y,z=0;
for (x=1;x<=10;x++)
for (y=1;y<10;y++){
if (x==5) continue;
if (y>5) break;
z++;
}
System.out.println("z="+z);
}
}
运行结果: z=45
4.给定两个浮点数,输出它们的和、差、积和商。
import java.util.Scanner;
public class Fudianshu {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
double x = sc.nextDouble();
double y = sc.nextDouble();
System.out.println("x+y="+(x+y));
System.out.println("x-y="+(x-y));
System.out.println("x*y="+(x*y));
System.out.println("x/y="+(x/y));
}
}
5.编写一个程序,要求将一个浮点数强制转化成整型后再输出。
package javaTest;
public class Qiangzhi {
public static void main(String[] args) {
double n =3.1415926;
System.out.println((int)n);
}
}
6.计算身体质量指数(BMI)。BMI 指数(Body Mass Index)是国际上常用的衡量人体胖瘦程度的一个标准,其计算方法是用体重(公斤)数除以身高(米)的平方得出的数据。我国的标准如表 2-1 所示。
import java.util.Scanner;
public class Shentizhiliang {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请输入该人的体重(kg)和身高(m)");
double T = sc.nextDouble();
double H = sc.nextDouble();
double BMI = T/(H*H);
if(BMI <=18.4) {
System.out.println("偏瘦");
}
else if(BMI >18.4 && BMI <=23.9) {
System.out.println("正常");
}
else if(BMI >23.9 && BMI <=27.9) {
System.out.println("过重");
}
else if(BMI >28.0) {
System.out.println("肥胖");
}
}
}
7.打印如下图形:
public class Dayin {
public static void main(String[] args){
for(int i=0;i<6;i++){
for(int j=5-i;j>0;j--){
System.out.print(" ");
}
for(int k=0;k<=2*i;k++){
System.out.print("*");
}
System.out.println();
}
}
}
8.计算出 3 至 1000 范围内最大的十个素数,放入数组中,并计算出其累加和。
public class Sushu {
public static void main(String[] args) {
int sum = 0;//十个素数的累加和
int I=0;//统计找到多少个素数
for(int n=1000;n>=3;n--) {//从1000开始往小找就能找最大的
int yn=1;//yn为1时经过下面循环后如果没有变成0 说明它是素数
for(int i=2; i<n; i++) {
if(n % i==0) {//发现能被小的数整除不是素数终止小循环
yn=0;
break;
}
}
if(yn==1) {
System.out.print(n+" ");
sum = sum + n;
I++;
if(I==10) {//如果已经找够十个终止大循环
break;
}
}
}
System.out.println("累加和为: sum = "+sum);
}
}
9.水仙花数是三位数,它的各位数字的立方和等于这个三位数本身,例如:371=33+73+13,371 就是一个水仙花数。找出所有水仙花数。
public class Shuixianhua {
public static void main(String[] args) {
for(int n=100;n<1000;n++)
{
//提取个十百位
int ge = n%10;
int shi = n%100/10;
int bai = n/100;
if(n==(ge*ge*ge)+(shi*shi*shi)+(bai*bai*bai)) {
System.out.println(n);
}
}
}
}
10.编写程序读取一组正数,找出它们的最大数,然后计算该数的出现次数,输入是以 0结束。比如:输入 3 5 2 5 5 5 0,程序找出最大数是 5,它出现的次数是 4。
import java.util.Scanner;
public class Fmax {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int a = sc.nextInt();
int count = 0;
int max = a;
while(a != 0) {
if(a>max) {
max=a;
}
else if(a==max)
{
count++;
}
a = sc.nextInt();
}
System.out.println("最大数是:"+max+"出现的次数:"+count);
}
}