JAVA理解逻辑程序的书上全部重要的习题

时间:2021-07-16 12:48:20

今天随便翻翻看以前学过JAVA理解逻辑程序的书上全部练习,为了一些刚学的学弟学妹,所以呢就把这些作为共享了.

希望对初学的学弟学妹有所帮助!

 例子:升级“我行我素购物管理系统”,实现购物结算功能
代码: public class OneDay {
public static void main(String[] args) {
int shirtPrice=245;
int shoePrice=570;
int pADpRICE=320;
int ShoeNo=1;
int shirtNo=2;
int padNo=1;
double discount=0.8;
/*
* 计算消费总金额
*
* */
double finalPay=(shirtPrice*shirtNo+ shoePrice*ShoeNo+shoePrice+pADpRICE*padNo)*discount;
System.out.println("消费总金额:"+finalPay);
}
} 例子:升级“我行我素购物管理系统”,实现打印购物小票和计算积分功能
public class OneDay {
public static void main(String[] args) {
int shirtPrice=245;
int shoePrice=570;
int pADpRICE=320;
int ShoeNo=1;
int shirtNo=2;
int padNo=1;
double discount=0.8;
double finalPay=(shirtPrice*shirtNo+shoePrice*ShoeNo+shoePrice+pADpRICE*padNo)*discount;
double Sum=1800;
double SuM=Sum-finalPay;
//积分
int Point=(int)finalPay*3/100;
System.out.println("**********消费单***********");
System.out.println("购买物品\t单价\t个数\t金额");
System.out.println("T血\t¥245\t2\t¥490");
System.out.println("网球鞋\t¥570\t1\t¥570");
System.out.println("网球拍\t¥320\t1\t¥320\n");
System.out.println("折扣:\t"+discount);
System.out.println("消费总金额:"+finalPay);
System.out.println("实际交费\t¥"+Sum);
System.out.println("找钱\t¥"+SuM);
System.out.println("积分\t¥"+Point);
}
} //我行我素,幸运抽奖
public class OneDay {
public static void main(String[] args) {
int cusNO;//客户会员号
System.out.println("请输入四位会员卡号:");
Scanner input=new Scanner(System.in);//导包
cusNO=input.nextInt();
//获得 每位数字
int gewei=cusNO%10;
int shiwei=cusNO%10%10;
int baiwei=cusNO/100%10;
int qianwei=cusNO/1000;
int sum=gewei+shiwei+baiwei+qianwei;
System.out.println("会员卡号:"+cusNO+"各位之和:"+sum);
boolean isluck=sum>20;//boolean的判断为true或false
System.out.println("是幸运客户吗?"+isluck);
}
} //我行我素,判断商品判断商品折扣价格
public static void main(String[] args) {
int shirtPrice=245;
int shoePrice=570;
int padPrice=320;
//用户输入折扣
Scanner input=new Scanner(System.in);
System.out.println("请输入折扣:");
double discount=input.nextDouble();
//计算商品的享受价格
double shirtPriceDis=shirtPrice*discount;
double shoePriceDis=shoePrice*discount;
double padPriceDis=padPrice*discount;
//判断商品价格是否低于100
boolean shirtPricebool=shirtPriceDis<100;
boolean shoePricebool=shoePriceDis<100;
boolean padPricebool=padPriceDis<100;
System.out.println("T血折扣低于100吗?"+shirtPricebool);
System.out.println("网球鞋折扣低于100吗?"+shoePricebool);
System.out.println("网球拍折扣低于100吗?"+padPricebool); 小明左右手分别拿出两张牌
public static void main(String[] args) {
int num;
int num1;
System.out.println("输出互换前手中的纸牌 :"); Scanner input=new Scanner(System.in);
System.out.println("左手中的纸牌:\n");
num=input.nextInt();
System.out.println("输出右手的纸牌:");
num1=input.nextInt();
System.out.println("**************************************");
System.out.println("输出互换后手中的纸牌 :");
int temp;
temp=num;
num=num1;//
num1=temp;//
System.out.println("左手中的纸牌:"+num);
System.out.println("输出右手的纸牌:"+num1);
}
} double sheshi=0;
int num=0;//循环次始值
double huashi;
do{
num++;
sheshi+=20;
huashi=sheshi*9/5+32;
System.out.println("摄氏温度:"+sheshi+"度,华氏温度:"+huashi+"度");
}while(num<=9);//循环次条件
System.out.println("结束"); public static void main(String[] args) {
Scanner input=new Scanner(System.in);
/*
* 产生随机数
* */
int random=(int)(Math.random()*10);
System.out.println("************购物系统抽奖************\n");
System.out.println("请输入4位会员号:");
int cusNo=input.nextInt();
/*
* 获得百分位
* */
int baiwei=cusNo/100%10;
/*
* 判断该会员是否是幸运会员
* */
if (baiwei==random)
System.out.println(cusNo+"是幸运客户,获得笔记本一个"); }else {
System.out.println(cusNo+"号客户"+"谢谢");
}
}
public static void main(String[] args) { Scanner input=new Scanner(System.in);
System.out.println("请输入第一个整数");
int a=input.nextInt();
System.out.println("请输入第二个整数");
int b=input.nextInt();
System.out.println("请输入第三个整数");
int c=input.nextInt();
/*第一道排序*/
System.out.println("第一道排序");
if (a<b&&a<c) {
System.out.print(a);
System.out.print(b);
System.out.println(c);
}
/*第二道排序*/
System.out.println("第二道排序");
if (b<c) {
System.out.print(a);
System.out.print(b);
System.out.println(c);
}
System.out.println("每个变量的值");
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
public static void main(String[] args) {
int price = 5000; // 机票的原价
int month; // 出行的月份
int type; // 头等舱为1,经济舱为2
Scanner input = new Scanner(System.in);
System.out.println("请输入您出行的月份:1~12");
month = input.nextInt();
System.out.println("请问您选择头等舱还是经济舱?头等舱输入1,经济舱输入2");
type = input.nextInt(); switch(month/10){
case 1:
case 2:
case 3:
if (type == 1) // 头等舱
{
System.out.println("您的机票价格为:"+ price * 0.6);
}
else if (type == 2) // 经济舱
{
System.out.println("您的机票价格为:"+ price * 0.3);
}
break;
case 0:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if (type == 1) // 头等舱
{
System.out.println("您的机票价格为:"+price * 0.9);
}
else if (type == 2) // 经济舱
{
System.out.println("您的机票价格为:"+ price * 0.8);
}
break;
default:
System.out.println("请正确输入成绩!");
break;
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("*******************计算器****************");
int result=0;
char opt='*'; //定义操作符
System.out.println("请输入第一个操作数:");
if (input.hasNextInt() == true) {
int num1 = input.nextInt();
System.out.println("请输入第二个操作数:");
if (input.hasNextInt() == true) {
int num2 = input.nextInt();
switch (opt) {
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=num1/num2;
break;
default:
System.out.println("输入错误。");
break;
}
System.out.println("计算结果:"+num1+""+opt+num2+"="+result);
}else {
System.out.println("请输入正确的数字!");
}
} else {
System.out.println("请输入正确的数字!");
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("*******************系统*************");
System.out.println("1.进入");
System.out.println("2.退出");
System.out.println("请输入数字");
/*
* 通过调用Scanner对象的hasNextInt(),来判断输入的数字是否合法
* */
if (input.hasNextInt()==true) {
int num=input.nextInt();
switch (num) {
case 1:
System.out.println("don't go");
break;
case 2:
System.out.println("let go");
break;
default:
System.out.println("错误");
break;
}
} public static void main(String[] args) {
//方法一:
/*100以内的偶数之和
* */
int num=0;
int sum=0;
while (num<=100) {
if (num%2==0) {
sum+=num;
}
num++;
}
System.out.println(sum);
//方法二:
while (num<=100) {
sum+=num;
num+=2;
}
System.out.println(sum); }
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请选择编号:\n 1.T血,2.网球鞋,3.网球拍"); double price=0.0;
String name="";
int goodsno=0;
String answer="y";
while ("y".equals(answer)) {
System.out.println("请输入编号:");
goodsno=input.nextInt();
switch (goodsno) {
case 1:
name="djs";
price=245.0;
break;
case 2:
name="fd";
price=570.0;
break;
case 3:
name="res";
price=320.0;
break;
default:
break;
}
System.out.println(name+"\t"+"¥"+price+"\t");
System.out.println("是否继续?(y/n)");
answer=input.next();
}
System.out.println("程序结束");
}
public static void main(String[] args) {
//01.列出商品清单
System.out.println("1.T恤 2.网球鞋 3.网球拍");
//02.肯定要用到循环
String answer="";
//定义折扣
double discount=0.8;
//保存总金额
double money=0;
Scanner input=new Scanner(System.in); do{
System.out.println("请输入商品编号"); int proNo=input.nextInt();
System.out.println("请输入商品数量");
int proNum=input.nextInt();
String proName="";
double proPrice=0;
switch (proNo) {
case 1:
//列出第一个商品的信息
//名称
proName="T恤";
//价格
proPrice=25;
break;
case 2:
proName="网球鞋";
proPrice=260;
break;
case 3:
proName="网球拍";
proPrice=200;
break;
default:
break;
}
//03.输出当前所购买商品的详情
double sum=proPrice*proNum;
money=money+sum;
System.out.println(proName+"\t¥"+proPrice+"\t数量"+proNum+"\t小计"+sum);
System.out.println("是否继续?(y/n)");
answer=input.next();
}while(answer.equals("y"));
//出了循环,意味着用户要结账,离开商店
System.out.println("折扣:"+discount);
//应付金额
double yingfuMoney=money*discount;
System.out.println("应付金额:"+yingfuMoney);
System.out.println("请输入您的支付金额:");
int userMoney=input.nextInt();
//找零
double lastMoney=userMoney-yingfuMoney;
//最好判定一下
if (userMoney>=yingfuMoney) {
System.out.println("找零"+lastMoney);
} //用户录入一堆数字,拎出最大和最小值
Scanner input=new Scanner(System.in);
System.out.println("爷爷,请您输入一个数字呗");
int num=input.nextInt();
//最大值
int max=num;
//最小值
int min=num;
while(num!=0){
if(num>max){
max=num;
}
if(num<min){
min=num;
}
System.out.println("爷爷,请您输入一个数字呗");
num=input.nextInt();
}
System.out.println("最大值是"+max+"\t最小值是"+min); public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int cource=0;
int number=0;//班级总人数
int sum=0;//大于80分的人数
System.out.println("总人数:");
number =input.nextInt();
for (int i =0; i <number; i++) {
System.out.println("输入第"+(i+1)+"位成绩");
cource=input.nextInt();
if (cource<80) {
continue;
}
sum++;
}
System.out.println("80分以上的人数"+sum);
double rate=(double)sum/number*100;
System.out.println("比例:"+rate+"%");
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int sum=0;
int coure=0;
int avg=0;
boolean isnegi=false;
//Scanner input=new Scanner(System.in);
for (int i =0; i <5; i++) {
System.out.println("请输入5门课程的"+(i+1)+"门课的成绩:");
coure=input.nextInt();
if (coure<0) {
isnegi=true;
break;
}
sum+=coure;
}
if (isnegi) {
System.out.println("成绩输入错误");
}else {
avg=sum/5;
System.out.println("总成绩:"+sum);
System.out.println("平均成绩:"+avg);
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int pwd=123456;//密码
String name="jim";//户名s
for (int i = 0; i <3; i++) {
System.out.println("请输入用户名:");
String Rname=input.next();
System.out.println("请输入密码:");
int Rpwd=input.nextInt();
if (Rname.equals(name)&&Rpwd==pwd) {
System.out.println("欢迎登陆系统"); }else {
System.out.println("输入错误!您还有"+(3-(i+1))+"次机会");
System.out.println("对不起!"+((i+1))+"次错误!");
continue; }
}
int rabbit;//兔子
int chicken;//小鸡
for(rabbit=1;rabbit<14;rabbit++){
chicken=35-rabbit;
if((rabbit*4+chicken*2)==94)
System.out.println("兔子:"+rabbit+"\t小鸡:"+chicken);
} for (int i = 1; i <=100; i++) { if (i%3==0) {
System.out.println("Flip");
}
else {
System.out.println(i);
}
if (i%5==0) {
System.out.println("Flop");
}
else {
System.out.println(i);
}
if (i%3==0&&i%5==0) {
System.out.println("FlipFlop");
}else {
System.out.println(i);
}
} int men;
int women;
for( men=0;men<=10;men++){
for( women=0;women<=30;women++){
if(30-men-women >= 0){
int count = 3*men + 2*women + 1*(30-men-women) ;
if(count == 50){
System.out.println("男人:" + men+",女人:"+women+",小孩:" +(30-men-women)) ;
}
}
}
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
/*
*选择游戏
*
* */
System.out.println("欢迎进入青鸟迷你戏平台");
System.out.println("");
System.out.println("请选择你喜爱的游戏");
System.out.println("----------------------------");
System.out.println("1.斗地主");
System.out.println("2.斗牛");
System.out.println("3.泡泡龙");
System.out.println("4.连连看");
System.out.println("-----------------------------");
System.out.println("请选择,输入数字:");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("您已进入斗地主房间!");
break;
case 2:
System.out.println("您已进入斗牛房间!");
break;
case 3:
System.out.println("您已进入泡泡龙房间!");
break;
case 4:
System.out.println("您已进入连连看房间!");
break;
}
/*
* 玩游戏升级
*
* */
int score = 0;
int Num = 1;
String answer;
do {
System.out.println("您正在玩" + Num + "局,输入您的成绩为:");
score = input.nextInt();
if (score > 80) {
}
Num++;
if (Num > 5) {
System.out.println("游戏结束!!");
System.out.println("恭喜你,通过一级"); } else {
System.out.println("继续玩下一局吗?(yes/no)");
answer = input.next();
if (answer.equals("no")) {
System.out.println("您已中途退出游戏");
System.out.println("对不起,您未能晋级,继续加油呀!");
break;
} else {
System.out.println("进入下一局");
}
}
} while (Num <= 5); /*玩游戏并支付游戏币
*
* */
int money;
double discount1 = 0.5;
double discount2 = 0.8;
int m1 = 10;
int m2 = 20;
System.out.println("请您选择玩的游戏类型:");
System.out.println("1.牌类");
System.out.println("2.休闲竞技类");
int num1 = input.nextInt();
System.out.println("请您输入游戏时长:");
int time = input.nextInt();
switch (num1) {
case 1:
if (time > 10) {
System.out.println("您玩的是牌类游戏,时长是:" + time + "小时,可以享受5折优惠");
money = (int) (time * m1 * discount1);
System.out.println("您需支付" + money + "个游戏币"); } else {
System.out.println("您玩的是牌类类游戏,时长是:" + time + "小时,可以享受8折优惠");
money = (int) (time * m1 * discount2);
System.out.println("您需支付" + money + "个游戏币");
}
break;
case 2:
if (time > 10) {
System.out.println("您玩的是休闲类游戏,时长是:" + time + "小时,可以享受5折优惠");
money = (int) (time * m2 * discount1);
System.out.println("您需支付" + money + "个游戏币"); } else {
System.out.println("您玩的是休闲类游戏,时长是:" + time + "小时,可以享受8折优惠");
money = (int) (time * m2 * discount2);
System.out.println("您需支付" + money + "个游戏币");
}
break;
}
/*统计游戏点击率
*
* */
int j = 0;
int num2 =0;
for (int i = 1; i <= 4; i++) {
System.out.println("请输入第" + i + "个游戏的点击率:");
num2 = input.nextInt();
if (num2 <= 100) {
continue;
}
j++;
}
System.out.println("点击率大于100的游戏数是:" + j);
double rate = (double)j/ 4 * 100;
System.out.println("点击率大于100的游戏所占的比例是:" + rate + "%");
/*添加用户信息
*
* */
int a=0,age=0,b=0;
System.out.println("请输入要登录用户的数量:");
int num3=input.nextInt();
for (int i=0;i<num3;i++){
System.out.println("请输入用户编号<4位整数>:");
a=input.nextInt();
System.out.println("请输入用户年龄:");
age=input.nextInt();
System.out.println("请输入用户积分:");
b=input.nextInt();
if(age<10){
System.out.println("很抱歉,你年龄不适合玩游戏\n录入失败");
}else{
System.out.println("您输入的会员信息是:");
System.out.println("用户编号"+a+"\t用户年龄"+age+"\t用户积分"+b);
}
} } public static void main(String[] args) { Scanner input=new Scanner(System.in);
System.out.println("请输入整数:");
int in=input.nextInt();
int result=1;
System.out.print(in+"!=");
for (int i = 1; i<=in; i++) {
System.out.print(i);
if (i<in) {
System.out.print("*"); }
result=result*i; }
System.out.print("="+result);
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int num = 0; //个数
int sum = 0; //和
for(int i = 0;i <= 100;i ++) {
if(i % 7!= 0) {
num ++;
sum = sum+i;
System.out.print(i + "\t");
if(num % 4 == 0)
System.out.println();
}
}
System.out.println();
System.out.println("总和为:"+sum); Scanner input=new Scanner(System.in); int scores []=new int[5];
int sum=0; System.out.println("请输入五位同学的成绩:");
for (int i = 0; i < scores.length; i++) {
scores[i]=input.nextInt();
sum+=scores[i];//成绩累加
}
System.out.print("平均分:"+sum/scores.length); int scores []=new int[]{90,80,100,95,90};
System.out.println("请修改第三位同学的成绩为98:");
scores[2]=98;//数组下标
System.out.println("修改后,五位同学的成绩:");
for (int i = 0; i < scores.length; i++) {
System.out.print(scores[i]+" ");
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double sum=0;
double scores[]=new double[5];
for (int i = 0; i < scores.length; i++) {
System.out.print("请输入第"+(i+1)+"笔金额的记录:");
scores[i]=input.nextDouble();
sum+=scores[i];
}
System.out.println("序号\t\t"+"金额(元)");
for (int i = 0; i < scores.length; i++) {
System.out.print((i+1)+"\t\t");
System.out.println(scores[i]);
}
System.out.println("总金额:\t\t"+sum); public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int scores []=new int[5];
System.out.println("请输入5位同学的成绩:");
//录入成绩
for (int i = 0; i < scores.length; i++) {
scores[i]=input.nextInt();
}
Arrays.sort(scores);//排序
System.out.println("学员成绩按升序排序:");
for (int i = 0; i < scores.length; i++) {
System.out.println(scores [i]+" ");
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int scores []=new int[5];
int max=0;
System.out.println("请输入5位同学的成绩:");
//录入成绩
for (int i = 0; i < scores.length; i++) {
scores[i]=input.nextInt();
} for (int i = 0; i < scores.length; i++) {
if (scores[i]>max) {
max=scores[i];
}
}
System.out.println("最高分:"+max);
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int [] list=new int [6];
list[0]=99;
list[1]=95;
list[2]=92;
list[3]=89;
list[4]=69;
list[5]=49;
int index=list.length;//保存新增成绩的位置
System.out.println("请输入新增成绩:");
int num=input.nextInt();//输入要插入的数据
//找到新元素插入的位置
for (int i = 0; i < list.length; i++) { if (num>list[i]) {
index=i;
break;
}
}
//元素后移
for (int i =list.length-1 ; i >index ; i--) {
list[i]=list[i-1];//index下标开始的元素后移一个位置
}
list[index]=num;
System.out.println("插入成绩的下标:"+index); System.out.println("插入后成绩信息是:");
for (int i = 0; i < list.length; i++) {
System.out.println(list[i]+"\t");
}
} public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String[]num=new String[]{"a","c","u","b","e","p","f","z"};
System.out.print("原字符序列:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
Arrays.sort(num);
System.out.println();//换行
System.out.print("升序排序后:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
System.out.println();//换行
System.out.print("逆序输出为:");
//逆序,则从最后的哪一个元素排在第一位
for (int i = num.length-1; i >=0 ; i--) {
System.out.print(num[i]+" ");
} Scanner input=new Scanner(System.in);
String[]num=new String[]{"a","c","u","b","e","p","f","z"};
System.out.print("原字符序列:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
Arrays.sort(num);
System.out.println();//换行
System.out.print("升序排序后:");
for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+" ");
}
System.out.println();//换行
System.out.print("逆序输出为:");
//逆序,则从最后的哪一个元素排在第一位
for (int i = num.length-1; i >=0 ; i--) {
System.out.print(num[i]+" ");
}
int index=num.length;//待插入的字符的位置
System.out.println();
System.out.print("待插入的字符:");
String way=input.next();//输入要插入的位置
//找到要插入字符的位置
for (int i = 0; i < num.length; i++) {
if (way.equalsIgnoreCase(num[i])) {
index=i;
break;
}
}
//元素后移
for (int i =num.length-1; i >index; i--) {
num[i]=num[i-1];
}
num[index].equals(way);//插入数据
System.out.println("插入字符的下标:"+index);
System.out.println("插入后字符的信息是:");
for (int i = 0; i < num.length; i++) {
System.out.println(num[i]+"\t");
} Scanner input=new Scanner(System.in);
System.out.println("请输入4家店的价格");
int[]num=new int[4];
for (int i = 0; i < num.length; i++) {
System.out.print("第"+(i+1)+"店的价格:");
num[i]=input.nextInt();
}
int min=num[0];
int index=0;
for (int j = 0; j < num.length; j++) {
if (num[j]<min) {
min=num[j];
index=j;
} }
System.out.print("最低价格:"+min);
System.out.println("且它在数组中的原始位置(下标)是:" + index);
}
} } Scanner input=new Scanner(System.in);
String []num= new String[5];
System.out.println("请输入5句话");
for (int i = 0; i < num.length; i++) {
System.out.print("第"+(i+1)+"句话:");
num[i]=input.next();
}
System.out.println();
System.out.println("逆序输出的5句话:");
for (int i =num.length-1; i>=0 ; i--) {
System.out.print(num[i]+" ");
}
int[] points = { 18, 25, 7, 36, 13, 2, 89, 63 };
System.out.println("8位顾客的积分:");
for (int i = 0; i < points.length; i++) {
System.out.print(points[i] + "\t");
}
System.out.println();
int min = 0;
int index = 0;
min = points[0];
for (int i = 1; i < points.length; i++) {
if (points[i] < min) {
min = points[i];
index = i;
}
}
System.out.println("其中最低积分:" + min);
System.out.println("且它在数组中的原始位置(下标)是:" + index);
} Scanner input=new Scanner(System.in);
int nums[] = new int[10];
int a = 0;
int b = 0;
int c = 0;
int d = 0;
System.out.println("请输入10个数:");
for (int i = 0; i < nums.length; i++) {
nums[i] = input.nextInt(); switch (nums[i]) {
case 1:
a++;
break;
case 2:
b++;
break;
case 3:
c++;
break;
default:
d++;
break;
} }
System.out.println("数字1的个数:"+a);
System.out.println("数字2的个数:"+b);
System.out.println("数字3的个数:"+c);
System.out.println("非法数字的个数:"+d); Scanner input=new Scanner(System.in); int []array=new int[]{1,3,-1,5,-2};
System.out.println("原数组的为:");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]+ " ");
}
System.out.println();
int newarray[]=new int[5];
for (int i = array.length - 1; i >= 0; i--) {
if (array[i] < 0) {
continue;
}
if (array[i] > 0) {
newarray[array.length - i - 1] = array[i];
}
}
System.out.println("");
System.out.println("逆序并处理后的数组为:");
for (int i = 0; i < newarray.length; i++) {
System.out.print(newarray[i]+" ");
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int [] score=new int[4];
int classNum=3;
double Sum=0.0;//成绩总和
double []average=new double[classNum];//平均成绩数组
//循环输入学员成绩
for (int i = 0; i <classNum; i++) {
Sum=0.0;//成绩总和归零
System.out.println("请输入第"+(i+1)+"个班的成绩");
for (int j = 0; j < score.length; j++) {
System.out.print("第"+(j+1)+"个学员的成绩:");
score[j]=input.nextInt();
Sum=Sum+score[j];
}
average[i]=Sum/score.length;//计算平均分
System.out.println("第"+(i+1)+"个班级参赛学员的总成绩:"+Sum);
System.out.print("第"+(i+1)+"个班级参赛学员的平均分是:"+average[i]+" \n");
} } public static void main(String[] args) {
Scanner input=new Scanner(System.in); int [] score=new int[4];
int classNum=3;
double Sum=0.0;//成绩总和
int count=0;//记录80分以上的人数
double []average=new double[classNum];//平均成绩数组
//循环输入学员成绩
for (int i = 0; i <classNum; i++) {
Sum=0.0;//成绩总和归零
System.out.println("请输入第"+(i+1)+"个班的成绩");
for (int j = 0; j < score.length; j++) {
System.out.print("第"+(j+1)+"个学员的成绩:");
score[j]=input.nextInt();
Sum=Sum+score[j];
if (score[j]<85) {//成绩小于85,则跳出本次循环
continue;
}
count++;
}
average[i]=Sum/score.length;//计算平均分
System.out.println("第"+(i+1)+"个班级参赛学员的总成绩:"+Sum);
System.out.print("第"+(i+1)+"个班级参赛学员的平均分是:"+average[i]+" \n");
System.out.println("成绩85分以上的人数:"+count+"人");
} Scanner input=new Scanner(System.in);
System.out.println("请输入直角三角形的行数:");
int Rownum=0;
Rownum=input.nextInt();
for (int i = 1; i <=Rownum; i++) {//行数
for (int j = 1; j <=i*2-1; j++) {//星数
System.out.print("☆");
}
System.out.println();
}
}
Scanner input=new Scanner(System.in);
System.out.println("请输入倒直角三角形的行数:");
int Rownum=0;
Rownum=input.nextInt();
for (int i = 1; i <=Rownum; i++) {//行数
for (int j = 1; j <=10-i; j++) {
System.out.print("*");
}
System.out.println("");
} }
Scanner input=new Scanner(System.in);
System.out.println("请输入等腰三角形的行数:");
int Rownum=0;
Rownum=input.nextInt();
for (int i = 1; i <=Rownum; i++) {//行数
for (int j = 0; j <Rownum-i; j++) {//空格数
System.out.print(" ");
}
for (int k = 1; k <=2*i-1; k++) {//星星数
System.out.print("*");
}
System.out.println(""); }
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int Rownum=9;
for (int i = 1; i <=Rownum; i++) {//行数
for (int j = 1; j <=i; j++) {//空格数
System.out.print(j+"*"+i+"="+j*i+" ");
}
System.out.println(""); }
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int count=0;//计数器
String choice="";//顾客选择是否离开
for (int i = 0; i <5; i++) {
System.out.println("欢迎光临第"+(i+1)+"家专卖店");
for (int j = 0; j <3; j++) {
System.out.println("要离开吗?(y/n)");
choice=input.nextLine();
//如果离开,则跳出,进入下一家店
if ("y".equals(choice)) {
break;
}
System.out.println("买了一件衣服");
count++;
}
System.out.println("离店结账");
}
System.out.println("总共买了"+count+"件衣服");
choice=input.nextLine(); int way = 1; //买法
int k = 0; //雏鸡数
for(int i=1;i<=20;i++){ //公鸡数
for(int j=1;j<=33;j++){ //母鸡数
k = 100-i-j; //一共100只鸡
if(k%3 == 0 && (5*i+3*j+k/3 == 100)){//雏鸡数是3的倍数,总计100文钱
System.out.print("买法 " + way++ + ": ");
System.out.println("公鸡: " +i+ " 母鸡:" +j+ " 雏鸡:" +k); }
}
}
Scanner input=new Scanner(System.in); int[] score = new int[4];//从零开始,0,1,2,3数 ,//成绩数组
int classnum = 3; //班级数目
double sum = 0.0; //成绩总和
double average = 0.0; //平均成绩
int count = 0; //记录85分以上学员人数 //循环输入学员成绩 for(int i = 0; i < classnum; i++){
System.out.println("请输入第" + (i+1) + "个班级的成绩");
for(int j = 0; j < score.length; j++){
System.out.print("请输入第" + (j+1) + "个学员的成绩:");
score[j] = input.nextInt();
if(score[j] < 85){ //成绩小于85,则跳出本轮循环
continue;
}
sum = sum + score[j]; //成绩85分以上才进行累加
count++;
}
}
average = sum / count; //所有成绩85分以上的学员的平均分
System.out.println("所有成绩85分以上的学员的平均分为:" + average); Scanner input=new Scanner(System.in);
String pass = ""; //保存用户输入密码
int amount = 0; //取款金额
String password = "111111"; //用户密码
int count = 0; //记录密码输入次数
boolean isPass = false; //密码是否通过验证
while(count < 3 && !isPass){
System.out.print("请输入密码:");
pass = input.next();
if(!password.equals(pass)){
count++;
continue;
}
isPass = true; //密码通过验证
System.out.print("请输入金额:");
amount = input.nextInt();
while(amount>0){
if(amount<=1000 && amount%100==0){
System.out.println("您取了" +amount+ "元");
System.out.println("交易完成,请取卡!");
break; //完成交易,退出
}else{
System.out.print("您输入金额的金额不合法,请重新输入:");
amount = input.nextInt();
continue; //继续让用户输入金额
}
}
}
if(!isPass){ //用户输入了3次错误密码
System.out.print("密码错误,请取卡!");
}
Scanner input=new Scanner(System.in);
int rows = 0; //菱形的行数
System.out.print("请输入菱形行数:");
rows = input.nextInt(); while(rows%2 == 0){
System.out.print("请输入奇数:");
rows = input.nextInt();
} int n = (rows+1)/2;
//打印菱形的上半部分
for(int i = 1; i <= n; i++){//外层循环变量i控制行数
for(int j = 1; j <= n-i; j++){//内层循环变量j控制该行空格数
System.out.print(" ");
}
for(int k = 1; k <= 2*i-1; k++){//内层循环变量k控制该行*号数
System.out.print("*");
}
System.out.print("\n");
}
//打印菱形的下半部分
for(int i = n-1; i >= 1; i--){
for(int j = 1; j <= n-i; j++){
System.out.print(" ");
}
for(int k = 1; k <= 2*i-1; k++){
System.out.print("*");
}
System.out.print("\n");
}
Scanner input = new Scanner(System.in);
String answer = "";
boolean a = false;
boolean b = false;
do {
System.out.println("*********欢迎**********");
System.out.println("1.注册");
System.out.println("2.登录");
System.out.println("3.抽奖");
System.out.println("请输入:");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("您选择的是注册");
System.out.println("请填写个人注册信息");
System.out.println("用户名:");
String name = input.next();
System.out.println("密码:");
int nums = input.nextInt();
int random = (int) (Math.random() * 9000) + 1000;
System.out.println("用户名\t密码\t会员号");
System.out.println(name + "\t" + nums + "\t" + random);
a = true;
System.out.println("是否继续(y/n)");
answer = input.next();
break;
case 2:
if (a == true) {
System.out.println("您选择的是登录");
System.out.println("请输入您的用户名:");
name = input.next();
System.out.println("请输入密码:");
nums = input.nextInt();
System.out.println("欢迎您" + name);
b = true;
} else {
System.out.println("请您先注册,亲");
}
System.out.println("是否继续(y/n)");
answer = input.next();
break;
case 3:
int random1 = (int) (Math.random() * 9000) + 1000;
if (b == true) {
System.out.println("您选择的是抽奖");
System.out.println("请输入您的卡号:");
int kahao = input.nextInt();
System.out.println("本日的幸运数字为");
for (int i = 0; i < 5; i++) {
random1 = (int) (Math.random() * 9000) + 1000;
System.out.print(random1+"\t");
}
if (kahao == random1) {
System.out.println("恭喜您,您中奖了");
System.out.println("是否继续(y/n)");
answer = input.next(); } else {
System.out.println("对不起!您不是本日的幸运会员.");
System.out.println("是否继续(y/n)");
answer = input.next(); } } else {
System.out.println("请先登录,亲");
System.out.println("是否继续(y/n)");
answer = input.next(); }
break;
default:
System.out.println("输入错误!");
System.out.println("是否继续(y/n)");
answer = input.next();
break;
} } while (answer.equals("y"));
System.out.println("退出"); public class School {
/*
* School类的属性
* */
String schName;//名称
int classNumber;//教室数目
int labNumber;//机房数目
/*
* School类的Show方法
* */
public void Show(){
System.out.println(schName+"培训中心\n"+"配备:"+
classNumber+"教"+labNumber+"机"); public static void main(String[] args) {
School sch=new School();
System.out.println("*******初始化成员变量前*******");
sch.Show();
sch.schName="北京";
sch.classNumber=20;
sch.labNumber=30;
System.out.println("\n**********初始化变量后**************");
sch.Show(); public class Student {
/*Student的属性
* */
String name;
int age;
String classNo;//班级
String hobby;//爱好
/*Student的方法
* */
public void showStudent(){
System.out.println("姓名:"+name+"\n年龄:"+age+"\n就读于:"+classNo+"\n爱好:"+hobby); }
}
public static void main(String[] args) {
Student stu=new Student();
stu.name="小小";
stu.age=15;
stu.classNo="S1";
stu.hobby="打篮球";
stu.showStudent(); public class Visitor {
String name;
int age;
public void Show(){
Scanner input=new Scanner(System.in);
while (!"n".equals(name)) {
if (age>=18&&age<=60) {
System.out.println(name+"的年龄为:"+age+",门票价格为:20元\n");
}else {
System.out.println(name+"的年龄为:"+age+",门票免费\n");
}
System.out.println("请输入姓名:");
name=input.next();
if (!"n".equals(name)) {
System.out.print("请输入年龄:");
age=input.nextInt();//给age属性赋值
}
}
System.out.println("退出程序");
} public static void main(String[] args) {
//************Visitor**********
Scanner input=new Scanner(System.in);
Visitor v=new Visitor();
System.out.println("请输入姓名:");
v.name=input.next();
System.out.println("请输入年龄:");
v.age=input.nextInt();
v.Show(); public class Administrator {
public String name;
public String password; }
public class OneDay {
public static void main(String[] args) {
//************Visitor**********
Scanner input=new Scanner(System.in);
Administrator as=new Administrator();
as.name="admin";
as.password="111111";
//输入旧密码
String chioce="";
do {
System.out.println("请输入用户名:");
String nameinput=input.next();
System.out.println("请输入密码");
String pass=input.next();
//判断用户输入的用户名是否正确
if (as.name.equals(nameinput)&&as.password.equals(pass)) {
System.out.println("\n请输入新密码:");
as.password=input.next();
System.out.println("修改密码成功!!您的新密码:"+pass); }else {
System.out.println("用户名和密码不匹配,您没有权利更改信息");
System.out.println("还想继续吗?请输入(y/n)");
chioce=input.next();
}
}while("y".equals(chioce)); } public class Calcutor {
public int num1;
public int num2;
public String yunsuan; }
Scanner input =new Scanner(System.in);
Calcutor calculator=new Calcutor();
System.out.println("请输入操作数");
calculator.num1=input.nextInt();
System.out.println("请输入运算符");
calculator.yunsuan=input.next();
char a=calculator.yunsuan.charAt(0);
System.out.println("请输入操作数");
calculator.num2=input.nextInt();
switch(a){
case '+':
System.out.println(calculator.num1+"+"+calculator.num2+"="+(calculator.num1+calculator.num2));
break; public class Currentlor {
public String time;
public void show(){ }
public String time2;
public void show(){ }
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
Currentlor currentTime=new Currentlor();
currentTime.time="2015年5月12日10点11分00秒";
System.out.println(currentTime.time);
currentTime.show();
System.out.println("改后为");
Domo domo=new Domo();
domo.time2="2015年5月12日10点11分30秒";
System.out.println(domo.time2);
domo.show(); public class Season {
String spring(){
return "\n该季节为春季";
}
String summer (){
return "\n该季节为夏季";
}
String autumn(){
return "\n该季节为秋季";
}
String winter(){
return "\n该季节冬季";
}
} public static void main(String[] args) {
// TODO Auto-generated method stub
Season sn=new Season();
//获取输入
/* 接收成绩 */
Scanner input = new Scanner(System.in);
System.out.print("请输入月份:");
int mon = input.nextInt();
switch(mon){
case 1:
case 2:
case 3:
System.out .println(sn.spring());
break;
case 4:
case 5:
case 6:
System.out .println(sn.summer());
break;
case 7:
case 8:
case 9:
System.out .println(sn.autumn());
break;
case 10:
case 11:
case 12:
System.out .println(sn.winter());
break;
default:
System.out .println("输入有误,请输入1~12月份数。");
break; } public class GuessMachine {
String name ;
double price; /**
* 初始化商品信息
*/
public void initial(){
int random = (int)(Math.random()*10); //产生随机数
switch(random){
case 0:
case 1:
case 2:
case 3:
name = "公主电动车";
price = 2000;
break;
case 4:
case 5:
case 6:
name = "34寸纯平彩电";
price = 3000;
break;
case 7:
case 8:
case 9:
name = "新飞冰箱";
price = 3400;
break;
default:
break;
}
} /**
* 猜测
*/
public void guess(){
System.out.print("\n请猜测\""+ name + "\"的价格: " );
Scanner input = new Scanner(System.in);
double guessPrice = input.nextDouble(); //从键盘获取用户猜的价格;
boolean isCorrect = false;
for(int i = 0; i<3; i++){ //允许最多猜4次
if(guessPrice == price){ //判断是否猜对,并给出提示信息
System.out.println("猜对了!");
isCorrect = true;
break;
}else if(guessPrice > price){
System.out.println("再小点!");
}else{
System.out.println("再大点!");
}
System.out.print("\n再猜一次吧: ");
guessPrice = input.nextDouble();
}
if(!isCorrect){
System.out.println("\n4次内没有猜对,下次努力吧!");
}}
public static void main(String[] args) {
GuessMachine gm = new GuessMachine();
gm.initial();
gm.guess();
} public class Computer {
public String nickName;
public int fenshu;
public int chuquan(){
int num=(int)(Math.random()*3)+1;
switch (num) {
case 1:
System.out.println("["+nickName+"]"+"您出的是剪刀");
break;
case 2:
System.out.println("["+nickName+"]"+"您出的是石头");
break;
case 3:
System.out.println("["+nickName+"]"+"您出的是布");
break;
default:
break;
}
return num;
} public class Judge {
Computer computer = new Computer();
Person person = new Person();
int count = 0;
String answer = ""; public void result() {
System.out.println("游戏结束了,比赛结果如下");
System.out.println(person.nickName + "\tVS" + computer.nickName);
System.out.println("我们一共玩了" + count + "局");
System.out.println("其中我赢了" + person.fenshu + "局");
if (person.fenshu > computer.fenshu) {
System.out.println("我战胜了你");
} else if (person.fenshu == computer.fenshu) {
System.out.println("我们打了个平手"); } else {
System.out.println("我输了,其实我是让你的");
}
} public void getOnceResult() {
Scanner input = new Scanner(System.in);
System.out.println("您确定对战吗?(y/n)");
answer = input.next();
do {
count++;
int person1 = person.chuquan();
int computer1 = computer.chuquan();
if (person1 == 1 && computer1 == 2 || person1 == 2
&& computer1 == 3 || person1 == 3 && computer1 == 1) {
System.out.println("不好意思,我赢了");
person.fenshu++;
} else if (person1 == computer1) {
System.out.println("我们平局");
} else {
System.out.println("我输了,你厉害");
computer.fenshu++; }
System.out.println("继续吗?(y/n)");
answer = input.next();
} while (answer.equals("y"));
} public void menuList() {
System.out.println("------------------欢迎进入天尽头猜拳系统--------------");
System.out.println("\n\t\t******************");
System.out.println("\n\t\t*****游戏开始了 ******");
System.out.println("\n\t\t*******************");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.println("请选择对方角色:1.上帝 2.耶稣 3.佛祖");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println("请输入您的大名:");
person.nickName = input.next();
switch (num) {
case 1:
computer.nickName = "上帝"; System.out.println(person.nickName + ",您选择与上帝对战"); break;
case 2:
computer.nickName = "耶稣";
System.out.println(person.nickName + ",您选择与耶稣对战"); break;
case 3:
computer.nickName = "佛祖";
System.out.println(person.nickName + ",您选择与佛祖对战");
break;
default:
break;
}
} public void Welcome() {
menuList();
getOnceResult();
// 出了循环,打印结果
if (answer.equals("n")) {
result(); }
public String nickName;
public int fenshu;
public int chuquan(){
Scanner input=new Scanner(System.in);
System.out.println("请出拳1.剪刀 2.石头 3.布 ");
int num=input.nextInt(); switch (num) {
case 1:
System.out.println(nickName+"您出的是剪刀");
break;
case 2:
System.out.println(nickName+"您出的是石头");
break;
case 3:
System.out.println(nickName+"您出的是布");
break;
default:
break;
}
return num;
} public static void main(String[] args) {
Judge judge=new Judge();
judge.Welcome();
} public class Loan { /**
* 计算月供
* @return money
*/
public double loan(double loan,int yearchoice){
double money=0; if(yearchoice==1){
money=(loan+loan*0.0603)/36;
}else if(yearchoice==2){
money=(loan+loan*0.0612)/60;
}else{
money=(loan+loan*0.0639)/240;
}
return money;
} /**
* @param args
*/
public static void main(String[] args) { Loan l= new Loan();
Scanner input = new Scanner(System.in);
System.out .print("请输入贷款金额:");
double loan=input.nextDouble();
System.out .print("请选择贷款年限:1.3年(36个月) 2.5年(60个月) 3.20年(240个月):");
int yc=input.nextInt();
System.out.println("\n***月供为:"+l.loan(loan, yc));
} public class StudentBiz {
/**
* 求平均分
* @param 一个学生
*/
public double getAvg(Student stu){
return stu.java+stu.c+stu.sql/3.0;
}
public class Student {
public int java;
public int c;
public int sql; }
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
//实例化学生对象
Student student=new Student();
System.out.println("请输入java的成绩");
student.java=input.nextInt();
System.out.println("请输入c的成绩:");
student.c=input.nextInt();
System.out.println("请输入sql的成绩:");
student.sql=input.nextInt(); StudentBiz sb=new StudentBiz();
double avgScore=sb.getAvg(student);
System.out.println("平均分:"+avgScore); }
}
public class Triangle {
/**
* 三角形的判断
* @param a、b、c边长
* @return 是否是三角形
*/
public boolean isTriangle(int a,int b,int c){
boolean flag=false;
if((a+b)>c &&(b+c)>a &&(a+c)>b){
flag=true;
}
return flag;
}
/**
* 三角形的形状判断
* @param a、b、c边长
* @return 何种三角形
*/
public String Shape(int a,int b,int c){
String shape=""; if((a==b)&&(b==c)&&(c==a)){
shape="等边三角形";
}else if((a==b)||(b==c)||(c==a)){
shape="等腰三角形";
}else {
int A=a*a;
int B=b*b;
int C=c*c;
if((A>B+C)||(B>A+C)||(C>A+B)){
shape="钝角三角形";
}else if((A==B+C)||(B==A+C)||(C==A+B)){
shape="直角三角形";
}else{
shape="锐角三角形";
}
}
return shape;
} public class TestTriangle {
public static void main(String[] args) { Triangle t=new Triangle();
boolean con=true;
while(con){
Scanner input = new Scanner(System.in);
System.out .print("请输入第一条边:");
int num1=input.nextInt();
System.out .print("请输入第二条边:");
int num2=input.nextInt();
System.out .print("请输入第三条边:");
int num3=input.nextInt();
if(t.isTriangle(num1,num2,num3)){
System.out .print("这是一个"+t.Shape(num1, num2, num3));
}else{
System.out .print("这不能构成三角形。");
}
System.out .print("\n继续吗?(y/n):");
String choice=input.next();
if(choice.equals("n")){
con=false;
System.out .print("谢谢使用!");
}
} public class InputVerify {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean con = true;
/*检验生日形式的有效性*/
while(con){
System.out.print("\n请输入会员生日<月/日:00/00>: ");
String day = input.next();
if(day.indexOf('/')!=2){
System.out.println("生日形式输入错误!");
}else{
System.out.println("该会员生日是: " + day);
con = false;
}
} con = true;
/*检验用户密码的有效性*/
while(con){
System.out.print("\n请输入会员密码<6~10位>: ");
String psw = input.next();
if(psw.length() < 6 || psw.length() > 10){
System.out.println("密码形式输入错误!");
}else{
System.out.println("该会员的密码是: " + psw);
con = false;
}
}
}
public class StringExplorer {
/**
* 定位字符
*/
public void locateChar(String full, char c) {
System.out.print(c + "出现的位置是: ");
for (int i = 0; i < full.length(); i++) {
if (c == full.charAt(i)) {
System.out.print(i + "\t");
}
}
} /**
* 定位字符串
*/
public void locateString(String full, String s) {
int index = 0;
int i = index;
int length = full.length();
System.out.print(s + "出现的位置是: ");
do {
index = full.indexOf(s, i);
if (index == -1) {
break;
}
System.out.print(index + " ");
i = index + s.length();
} while (i <= length);
}
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StringExplorer se = new StringExplorer();
Scanner input = new Scanner(System.in);
System.out.println("请输入一段字符: ");
String full = input.next();
System.out.println("请输入要查询的字符串: ");
String s = input.next();
se.locateString(full, s);
}