/*
* 2022年11月12日
* SpraingBoy
*/
1、求任意输入的10个数的和。
public static int Sum(int[] arr, int len){ // 数组作为参数
int sum = 0;
for (int i = 0; i < len; ++i){
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in); //创建输入对象
int[] arr = new int[10];
System.out.println("please input ten numbers:");
for (int i = 0; i < 10; ++i){
arr[i] = in.nextInt();
}
int result = Sum(arr,arr.length);
System.out.println("sum = "+result);
}
2、获取三个整数中的最大值(用三元运算符)
class MaxNumber {
public static int Max(int num1, int num2, int num3){
//1、num1 > num2结果为true, 继续判断num1是否大于num3,如果大于,返回num1,否则返回num3
//2、num1 > num2结果为false,继续判断num2 是否大于num3,如果大于,返回num2,否则返回num3
int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3);
return max;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入三个整数:");
int num1 = in.nextInt();
int num2 = in.nextInt();
int num3 = in.nextInt();
int max = Max(num1,num2,num3);
System.out.println("三个整数中最大的数是:"+max);
}
}
3、键盘录入月份的值,输出对应的季节。
class Season {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int month;
int judge = 1;
while (judge == 1){
System.out.println("please input the month:");
month = in.nextInt();
if (month <= 12 && month >= 1){
if (month == 2 || month == 3 || month == 4){
System.out.println("you are in spring");
} else if (month == 5 || month == 6 || month == 7){
System.out.println("you are in sumer");
} else if (month == 8 || month == 9 ||month == 10){
System.out.println("you are in autumn");
} else {
System.out.println("you are in winter");
}
break;
}
else {
System.out.println("you input an illegal number, please input again");
}
}
}
}
4、输入年份和月份,输出该年月的天数。
class Days {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int year, month;
boolean b = false; // 用于判断该年份是否为闰年
System.out.println("please input the year and month:");
year = in.nextInt();
month = in.nextInt();
//判断闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
b = true;
}
if (b == true){
if (month == 2){
System.out.println(year+"年"+month+"月有29天");
} else if (month == 4 || month == 6 || month == 9 || month == 11){
System.out.println(year+"年"+month+"月有30天");
} else {
System.out.println(year+"年"+month+"月有31天");
}
}
else {
if (month == 2){
System.out.println(year+"年"+month+"月有28天");
} else if (month == 4 || month == 6 || month == 9 || month == 11){
System.out.println(year+"年"+month+"月有30天");
} else {
System.out.println(year+"年"+month+"月有31天");
}
}
}
}
5、
出租车计费问题。
• 开封市的出租车计费方式为:起步2公里内5元,2公里以上每公里收费1.3元,9公里以上每公里收费2元,燃油附加费1元。
• 编写程序,输入公里数,计算出所需的出租车费用。
class Taxi {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double money, distance;
System.out.println("please input the distance:");
distance = in.nextDouble();
if (distance <= 2){
money = 5;
} else if (distance > 2 && distance <= 9){
money = 5 + (distance - 2) * 1.3;
} else {
money = 5 + 7*1.3 + (distance - 9)*2 + 1;
}
System.out.println("you should pay:"+money);
}
}
6、分别用do-while和for循环计算1+1/2!-1/3!+1/4!-1/5!…的前20项之和。
class Calculate {
public static void main(String[] args) {
float sum1 = 0;
//for循环实现
for (int i = 1; i <= 20; ++i){
int m = 1 ;
for (int j = 1; j <= i; ++j){
m *= j;
}
sum1 += 1.0/m;
}
System.out.println("sum1 = "+sum1);
float sum2 = 0;
float n = 1;
int j = 1;
//do-while实现
do {
n *= j;
j++;
sum2 += 1.0/n;
} while (j <= 20);
System.out.println("sum2 = "+sum2);
}
}
7、求1000以内的完全数(一个数等于它的因子之和称为完全数)。
class CompeleteNumber {
public static void main(String[] args) {
for (int i = 1; i <= 1000; ++i){
int sum = 0;
for (int j = 1; j < i; ++j){
if (i % j == 0){
sum += j;
}
}
if (sum == i){
System.out.println(i);
}
}
}
}
8、微信中的一个问题:
一筐鸡蛋,1个1个拿,正好拿完。 2个2个拿,还剩1个。3个3个拿,正好拿完。 4个4个拿,还剩1个。5个5个拿,还差1个。 6个6个拿,还剩3个。 7个7个拿,正好拿完。 8个8个拿,还剩1个。 9个9个拿,正好拿完。
问筐里最少有多少鸡蛋?
class Eggs {
public static void main(String[] args) {
for (int i = 1; ; ++i){
if (i % 2 == 1 && i % 3 == 0 && i % 4 == 1 && i % 5 == 4 && i % 6 == 3 && i % 7 == 0 && i % 8 == 1 && i % 9 == 0){
System.out.println("Eggs at least "+i);
break;
}
i++;
}
}
}
9、
求六边形面积,六边形面积可以通过下面公式计算(s是边长):
public class GetSquare {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double s = 0;
final double p = 3.1415926; //final修饰变量相当于常量
double result = 0;
System.out.println("请输入你要求的六边形的边长:");
s = in.nextDouble();
result = (6 * s * s) / (4 * Math.tan(p / 6));
System.out.println("六边形的面积是:"+ result);
}
}
10、实现会员注册,要求用户名长度不小于3,密码长度不小于6,若不满足需有提示信息,提示输入有误;注册时两次输入密码必须相同(字符串)。
public class LoginVIP {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String name;
String password1, password2;
boolean isLegal = false;
boolean isSame = false;
VIP vip = new VIP();
while(!isLegal){
System.out.println("请设置长度不小于3的用户名:");
name = in.nextLine();
if (name.length() >= 3){
isLegal = true;
vip.username = name;
} else {
System.out.println("你的用户名不满足要求");
}
}
isLegal = false;
while(!isLegal){
System.out.println("请输入长度不小于6的密码:");
password1 = in.nextLine();
if (password1.length() >= 6){
isLegal = true;
while(!isSame){
System.out.println("请再次确认密码:");
password2 = in.nextLine();
if (password1.equals(password2)){
isSame = true;
vip.setPassword(password1);
} else {
System.out.println("两次输入不一致,请重新输入确认密码");
}
}
} else {
System.out.println("你的密码格式不满足要求,请重新输入");
}
}
System.out.println("用户注册成功");
System.out.println("用户名:"+vip.username+" "+"密码:"+vip.getPassword());
}
}
class VIP {
public String username;
private String password; //密码设为私有属性,需要提供get和set方法
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
11、找出两个分教最高的学生)编写程序,提示输入学生的个数、每个学生的名字及其分数,最后显示获得最高分的学生和第二高分的学生。
public class ScoreBest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
System.out.println("请输入学生的个数:");
number = in.nextInt();
String[] name = new String[number];
int[] score = new int[number];
for (int i = 0; i < number; ++i){
System.out.println("请输入学生的姓名:");
name[i] = in.next();
System.out.println("请输入学生的分数:");
score[i] = in.nextInt();
}
Arrays.sort(score);//数组的排序方法
System.out.println("最高分:");
System.out.println(name[number-1]+" "+score[number-1]);
System.out.println("第二高分:");
System.out.println(name[number-2]+" "+score[number-2]);
}
}
12、定义一维数组并初始化,通过键盘任意输入一个数,查找该数是否存在(结果返回下标值)。
public class SearchNum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[] {1, 6, 3, 9 , 10};
System.out.println("请输入你要查找的数字:");
int num, index = 0;
num = in.nextInt();
for (int i = 0; i < arr.length; ++i){
if (arr[i] == num){
index = i;
}
}
if (index == 0){
System.out.println("该数组中不存在该数字");
} else {
System.out.println("该数组中存在该数字,下标为:"+ index);
}
}
}
13、编写一个程序,将二维数组a转置后存入数组b(所谓转置就是行列互换)
public class TransposeArr {
public static void main(String[] args) {
int[][] arr = new int[][] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] brr = new int[3][3];
for (int i = 0; i < arr.length; ++i){
for (int j = 0; j < arr[i].length; ++j){
brr[i][j] = arr[j][i];
}
}
System.out.println("转置之前的数组为:");
for (int i = 0; i < arr.length; ++i){
for (int j = 0; j < arr[i].length; ++j){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("转置之后的数组为:");
for (int i = 0; i < brr.length; ++i){
for (int j = 0; j < brr[i].length; ++j){
System.out.print(brr[i][j]+" ");
}
System.out.println();
}
}
}