1  本题水题,记住要知道输入格式即可

 import java.util.Scanner;

 public class test {
public static void main(String[] args) {
// 从键盘中读取数据
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if(a%2==0)
System.out.println("这个数是偶数");
else
System.out.println("这个数是奇数");
}
}

2  本题水题,知道闰年的判断条件即可

 import java.util.*;
public class ch03_2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
//判断闰年的方法:能被4整除,但不能被100整除;能被400整除
//由2个判断条件组成,满足一个即可,中间使用或连接
if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0))
System.out.println(a+"是闰年");
else
System.out.println(a+"不是闰年");
}
}

3  这一题需要进行数值的替换,用if条件语句,逐个进行比较,将最大值和最小值储存在变量max和min中

全部比较完毕就可以得到最大值和最小值

 import java.util.*;
public class ch03_3 {
public static void main(String[] args) {
int a[] = new int[4];
//给最大值和最小值赋初值为第一个数
int max = a[0];
int min = a[0];
Scanner in = new Scanner(System.in);
System.out.println("输入四个整数:");
for (int i = 0; i < 4; i++) {
int j = in.nextInt();
a[i] = j;
if(a[i] > max)
max = a[i];
if(a[i] < min)
min = a[i];
}
System.out.println("最大值是:"+max+"最小值是:"+min);
}
}

4  本题水题,知道判别式,和求根的方式即可

 import java.util.Scanner;
import static java.lang.StrictMath.sqrt; public class ch03_4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入a,b,c");
double a = in.nextDouble();
double b = in.nextDouble();
double c = in.nextDouble();
double x1,x2;
double x;
//x为一元二次方程的判别式
x = b * b - 4 * a * c;
x1 = (-b + Math.sqrt(x)) / (2 * a);
x2 = (-b - Math.sqrt(x)) / (2 * a);
if(x > 0)//方程有两个实数根{
System.out.println("方程有两个实数根");
System.out.println("结果为:"+x1+""+x2);
}
else if(x == 0)//方程有一个实数根{
System.out.println("方程有一个实数根");
System.out.println("结果为:"+x1);
}
else//方程没有实数根
System.out.println("方程没有实数根");
}
}

5  按照题目要求用switch语句,不要忘记case语句要加入break退出

 import java.util.*;
public class ch03_5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个成绩:");
Integer sco = in.nextInt();
switch (sco / 10){
case 10: case 9:
System.out.println("优秀");
break;
case 8: case 7:
System.out.println("良好");
break;
case 6:
System.out.println("及格");
break;
case 5:
System.out.println("不及格");
break;
default:
System.out.println("差");
break;
}
}
}

6  这道题和第三题一样,使用循环进行比较选择出最大值最小值即可

 import java.util.Scanner;
public class ch03_6 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a[] = new int[10];
int max = a[0];
int min = a[0];
System.out.println("输入十个数字:");
for (int i = 0; i < 10; i++) {
Integer input = in.nextInt();
a[i] = input;
if(a[i]>max)
max = a[i];
if(a[i]<min)
min = a[i];
}
System.out.println("最大值是"+max+"最小值是:"+min);
}
}

7  这题是日期,注意区分闰年二月和平年二月的天数

 public class ch03_7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入年和月");
int year = in.nextInt();
int month = in.nextInt();
int day;
switch (month)
{
//31天的月份
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
day = 31;
System.out.println(year+"年"+month+"月有"+day+"天");
break;
//30天的月份
case 4: case 6: case 9: case 11:
day = 30;
System.out.println(year+"年"+month+"月有"+day+"天");
break;
//将二月单独拿出来判断是否是闰年
case 2:
//是闰年有29天
if((year % 4 == 0) & (year % 100 != 0) | (year % 400 == 0)){
day = 29;
System.out.println(year+"年"+month+"月有"+day+"天");
break;
}
//不是闰年只有28天
else{
day = 28;
System.out.println(year+"年"+month+"月有"+day+"天");
break;
}
} }
}

