1 本题水题,记住要知道输入格式即可
1 import java.util.Scanner; 2 3 public class test { 4 public static void main(String[] args) { 5 // 从键盘中读取数据 6 Scanner input = new Scanner(System.in); 7 int a = input.nextInt(); 8 if(a%2==0) 9 System.out.println("这个数是偶数"); 10 else 11 System.out.println("这个数是奇数"); 12 } 13 }
2 本题水题,知道闰年的判断条件即可
1 import java.util.*; 2 public class ch03_2 { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 int a = in.nextInt(); 6 //判断闰年的方法:能被4整除,但不能被100整除;能被400整除 7 //由2个判断条件组成,满足一个即可,中间使用或连接 8 if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0)) 9 System.out.println(a+"是闰年"); 10 else 11 System.out.println(a+"不是闰年"); 12 } 13 }
3 这一题需要进行数值的替换,用if条件语句,逐个进行比较,将最大值和最小值储存在变量max和min中
全部比较完毕就可以得到最大值和最小值
1 import java.util.*; 2 public class ch03_3 { 3 public static void main(String[] args) { 4 int a[] = new int[4]; 5 //给最大值和最小值赋初值为第一个数 6 int max = a[0]; 7 int min = a[0]; 8 Scanner in = new Scanner(System.in); 9 System.out.println("输入四个整数:"); 10 for (int i = 0; i < 4; i++) { 11 int j = in.nextInt(); 12 a[i] = j; 13 if(a[i] > max) 14 max = a[i]; 15 if(a[i] < min) 16 min = a[i]; 17 } 18 System.out.println("最大值是:"+max+"最小值是:"+min); 19 } 20 }
4 本题水题,知道判别式,和求根的方式即可
1 import java.util.Scanner; 2 import static java.lang.StrictMath.sqrt; 3 4 public class ch03_4 { 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 System.out.println("输入a,b,c"); 8 double a = in.nextDouble(); 9 double b = in.nextDouble(); 10 double c = in.nextDouble(); 11 double x1,x2; 12 double x; 13 //x为一元二次方程的判别式 14 x = b * b - 4 * a * c; 15 x1 = (-b + Math.sqrt(x)) / (2 * a); 16 x2 = (-b - Math.sqrt(x)) / (2 * a); 17 if(x > 0)//方程有两个实数根{ 18 System.out.println("方程有两个实数根"); 19 System.out.println("结果为:"+x1+""+x2); 20 } 21 else if(x == 0)//方程有一个实数根{ 22 System.out.println("方程有一个实数根"); 23 System.out.println("结果为:"+x1); 24 } 25 else//方程没有实数根 26 System.out.println("方程没有实数根"); 27 } 28 }
5 按照题目要求用switch语句,不要忘记case语句要加入break退出
1 import java.util.*; 2 public class ch03_5 { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 System.out.println("请输入一个成绩:"); 6 Integer sco = in.nextInt(); 7 switch (sco / 10){ 8 case 10: case 9: 9 System.out.println("优秀"); 10 break; 11 case 8: case 7: 12 System.out.println("良好"); 13 break; 14 case 6: 15 System.out.println("及格"); 16 break; 17 case 5: 18 System.out.println("不及格"); 19 break; 20 default: 21 System.out.println("差"); 22 break; 23 } 24 } 25 }
6 这道题和第三题一样,使用循环进行比较选择出最大值最小值即可
1 import java.util.Scanner; 2 public class ch03_6 { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 int a[] = new int[10]; 6 int max = a[0]; 7 int min = a[0]; 8 System.out.println("输入十个数字:"); 9 for (int i = 0; i < 10; i++) { 10 Integer input = in.nextInt(); 11 a[i] = input; 12 if(a[i]>max) 13 max = a[i]; 14 if(a[i]<min) 15 min = a[i]; 16 } 17 System.out.println("最大值是"+max+"最小值是:"+min); 18 } 19 }
7 这题是日期,注意区分闰年二月和平年二月的天数
1 public class ch03_7 { 2 public static void main(String[] args) { 3 Scanner in = new Scanner(System.in); 4 System.out.println("输入年和月"); 5 int year = in.nextInt(); 6 int month = in.nextInt(); 7 int day; 8 switch (month) 9 { 10 //31天的月份 11 case 1: case 3: case 5: case 7: case 8: case 10: case 12: 12 day = 31; 13 System.out.println(year+"年"+month+"月有"+day+"天"); 14 break; 15 //30天的月份 16 case 4: case 6: case 9: case 11: 17 day = 30; 18 System.out.println(year+"年"+month+"月有"+day+"天"); 19 break; 20 //将二月单独拿出来判断是否是闰年 21 case 2: 22 //是闰年有29天 23 if((year % 4 == 0) & (year % 100 != 0) | (year % 400 == 0)){ 24 day = 29; 25 System.out.println(year+"年"+month+"月有"+day+"天"); 26 break; 27 } 28 //不是闰年只有28天 29 else{ 30 day = 28; 31 System.out.println(year+"年"+month+"月有"+day+"天"); 32 break; 33 } 34 } 35 36 } 37 }
8 这一题主要是要注意生肖的位置,调整好之后就可以确保答案准确
1 import java.util.*; 2 public class ch03_8 { 3 public static void main(String[] args) { 4 while(true) { 5 //存放生肖信息的数组,需要经过自己调试得出顺序 6 String[] ShengXiao = new String[]{ 7 "猴(monkey)", "鸡(rooster)", 8 "狗(dog)", "猪(pig)", 9 "鼠(rat)", "牛(ox)", 10 "虎(tiger)", "兔(rabbit)", 11 "龙(dragon)", "蛇(snake)", 12 "马(horse)", "羊(sheep)"}; 13 Scanner in = new Scanner(System.in); 14 System.out.println("输入年份"); 15 int year = in.nextInt(); 16 int a = year % 12; 17 System.out.println(year + "年属" + ShengXiao[a]); 18 } 19 } 20 }
9 这一题额外添加了一个判断是否输入是否出错的语句,随机数使用Random方法
1 import java.util.*; 2 public class ch03_9 { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 System.out.println("你出什么?(石头(2)、剪刀(1)、布(0))"); 6 int num = in.nextInt(); 7 String[] a = new String[] {"布","剪刀","石头"}; 8 boolean isNum = false; 9 //安全性检测,出现输入错误要求重新输入 10 while(!isNum){ 11 if(num > 2 | num < 0){ 12 System.out.println("输入错误请重新输入:"); 13 num = in.nextInt(); 14 continue; 15 } 16 else 17 isNum = true; 18 } 19 //定义一个随机数对象来取随机值 20 Random ra = new Random(); 21 int r = ra.nextInt(2); 22 switch (r) 23 { 24 case 2: 25 if(num == 0) 26 System.out.println("石头,你出"+a[num]+"你赢了"); 27 if(num == 1) 28 System.out.println("石头,你出"+a[num]+"你输了"); 29 if(num == 2) 30 System.out.println("石头,你出"+a[num]+"你们都没赢"); 31 break; 32 case 1: 33 if(num == 0) 34 System.out.println("剪刀,你出"+a[num]+"你输了"); 35 if(num == 1) 36 System.out.println("剪刀,你出"+a[num]+"你们都没赢"); 37 if(num == 2) 38 System.out.println("剪刀,你出"+a[num]+"你赢了"); 39 break; 40 case 0: 41 if(num == 0) 42 System.out.println("布,你出"+a[num]+"你们都没赢"); 43 if(num == 1) 44 System.out.println("布,你出"+a[num]+"你赢了"); 45 if(num == 2) 46 System.out.println("布,你出"+a[num]+"你输了"); 47 break; 48 } 49 } 50 }
10 这题要求数字中含有7或者是7的倍数,限制了数字的位数,可以将每一位都单独拿出来判断,只要满足其中一个条件就可以
1 import java.util.*; 2 public class ch03_10 { 3 public static void main(String[] args) { 4 int sum = 0; 5 int count = 0; 6 //用for循环把每一个数字的个位十位百位千位都计算出来存放在对应变量里 7 for (int i = 0; i <= 1000; i++) { 8 int ge = i % 10; 9 int shi = i / 10 % 10; 10 int bai = i / 100 % 10; 11 int qian = i / 1000 % 10; 12 //用if条件语句进行判断,满足条件记个数和总和 13 if(i != 0) { 14 if (i % 7 == 0 | ge == 7 | shi == 7 | bai == 7 | qian == 7) { 15 count++; 16 sum += i; 17 } 18 } 19 } 20 System.out.println("个数:"+count+",总和:"+sum); 21 } 22 }
11 注意控制数字的范围和每行显示10个数字
1 import java.util.*; 2 public class ch03_11 { 3 public static void main(String[] args) { 4 int count = 0; 5 //用for循环控制数字范围在100~1000之间 6 for (int i = 100; i < 1000; i++) { 7 //用if条件语句找出满足条件的数字 8 if(i % 5 == 0 && i % 6 == 0) { 9 System.out.print(i+" "); 10 count++; 11 //每十个换一行显示 12 if(count % 10 == 0) 13 System.out.println(); 14 } 15 } 16 } 17 }
12 计算各位数字之和可以从最低位开始加,逐个舍去,直到加完为止
1 public class ch03_12 { 2 public static void main(String[] args) { 3 Scanner in = new Scanner(System.in); 4 System.out.println("请输入一个整数:"); 5 long n = in.nextLong(); 6 long sum = 0; 7 long[] a = new long[100]; 8 //当最高位加完时退出循环 9 while(n>0) { 10 for (int i = 0; i < 100; i++) { 11 //求出最低位 12 a[i] = n % 10; 13 //将最低位舍去,上一位代替最低位 14 n = n / 10; 15 sum += a[i]; 16 } 17 } 18 System.out.println("各位数字之和为:"+sum); 19 } 20 }
13 十进制转化二进制的方法是:不断除以2直到商为0,最后得到的二进制数为从下往上得到的数字
1 public class ch03_13 { 2 public static void main(String[] args) { 3 Scanner in = new Scanner(System.in); 4 System.out.println("输入一个十进制数:"); 5 int n = in.nextInt(); 6 int[] a = new int[10]; 7 int count = 0; 8 while (n>0){ 9 for (int i = 0; i < 10; i++) { 10 //取余数 11 a[i] = n % 2; 12 //得到下一次的整数 13 n = n / 2; 14 count++; 15 //商为0时退出循环 16 if(n == 0) 17 break; 18 } 19 } 20 System.out.println("转化的二进制数为:"); 21 //从下往上输出得到的数即转化的二进制数 22 for (int i = count - 1; i >= 0; i--) 23 System.out.print(a[i]); 24 } 25 }
14 找到规律用循环进行计算非常简便
1 import java.util.*; 2 public class ch03_14 { 3 public static void main(String[] args) { 4 double sum = 0.0; 5 for (double i = 1.0; i < 99; i+=2) 6 sum += i / (i+2); 7 System.out.println(String.format("%.2f",sum)); 8 } 9 }
15 经典的鸡兔同笼问题,使用循环解决
1 import java.util.*; 2 public class ch03_15 { 3 public static void main(String[] args) { 4 int ra = 0; 5 int chi = 0; 6 //控制鸡和兔的个数 7 for (ra = 0; ra <= 40; ra++) { 8 chi = 40 - ra; 9 //判断腿的个数是否满足 10 if(ra * 4 + chi * 2 == 100) 11 System.out.println("鸡的个数是:"+chi+";兔的个数是:"+ra); 12 } 13 } 14 }
16 水仙花数问题也是用循环解决,逐个寻找到满足条件的数字
水仙花数:一个三位数的各位数字的立方和等于这个三位数本身
1 import java.util.*; 2 public class ch03_16 { 3 public static void main(String[] args) { 4 //水仙花数是三位数 5 for (int i = 100; i < 1000; i++) { 6 int ge = i % 10; 7 int shi = i / 10 % 10; 8 int bai = i / 100 % 10; 9 //判断数的各位数字的立方和是否等于这个数 10 if(Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3) == i) 11 System.out.println(i); 12 } 13 } 14 }
17 最大公约数和最小公倍数都是用短除法计算
1 import java.util.Scanner; 2 3 public class test { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 System.out.print("输入第一个数:"); 7 int m = input.nextInt(); 8 System.out.print("输入第二个数:"); 9 int n = input.nextInt(); 10 int k = 2; 11 int gac=0; 12 int min = 0; 13 int i=1; 14 boolean flag = true; 15 while (k <= m && k <= n) { 16 if (m % k == 0 && n % k == 0) 17 gac = k; 18 k++; 19 } 20 21 while (flag) 22 { 23 if(i % m == 0 && i % n == 0) 24 { 25 min = i; 26 flag = false; 27 } 28 i++; 29 } 30 System.out.println(m+"和"+n+"最大公约数为:"+gac+"最小公倍数为:"+min); 31 } 32 }
18 求1~1000的完全数
完全数:一个数的所有因子(包含1但不包含这个数本身)之和等于这个数
1 import java.util.*; 2 public class ch03_18 { 3 public static void main(String[] args) { 4 //控制在1~1000以内的数 5 for (int i = 1; i <= 1000; i++) { 6 int sum = 0; 7 //判断从1到它本身的所有数字有哪些是它的因子 8 for (int j = 1; j < i; j++) { 9 //是这个数的因子则加到sum中 10 if(i % j == 0) 11 sum += j; 12 } 13 //判断因子之和是否等于它本身 14 if(sum == i) 15 System.out.println(i); 16 } 17 } 18 }
19 先找出因子,再判断是否是素数,如果不是,就舍去,继续进行下一个数的判断
1 import java.util.Scanner; 2 3 public class test { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int a = input.nextInt(); 7 int t; 8 // 循环是以a!=1结束 9 while (a != 1) { 10 for (int i = 2; i <= a; i++) { 11 // 每次能被整除就输出 12 if (a % i == 0) { 13 System.out.print(i + " "); 14 a /= i; 15 break; 16 } 17 } 18 } 19 20 } 21 }
20 用题目给的公式计算,找出规律用循环写出方法,注意控制正负符号
1 import java.util.*; 2 public class Helloworld { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 long n = in.nextLong(); 6 double pi = 0; 7 double m = 0.0; 8 int count = 0; 9 //用count和-1来控制符号,分母逐个加2,分子始终为1 10 for (double i = 1; i < (2 * n - 1); i+=2) { 11 m = m + Math.pow(-1,count) * (1 / i); 12 count++; 13 } 14 //得出Π的近似值 15 pi = 4 * m; 16 System.out.println(pi); 17 } 18 }
原文地址:https://www.cnblogs.com/PerZhu/p/10867451.html