循环结构习题代码
Posted twqwe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了循环结构习题代码相关的知识,希望对你有一定的参考价值。
一:升级购物结算
import java.util.Scanner;
public class ShoppingCheckOut {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String name = "";
String answer = "y";
int num;
double price = 0;// 商品金额
int amount = 0; // 购买数量
double discount = 0.8;// 折扣
double total = 0; // 商品总价
double payment = 0; // 实付金额
System.out.println("MyShopping管理系统>购物结算");
System.out.println("****************************************");
System.out.println("请输入购买的商品编号:");
System.out.println("1.T恤 2.网球鞋 3.网球拍");
System.out.println("****************************************
");
Scanner input = new Scanner(System.in);
while ("y".equals(answer)) {
System.out.print("请输入购买的商品编号:");
num = input.nextInt();
System.out.print("请输入购买数量:");
amount = input.nextInt();
switch (num) {
case 1:
name = "T恤";
price = 245.0;
break;
case 2:
name = "网球鞋";
price = 570.0;
break;
case 3:
name = "网球拍";
price = 320.0;
break;
default:
System.out.println("您的输入有误");
continue;
} // switch 括号
System.out.println(name + "¥" + price + " " + "数量" + amount + " "
+ "合计¥" + price * amount);
System.out.print("是否继续:(y/n)");
answer = input.next();
} // while括号
System.out.println("折扣为:" + discount);
total = total + price * amount;//
System.out.println("应付金额:" + total * discount);
System.out.print("实付金额为:");
payment = input.nextDouble();
while (payment < (total * discount)) {// 判断输入的金额是否小于应付金额
System.out.println("您的金额输入有误");
System.out.print("实付金额为:");
payment = input.nextDouble();
}
System.out.println("找钱:" + (payment - (total * discount)));
}
}
二:升级菜单切换
import java.util.Scanner;
public class MainMenu {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("欢迎使用MyShopping管理系统");
System.out.println("**********************");
System.out.println(" 1.客户信息管理");
System.out.println(" 2.购物结算");
System.out.println(" 3.真情回馈");
System.out.println(" 4.注销");
System.out.println("**********************
");
boolean ischoose = true;
Scanner input = new Scanner(System.in);
do {
System.out.println("请输入您的选择:");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("客户信息管理");
ischoose = true;
break;
case 2:
System.out.println("执行购物结算");
break;
case 3:
System.out.println("真情回馈");
break;
case 4:
System.out.println("注销");
break;
default:
System.out.println("您的输入有误,请从新输入:");
ischoose = false;
num = input.nextInt();
break;
}
} while (ischoose = false);
System.out.println("程序结束");
}
}
三:计算100以内奇数和
public class JiShu {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub 求100以内的奇数和
int sumji = 0;
int sumou = 0; // 声明sum为奇数和
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
sumji += i;
} else {
sumou += i;
}
}
System.out.println("100以内奇数和为:" + sumji);
System.out.println("100以内偶数和为:" + sumou);
}
}
四:统计顾客的年龄层次
import java.util.Scanner;
public class AgeRate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int age = 0;
int over = 0;
int less = 0;
Scanner input = new Scanner(System.in);
for (int i = 1; i <= 10; i++) {
System.out.println("请输入第" + i + "个顾客的年龄:");
age = input.nextInt();
if (age >= 30) {
over++;
} else {
less++;
}
}
System.out.println("30岁以下的比例是:" + (double) less / 10 * 100 + "%");
System.out.println("30岁以上的比例是:" + (double) over / 10 * 100 + "%");
}
}
五:录入会员信息
import java.util.Scanner;
public class AddCustomer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int vipno = 0;
String bir = "";
int vippoint = 0;
System.out.println("MyShopping管理系统> 客户信息管理> 添加客户信息");
for (int i = 1; i <= 3; i++) {
System.out.println("请输入会员号:");
vipno = input.nextInt();
System.out.println("请输入会员生日(月/日<用两位整数表示>)");
bir = input.next();
System.out.println("请输入会员积分:");
vippoint = input.nextInt();
if (vipno < 0 || vipno > 9999) {
System.out.println("您输入的会员卡号有误,请从新输入");
continue;
}
System.out.println("您录入的会员信息是:");
System.out.println(vipno + " " + bir + " " + vippoint);
}
System.out.println("程序结束!");
}
}
六:验证用户登录信息
import java.util.Scanner;
public class UserLogin {
/**
* @param args
* 用户登录 三次机会。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String user = "";
int pwd = 123456;
Scanner input = new Scanner(System.in);
for (int num = 1; num <= 3; num++) {
System.out.println("请输入用户名:");
user = input.next();
System.out.println("请输入密码:");
pwd = input.nextInt();
if ((user.equals("jim")) && pwd == 123456) {
System.out.println("欢迎使用SHopping系统");
break;
} else {
System.out.println("您的输入有误,还有" + (3 - num) + "次机会");
if (num == 3) {
System.out.println("对不起,您3次均输入错误");
}
continue;
}
//
}
}
}
七:HomeWork
import java.util.Scanner;
public class Test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double totaltime = 0;// 学习的总时长
double time = 0;
double avg = 0;
Scanner input = new Scanner(System.in);
for (int day = 1; day <= 5; day++) {
System.out.println("请输入周" + day + "的学习时间");
time = input.nextDouble();
totaltime = totaltime + time;
avg = totaltime / 5;
}
System.out.println("周一至周五每日平均学习时间是:" + avg + "小时");
}
}
public class Test3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int rabbitNum;
for (int chookNum = 0; chookNum <= 35; chookNum++) {
rabbitNum = 35 - chookNum;
if (2 * chookNum + 4 * rabbitNum == 94) {
System.out.println("鸡有" + chookNum + "只");
System.out.println("兔有" + rabbitNum + "只");
}
}
}
}
import java.util.Scanner;
public class Test4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1-100 遇到3的倍数输出FLIP 遇到5的倍数输出FLOP 3、5的倍数输出FILPFLOP 其余的输出当前数字
Scanner input = new Scanner(System.in);
for (int i = 1; i <= 100; i++) {
System.out.println("请输入一个数");
i = input.nextInt();
if (i % 3 == 0 && i % 5 == 0) {
System.out.println("FlipFlop");
} else if (i % 5 == 0) {
System.out.println("Flop");
} else if (i % 3 == 0) {
System.out.println("Flip");
} else {
System.out.println(i);
}
}
}
}
public class Test5 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int women;
int kids;
for (int men = 0; men <= 10; men++) {
women = 20 - 2 * men;
kids = 10 + men;
System.out.print("女人" + women + " ");
System.out.print("男人" + men + " ");
System.out.print("小孩" + kids + " ");
System.out.println();
}
}
}
以上是关于循环结构习题代码的主要内容,如果未能解决你的问题,请参考以下文章
习题3.12 另类循环队列(20 分)浙大版《数据结构(第2版)》题目集
习题2.2 数组循环左移(20 分)浙大版《数据结构(第2版)》题目集