java if作业_Java实训作业

1.编写程序:声明一个整型变量a,并赋初值5,在程序中判断a是奇数还是偶数,然后输出判断的结果。

public class hello{

public static void main(String[] args){

int a=5;

if(a%21) System.out.println("奇数");

else System.out.println("偶数");

}

}

2.编写程序:从键盘输入圆的半径,计算圆的面积并输出。

public class hello{

public static void main(String[] args{

Scanner sc =new Scanner (System.in);

final float p=3.14f;

float r,s;

System.out.println("请输入圆的半径:r=\n");

r=sc.nextFloat();

s=prr;

System.out.println("圆的面积为:"+s);

}

}

3.编写程序:实现一个数字加密器。运行时输入加密前的整数,通过加密运算后,输出加密后的结果,加密结果仍为一整数。加密规则为:加密结果 = (整数10+5) / 2 + 3.14159。

import java.util.;

public class hello{

public static void main(String[] args){

Scanner sc=new Scanner (System.in);

int a,sum;

System.out.println("请输入一个整数:a=\n");

a=sc.nextInt();

sum=(int)((a*10+5) / 2 + 3.14159);

System.out.println(sum);

}

}

4.编写程序公鸡5元/只,母鸡3元/只,小鸡3只/元,问100元买100只鸡,公鸡、母鸡、小鸡各几只?

public class ji{

public static void main(String[] args){

int x,y,z;

for(x=0;x<=100;x++)

for(y=0;y<=100;y++)

for(z=0;z<=100;z++)

if(x+y+z100&&5x+3y+z/3==100)

System.out.println("公鸡的数量:"+x+",母鸡的数量:"+y+"小鸡的数量:"+z);

}

}

5.编写程序:有1、2、3、4共4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。

public class shuzi {

public static void main(String[] args) {

int i = 0;

int j = 0;

int k = 0;

int t = 0;

for(i = 1; i <= 4; i++) {

for(j = 1; j <= 4; j++) {

for(k = 1; k <= 4; k++) {

if(i != j&& j != k && i!= k) {

t += 1;

System.out.println(i100 + j10 + k);

}

}

}

}

System.out.println("总共能够组成" + t + "个数字!");

}

}

6.编写程序:判断输入的整数是否为素数。

public class Issushu {

public static void main(String[] args) {

int a;

int i;

System.out.println("请输入一个数字:");

Scanner num = new Scanner(System.in);

a = num.nextInt();

if(a1){

System.out.println("不是素数");

}

for(i=2;i

if(a%i0){

System.out.println("不是素数");

break;

}

}

if(a==i) {

System.out.println("是素数");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值