学习java最重要的就是实际操作了,但是对于小白来说,自己想实现的又太难,实现不了,这里整理了几个也可以实现的简单练习题(大佬勿扰)快来看一下吧。
1、编写一个实现由1到k的平方和计算
package demo1.code1;
import java.util.Scanner;
public class Calculate_k {
public static int calculation(int k) {
int result = 0;
for (int i = 1; i <= k; i++) {
result = i * i + result;
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int result;
int num;
System.out.print(“输入一个整数:”);
num = sc.nextInt();
result = calculation(5);
System.out.println(result);
}
}
2.用Java描述一个三角形,提供计算三角形面积方法。三角形的三个边长从键盘输入。
package demo1.code1;
import java.util.Scanner;
public class Triangle {
private double a, b, c; //三角形的三条边的边长
public Triangle(double aa, double bb, double cc) {
a = aa;
b = bb;
c = cc;
}//构造函数
public double getArea() {
double p;
double s;
p = (a + b + c) / 2;
s = p * (p - a) * (p - b) * (p - c);
s = Math.sqrt(s);
return s;
}//计算三角形面积
public static void main(String[] args) {
double a, b, c;
double s;
Scanner sc = new Scanner(System.in);
System.out.print(“请输入三角形的三条边:”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
Triangle tri = new Triangle(a, b, c);
s = tri.getArea();
System.out.println(s);
}
}
3.假如今天是星期二,100天后将是星期几?
package demo1.code1;
public class CountDate {
public static void main(String[] args) {
int days = 100;
int d;
d = days % 7;
System.out.println(“假设今天是星期二”);
switch (d) {
case 0:
System.out.println(“今天的100天后是星期二”);
break;
case 1:
System.out.println(“今天的100天后是星期三”);
break;
case 2:
System.out.println(“今天的100天后是星期四”);
break;
case 3:
System.out.println(“今天的100天后是星期五”);
break;
case 4:
System.out.println(“今天的100天后是星期六”);
break;
case 5:
System.out.println(“今天的100天后是星期日”);
break;
case 6:
System.out.println(“今天的100天后是星期一”);
break;
}
}
}
4.人民币的面值有100元,50元,20元,10元,5元,1元,5角,1角,5分,1分。编写程序实现从键盘输入1万元以内的任意数,然后分解成不同数量的,不同面值的人民币?
package demo1.code1;
import java.util.Scanner;
public class RMBDivision {
public static void main(String[] args) {
double rmb;
int rmb_left;
int rmb_fj;
int hundred = 10000;
int fif = 5000;
int ten = 1000;
int fiv = 500;
int one = 100;
double fivej = 50;
double onej = 10;
double fivef = 5;
Scanner sc = new Scanner(System.in);
System.out.print(“请输入人民币数值(单位:元):”);
rmb = sc.nextDouble();
rmb_left = (int) rmb;
rmb = rmb - rmb_left;
rmb_left = rmb_left * 100; //将用户输入的人民币单位从元化作分
rmb = rmb * 100;
rmb_fj = (int) rmb;
hundred = rmb_left / hundred;
rmb_left = rmb_left - hundred * 10000;
fif = rmb_left / fif;
rmb_left = rmb_left - fif * 5000;
ten = rmb_left / ten;
rmb_left = rmb_left - ten * 1000;
fiv = rmb_left / fiv;
rmb_left = rmb_left - fiv * 500;
one = rmb_left / one;
rmb_left = rmb_left - one * 100;
fivej = rmb_fj / (int) fivej;
rmb_fj = rmb_fj - (int) fivej * 50;
onej = rmb_fj / (int) onej;
rmb_fj = rmb_fj - (int) onej * 10;
fivef = rmb_fj / (int) fivef;
rmb_fj = rmb_fj - (int) fivef * 5;
System.out.println(“该数值的人民币可分解为” + hundred + “张一百元,” + fif + “张五十元,” + ten + “张十元,” + fiv + “张五元,” +
one + “张一元,” + (int) fivej + “张五角,” + (int) onej + “张一角,” + (int) fivef + “张五分,” + rmb_fj + “张一分纸币。”);
}
已知三角形的底边和高尺寸分别是56厘米、120厘米,计算该三角形的面积?
package demo1.code1;
public class Triangle02 {
private double bottom, height; //三角形的底边和高
public Triangle02(double b, double h) {
bottom = b;
height = h;
}//构造函数
public double getArea() {
double s;
s = bottom * height / 2;
return s;
}
public static void main(String[] args) {
double result;
Triangle02 tri = new Triangle02(56, 120);
result = tri.getArea();
System.out.println(“三角形的面积为” + result + “平方厘米。”);
}
}
7.从键盘输入三个整数,分别计算它们的平均值、累加和、最大值和最小值。
package demo1.code1;
import java.util.Scanner;
public class ThreeNumCalculation {
public static int Sum(int a, int b, int c) {
int result;
result = a + b + c;
return result;
}
public static int Average(int a, int b, int c) {
int result;
result = (a + b + c) / 3;
return result;
}
public static int Max(int a, int b, int c) {
int max;
if (a > b) {
max = a;
} else {
max = b;
}
if (max > c) {
return max;
} else {
return c;
}
}
public static int Min(int a, int b, int c) {
int min;
if (a < b) {
min = a;
} else {
min = b;
}
if (min < c) {
return min;
} else {
return c;
}
}
public static void main(String[] args) {
int a, b, c;
int result;
Scanner sc = new Scanner(System.in);
System.out.println(“请输入三个整数:”);
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
result = Average(a, b, c);
System.out.println(“三个数的平均值为:” + result);
result = Sum(a, b, c);
System.out.println(“三个数的累加和为:” + result);
result = Max(a, b, c);
System.out.println(“三个数中的最大值为:” + result);
result = Min(a, b, c);
System.out.println(“三个数中的最小值为:” + result);
}
}
9.编写程序生成15个随机(int类型)整数?
package demo1.code1;
import java.util.Random;
public class FifteenRandomInt {
public static void main(String[] args) {
int num;
Random ran = new Random();
for (int i = 0; i < 15; i++) {
num = ran.nextInt(100);
System.out.print(num + " ");
}
}
}
可以试试这几个简单的小练习,如果有不懂的,或者还想了解一下其他的练习题可以加 java交流群934623944里面有很多一起讨论的小伙伴。