8  这一题主要是要注意生肖的位置,调整好之后就可以确保答案准确

 import java.util.*;
public class ch03_8 {
public static void main(String[] args) {
while(true) {
//存放生肖信息的数组,需要经过自己调试得出顺序
String[] ShengXiao = new String[]{
"猴(monkey)", "鸡(rooster)",
"狗(dog)", "猪(pig)",
"鼠(rat)", "牛(ox)",
"虎(tiger)", "兔(rabbit)",
"龙(dragon)", "蛇(snake)",
"马(horse)", "羊(sheep)"};
Scanner in = new Scanner(System.in);
System.out.println("输入年份");
int year = in.nextInt();
int a = year % 12;
System.out.println(year + "年属" + ShengXiao[a]);
}
}
}

9  这一题额外添加了一个判断是否输入是否出错的语句,随机数使用Random方法

 import java.util.*;
public class ch03_9 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("你出什么?(石头(2)、剪刀(1)、布(0))");
int num = in.nextInt();
String[] a = new String[] {"布","剪刀","石头"};
boolean isNum = false;
//安全性检测,出现输入错误要求重新输入
while(!isNum){
if(num > 2 | num < 0){
System.out.println("输入错误请重新输入:");
num = in.nextInt();
continue;
}
else
isNum = true;
}
//定义一个随机数对象来取随机值
Random ra = new Random();
int r = ra.nextInt(2);
switch (r)
{
case 2:
if(num == 0)
System.out.println("石头,你出"+a[num]+"你赢了");
if(num == 1)
System.out.println("石头,你出"+a[num]+"你输了");
if(num == 2)
System.out.println("石头,你出"+a[num]+"你们都没赢");
break;
case 1:
if(num == 0)
System.out.println("剪刀,你出"+a[num]+"你输了");
if(num == 1)
System.out.println("剪刀,你出"+a[num]+"你们都没赢");
if(num == 2)
System.out.println("剪刀,你出"+a[num]+"你赢了");
break;
case 0:
if(num == 0)
System.out.println("布,你出"+a[num]+"你们都没赢");
if(num == 1)
System.out.println("布,你出"+a[num]+"你赢了");
if(num == 2)
System.out.println("布,你出"+a[num]+"你输了");
break;
}
}
}

10  这题要求数字中含有7或者是7的倍数,限制了数字的位数,可以将每一位都单独拿出来判断,只要满足其中一个条件就可以

 import java.util.*;
public class ch03_10 {
public static void main(String[] args) {
int sum = 0;
int count = 0;
//用for循环把每一个数字的个位十位百位千位都计算出来存放在对应变量里
for (int i = 0; i <= 1000; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
int qian = i / 1000 % 10;
//用if条件语句进行判断,满足条件记个数和总和
if(i != 0) {
if (i % 7 == 0 | ge == 7 | shi == 7 | bai == 7 | qian == 7) {
count++;
sum += i;
}
}
}
System.out.println("个数:"+count+",总和:"+sum);
}
}

11  注意控制数字的范围和每行显示10个数字

 import java.util.*;
public class ch03_11 {
public static void main(String[] args) {
int count = 0;
//用for循环控制数字范围在100~1000之间
for (int i = 100; i < 1000; i++) {
//用if条件语句找出满足条件的数字
if(i % 5 == 0 && i % 6 == 0) {
System.out.print(i+" ");
count++;
//每十个换一行显示
if(count % 10 == 0)
System.out.println();
}
}
}
}

12  计算各位数字之和可以从最低位开始加,逐个舍去,直到加完为止

 public class ch03_12 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入一个整数:");
long n = in.nextLong();
long sum = 0;
long[] a = new long[100];
//当最高位加完时退出循环
while(n>0) {
for (int i = 0; i < 100; i++) {
//求出最低位
a[i] = n % 10;
//将最低位舍去,上一位代替最低位
n = n / 10;
sum += a[i];
}
}
System.out.println("各位数字之和为:"+sum);
}
}

