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+z
100&&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<a;i++){
if(a%i
0){
System.out.println("不是素数");
break;
}
}
if(a==i) {
System.out.println("是素数");
}
}

}

posted @ 2019-04-09 15:56  杨垚1  阅读(358)  评论(0编辑  收藏  举报