11.(判断一个数是否为偶数)编写程序读入一个整数并判断其是否为偶数。
package cutestFox.github.io.execise;
import java.util.Scanner;
class Code011 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个整数:");
int num = sc.nextInt();
if ((num % 2) == 0) {
System.out.println("您输入的是偶数");
} else
System.out.println("您输入的不是偶数");
}
}
12.编写程序输入一个整数,判断其能否被5和6整除,是否能被5或6整除,是否能被5或6整除但是不能被5和6整除.
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code012 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请您输入一个整数:");
int num = sc.nextInt();
if((num%5)==0&&(num%6)==0) {
System.out.println(num+"能被5和6整除");
}if((num%5)==0||(num%6)==0) {
System.out.println(num+"能被5或6整除");
}if(((num%5)==0||(num%6)==0)^((num%5)==0&&(num%6)==0)) {
System.out.println(num+"能被5或6整除但是不能被5和6整除");
}else
System.exit(0);
}
}
13.编写程序:录入金额,年利率和年数,显示终值(将来的金额),计算公式如下:终值 = 现值(1+年利率)年数
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code013 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入存储的金额:");
double xx = sc.nextDouble();
System.out.println("请输入年利率:");
double yy = sc.nextDouble();
System.out.println("请输入存入年数:");
int kk = sc.nextInt();
double res = xx*(1+yy)*kk;
System.out.println("您的连本带息最终值为:"+res);
}
}
14.(三个整数排序)编写程序对录入三个整数排序.整数由输入对话框读入,并对其排序.
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code014 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请您输入第一个整数:");
int aa = sc.nextInt();
System.out.println("请您输入第二个整数:");
int bb = sc.nextInt();
System.out.println("请您输入第三个整数:");
int cc = sc.nextInt();
int temp = 0;
if(aa>bb) {
temp = aa;
aa = bb;
bb = temp;
}if(aa>cc) {
temp = cc;
aa = cc;
cc = temp;
}if(bb>cc) {
temp = cc;
bb = cc;
bb= temp;
}
System.out.println("排序结果为:\n"+aa+"\n"+bb+"\n"+cc);
}
}
15.(计算三角形的周长)编写程序,录入三角形的三条边,如果有效,计算他的周长;否则 显示输入无效。如果任意两边之和大于第三边,输入有效
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code015 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入三角形的第一条边长:");
int x = sc.nextInt();
System.out.println("请输入三角形的第二条边长:");
int y = sc.nextInt();
System.out.println("请输入三角形的第三条边长:");
int z = sc.nextInt();
if((judgeTriangle(x, y, z))==true) {
System.out.println("输入有效");
Calculate(x, y, z);
}else
System.out.println("输入无效");
}
public static boolean judgeTriangle(int x,int y,int z) {
if(x+y<=z)
return false;
if(x+z<=y)
return false;
if(z+y<=x)
return false;
else
return true;
}
public static int Calculate(int x,int y,int z) {
int circle = x+y+z;
System.out.println("周长为:"+circle);
return 0;
}
}
计算数据的阶乘值
package cutestFox;
import java.util.Scanner;
public class Code016 {
// 计算数据的阶乘值
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int kk = sc.nextInt();
long res = 1L;
if(kk<0) {
System.out.println("无意义");
}
else {
for(int k=1;k<=kk;k++)
res*=k;
}
System.out.println(res);
}
}
17.互换两个数的值(不允许使用中间变量)
package cutestFox;
import java.util.Scanner;
public class Code017 {
//互换两个数的值(不允许使用中间变量)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int x = sc.nextInt();
System.out.println("请输入第二个数:");
int y = sc.nextInt();
Exchange(x, y);
}
public static void Exchange(int x, int y) {
x = x + y;
y = x - y;
x = x - y;
System.out.println("互换后的数据为:" + x + "," + y);
}
}
18.输出三个数最大值和最小值
package cutestFox;
import java.util.Scanner;
//输出三个数最大值和最小值
public class Code018 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int a = sc.nextInt();
System.out.println("请输入第一个数:");
int b = sc.nextInt();
System.out.println("请输入第一个数:");
int c= sc.nextInt();
System.out.println("您输入的数字中最大值为:"+MAX(a, b, c));
System.out.println("您输入的数字中最小值为:"+MIN(a, b, c));
}
public static int MIN(int k1, int k2, int k3) {
int res = k1;
if (res > k2)
res = k2;
if (res > k3)
res = k3;
return res;
}
public static int MAX(int k1, int k2, int k3) {
int res = k1;
if (res < k2)
res = k2;
if (res < k3)
res = k3;
return res;
}
}
19.(查找当月的天数)编写程序,提示用户输入年月,然后显示该月的天数,列如:如果用户输入2000年2月时。应该显示2000年2月有29天,如果用户输入2005年3月份时,应该显示是2005年3月份有31天。
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code019 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入查询的年份");
int year = sc.nextInt();
System.out.println("请输入查询的月份");
int month = sc.nextInt();
int res = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
res = 31;
break;
case 4:
case 6:
case 9:
case 11:
res = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
res = 29;
else
res = 28;
break;
default:
break;
}
System.out.println("您所查询的" + year + "年" + month + "月份有:" + res + "天");
}
}
20.(计算平均数)编写程序,读书个数是不确定的,求读入的整数的平均值,以0结束,0不参与计数,当用户输入0时i,程序结束将平均值作为一个浮点数显示(例如,如果输入1,2和0,平均值应当为1.5)
package cutestFox.github.io.execise;
import java.util.Scanner;
public class Code020 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double sum = 0;
double kk = 0;
while (true) {
System.out.println("请输入数字:");
double xx = sc.nextDouble();//录入输入的值
if (xx != 0)
sum = sum + xx;
if (xx == 0)
break;
kk++;//统计次数
}
System.out.println("平均数为:" + sum / kk);
}
}