13  十进制转化二进制的方法是:不断除以2直到商为0,最后得到的二进制数为从下往上得到的数字

 public class ch03_13 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("输入一个十进制数:");
int n = in.nextInt();
int[] a = new int[10];
int count = 0;
while (n>0){
for (int i = 0; i < 10; i++) {
//取余数
a[i] = n % 2;
//得到下一次的整数
n = n / 2;
count++;
//商为0时退出循环
if(n == 0)
break;
}
}
System.out.println("转化的二进制数为:");
//从下往上输出得到的数即转化的二进制数
for (int i = count - 1; i >= 0; i--)
System.out.print(a[i]);
}
}

14  找到规律用循环进行计算非常简便

 import java.util.*;
public class ch03_14 {
public static void main(String[] args) {
double sum = 0.0;
for (double i = 1.0; i < 99; i+=2)
sum += i / (i+2);
System.out.println(String.format("%.2f",sum));
}
}

15  经典的鸡兔同笼问题,使用循环解决

 import java.util.*;
public class ch03_15 {
public static void main(String[] args) {
int ra = 0;
int chi = 0;
//控制鸡和兔的个数
for (ra = 0; ra <= 40; ra++) {
chi = 40 - ra;
//判断腿的个数是否满足
if(ra * 4 + chi * 2 == 100)
System.out.println("鸡的个数是:"+chi+";兔的个数是:"+ra);
}
}
}

16  水仙花数问题也是用循环解决,逐个寻找到满足条件的数字

水仙花数:一个三位数的各位数字的立方和等于这个三位数本身

 import java.util.*;
public class ch03_16 {
public static void main(String[] args) {
//水仙花数是三位数
for (int i = 100; i < 1000; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
//判断数的各位数字的立方和是否等于这个数
if(Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3) == i)
System.out.println(i);
}
}
}

17  最大公约数和最小公倍数都是用短除法计算

 import java.util.Scanner;

 public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("输入第一个数:");
int m = input.nextInt();
System.out.print("输入第二个数:");
int n = input.nextInt();
int k = 2;
int gac=0;
int min = 0;
int i=1;
boolean flag = true;
while (k <= m && k <= n) {
if (m % k == 0 && n % k == 0)
gac = k;
k++;
} while (flag)
{
if(i % m == 0 && i % n == 0)
{
min = i;
flag = false;
}
i++;
}
System.out.println(m+"和"+n+"最大公约数为:"+gac+"最小公倍数为:"+min);
}
}

18  求1~1000的完全数

完全数:一个数的所有因子(包含1但不包含这个数本身)之和等于这个数

 import java.util.*;
public class ch03_18 {
public static void main(String[] args) {
//控制在1~1000以内的数
for (int i = 1; i <= 1000; i++) {
int sum = 0;
//判断从1到它本身的所有数字有哪些是它的因子
for (int j = 1; j < i; j++) {
//是这个数的因子则加到sum中
if(i % j == 0)
sum += j;
}
//判断因子之和是否等于它本身
if(sum == i)
System.out.println(i);
}
}
}

19  先找出因子,再判断是否是素数,如果不是,就舍去,继续进行下一个数的判断

 import java.util.Scanner;

 public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
int t;
// 循环是以a!=1结束
while (a != 1) {
for (int i = 2; i <= a; i++) {
// 每次能被整除就输出
if (a % i == 0) {
System.out.print(i + " ");
a /= i;
break;
}
}
} }
}

20  用题目给的公式计算,找出规律用循环写出方法,注意控制正负符号

 import java.util.*;
public class Helloworld {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
double pi = 0;
double m = 0.0;
int count = 0;
//用count和-1来控制符号,分母逐个加2,分子始终为1
for (double i = 1; i < (2 * n - 1); i+=2) {
m = m + Math.pow(-1,count) * (1 / i);
count++;
}
//得出Π的近似值
pi = 4 * m;
System.out.println(pi);
}
}
05-18 20:36