30天学会JAVA—练习题(2021韩顺平)——Day2
1. 打印1-100
public static void main(String[] args) {
for(int i = 1 ; i <= 100 ;i++){
System.out.println(i);
}
}
2. 计算1-100的和
public static void main(String[] args) {
int sum = 0;
for(int i = 1 ; i <= 100 ;i++){
sum += i;
}
System.out.println("1-100的和为:" + sum);
}
3. 统计1-200之间能被5整除,但不能被3整除的个数
public static void main(String[] args) {
int count = 0;
for(int i = 1 ; i <= 200 ;i++){
if((i % 5 == 0) &&(i % 3 != 0)){
count++;
}
}
System.out.println(count);
}
}
4.
- 统计3个班成绩情况,每个班有5名同学,求出各个班的平均分和所有班级的平均分【学生成绩从键盘输入】
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double totalScore = 0;
for(int i = 1; i <= 3; i++){
//班级
double sum = 0;
for(int j = 1; j <= 5; j++){
//学生
System.out.println("请输入第" + i + "个班级的第" + j +"个学生的成绩");
double score = sc.nextDouble();
sum += score;
System.out.println("成绩为" + score);
}
System.out.println("总成绩为:" + sum + "平均分为:" + (sum/5));
totalScore += sum;
}
System.out.println("总成绩为:" + totalScore + "总平均分为:" + (totalScore/15));
sc.close();
}
5. 统计三个班及格人数,每个班有5名同学。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int totalCount = 0;
for(int i = 1; i <= 3; i++){
//班级
int count = 0;
for(int j = 1; j <= 5; j++){
//学生
System.out.println("请输入第" + i + "个班级的第" + j +"个学生的成绩");
double score = sc.nextDouble();
System.out.println("成绩为" + score);
if(score >= 60){
count++;
}
}
System.out.println("该班及格人数为:" + count);
totalCount += count;
}
System.out.println("三个班及格人数为:" + totalCount);
sc.close();
}
6. 打印金字塔
- (接收一个整数,表示层数,打印出金字塔)
半个金字塔
public static void main(String[] args) {
System.out.println("请输入金字塔层数:");
Scanner sc = new Scanner(System.in);
int floor = sc.nextInt();//接收金字塔层数
for(int i = 1; i <= floor ; i++){
for(int j = 1; j <= i ; j++){
System.out.print("*");
}
System.out.println();
}
sc.close();
}
整个金字塔
public static void main(String[] args) {
System.out.println("请输入金字塔层数:");
Scanner sc = new Scanner(System.in);
int floor = sc.nextInt();//接收金字塔层数
for(int i = 1; i <= floor ; i++){
//层数
//空格输入(字符前面):空格 = 总层数 - 当前层
for(int k = 1; k <= floor -i; k++){
System.out.print(" ");
}
//字符输入
for(int j = 1; j <= 2*i -1 ; j++){
System.out.print("*");
}
System.out.println("");
}
sc.close();
}
空心金字塔
public static void main(String[] args) {
System.out.println("请输入金字塔层数:");
Scanner sc = new Scanner(System.in);
int floor = sc.nextInt();//接收金字塔层数
for(int i = 1; i <= floor ; i++){
//层数
//空格输入(字符前面):空格 = 总层数 - 当前层
for(int k = 1; k <= floor -i; k++){
System.out.print(" ");
}
//字符输入
for(int j = 1; j <= 2*i -1 ; j++){
//除最后一行第一个位置和最后一个位置均是字符
//最后一行全是字符
if(j == 1 || j == 2*i -1 || i == floor){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println("");
}
sc.close();
}
7.
- 实现登录验证,有3次机会,如果用户名为“懒鱼”,密码“666”提示登陆成功,否则提示还有几次机会。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for(int i = 3; i > 0 ;){
System.out.println("请输入用户名:");
String userID = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
if("懒鱼".equals(userID) && "666".equals(password)){
System.out.println("登陆成功");
break;
}
i--;
System.out.println("输入错误,请重新输入,您还有" + i + "次机会");
}
sc.close();
}
8. 实现下列功能
某人有100元,每经过一次路口,需要交费,规则如下:
- 当现金 >= 50时,每次交5%;
- 当现金< 50时,每次交10;
计算该人可以经过多少次路口。
public static void main(String[] args) {
int count = 0;
int money = 100000;
while(money >= 50000){
money -= 0.05 * money;
count++;
}
while(money >= 1000 && money < 50000){
money -= 1000;
count++;
}
System.out.println("通过路口" + count + "次");
}
9. 打印1-100之间所有能被3整除的数
public static void main(String[] args) {
for(int i = 1; i <= 100 ;i++){
if(i % 3 == 0){
System.out.println(i);
}
}
}
10. 打印40-200之间所有的偶数
public static void main(String[] args) {
for(int i = 40; i <= 200 ;i++){
if(i % 2 == 0){
System.out.println(i);
}
}
}