Java每日一练(4)
程序员文章站
2022-03-19 11:38:01
...
1.求1~100之间不能被3整除的数之和
package com.etc.four.day;
public class Translation {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 3 != 0) {
sum = sum + i;
}
continue;
}
System.out.println(sum);
}
}
2.计算顾客比例
训练要点: for循环结构
需求说明: 商场对顾客的年龄层次进行调查;计算各层次的顾客比例
实现思路: 1、定义计数器变量 2、利用循环录入顾客年龄
难点指导: 循环条件和循环体
package com.etc.four.day;
import java.util.Scanner;
public class Proportion {
public static void main(String[] args) {
System.out.println("请输入顾客数量:");
double number = 0;
int age = 0;
double number2 = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
break;
} else {
System.out.println("请输入顾客数量:");
}
}
for (int i = 0; i < number; i++) {
System.out.println("请输入第" + (i + 1) + "顾客的年龄:");
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
age = input.nextInt();
break;
} else {
System.out.println("请输入第" + (i + 1) + "顾客的年龄:");
}
}
if (age < 30) {
number2 = number2 + 1;
}
}
System.out.println("30岁以下的比例是:" + (double) ((number2 / number) * 100) + "%");
System.out.println("30岁以下的比例是:" + (double) (((number - number2) / number) * 100) + "%");
}
}
3.1~10之间的整数相加,得到累加值大于20的当前数
package com.etc.four.day;
public class Accumulation {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i <= 10; i++) {
if (sum < 20) {
sum = sum + i;
}
continue;
}
System.out.println(sum);
}
}
提示 :使用循环进行累加,从1到10 ;判断累加值是否大于20 ;如果大于20,则跳出循环,并打印当前值
4.求1~10之间的所有偶数和
package com.etc.four.day;
import java.util.concurrent.locks.Condition;
public class Even {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
sum = sum + i;
}
continue;
}
System.out.println(sum);
}
}
5.循环录入会员信息
训练要点: for循环结构 continue语句
需求说明: 循环录入3位会员的信息;会员号合法,显示录入信息;否则显示录入失败
实现思路: 循环录入3位会员信息;会员号无效,利用continue;实现程序跳转
难点指导: continue的使用
package com.etc.four.day;
import java.util.Scanner;
public class Member {
public static void main(String[] args) {
System.out.println("MyShopping管理系统>客户信息管理>添加客户信息");
int number = 0;
int month = 0;
int day = 0;
int points = 0;
int sum = 3;
while (true) {
System.out.println("请输入添加会员数目:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
sum = input.nextInt();
break;
}
}
for (int i = 0; i < sum; i++) {
while (true) {
System.out.println("请输入会员号(<4位整数>):");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
break;
}
}
while (true) {
System.out.println("请输入会员出生月份::");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
month = input.nextInt();
break;
}
if (0 < month && month <= 12) {
break;
}
}
while (true) {
System.out.println("请输入会员出生日期:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
day = input.nextInt();
break;
}
if (0 < day && day <= 31) {
break;
}
}
while (true) {
System.out.println("请输入会员积分:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
points = input.nextInt();
break;
}
}
if (999 < number && number < 10000) {
System.out.println("您录入的信息是:");
System.out.println(number + "\t" + month + "/" + day + "\t" + points);
} else {
System.out.println("客户号" + number + "是无效会员号!");
System.out.println("录入信息失败");
}
if (i == sum) {
continue;
}
}
}
}
6.验证用户登录信息
需求说明: 用户登录验证。验证次数最多3次
package com.etc.four.day;
import java.util.Scanner;
public class Verify {
public static void main(String[] args) {
String name = new String("jim");
String password = new String("123456");
int[] sum = new int[3];
String name2 = "";
String password2 = "";
for (int i = 1; i <= 3; i++) {
while (true) {
System.out.println("请输入用户名:");
Scanner input = new Scanner(System.in);
if (input.hasNext()) {
name2 = input.next();
break;
}
}
while (true) {
System.out.println("请输入密码:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
password2 = input.next();
break;
}
}
if (name.equals(name2) && password.equals(password2)) {
System.out.println("欢迎登陆Shopping系统:");
break;
} else {
System.out.println("您还有" + (3 - i) + "次机会");
if ((3 - i) == 0) {
System.out.println("对不起,您3次输入错误!!!");
break;
}
}
}
}
}
7.显示商品名称
训练要点: 数组的使用
需求说明: 在控制台显示5件特价商品名称
实现思路: 1、创建数组存储商品名称 2、遍历数组输出商品名称
package com.etc.four.day;
public class Show {
public static void main(String[] args) {
System.out.println("本次活动特价商品有:");
String[] name = new String[] { "Nike背包", "Adidas运动衫", "李宁运动鞋", "Kappa外套", "361°腰包" };
for (int i = 0; i < name.length; i++) {
System.out.println(name[i]);
}
}
}
8.购物金额结算
需求说明: 以表格的形式输出5笔购物金额及总金额
package com.etc.four.day;
import java.util.Arrays;
import java.util.Scanner;
public class Money {
public static void main(String[] args) {
int number = 0;
double number2 = 0;
int[] money = new int[999999];
while (true) {
System.out.println("请输入购物次数:");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
break;
}
}
for (int i = 0; i < number; i++) {
while (true) {
System.out.println("请输入第" + (i + 1) + "笔购物金额");
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
money[i] = input.nextInt();
number2 = number2 + money[i];
break;
}
}
}
System.out.println("序列" + "\t" + "金额(元)");
for (int i = 0; i < number; i++) {
System.out.println((i + 1) + "\t" + money[i]);
}
System.out.println("总金额:" + "\t" + number2);
}
}
调用的类
package com.etc.test.util;
import java.util.Scanner;
public class JudgeNumber {
public int judge() {
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
break;
} else {
System.out.print("请输入数字:");
}
}
return number;
}
public int odd() {
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
} else {
System.out.print("请输入数字:");
}
if (number % 2 == 1) {
break;
} else {
System.out.print("请输入奇数:");
}
}
return number;
}
public String name(){
String name="";
while(true){
Scanner input =new Scanner(System.in);
if(input.hasNext()){
name =input.next();
break;
}
}
return name;
}
}
上一篇: 表单学习及练习
下一篇: 抽奖程序(python)