目录
鸡兔同笼问题,输入鸡兔总个数和笼子里面脚的个数 求鸡兔各几只
鸡兔同笼问题,输入鸡兔总个数和笼子里面脚的个数 求鸡兔各几只
兔子:脚数4/只
鸡:脚数2/只
鸡的个数=总个数-兔子的个数
package com.day3;
import java.util.Scanner;
public class ChickenAndRabbit {
public static void main(String[] args) {
//鸡兔同笼
System.out.println("请输入鸡兔的总个数:");
Scanner sc = new Scanner(System.in);
int sum = sc.nextInt();
System.out.println("请输入笼子里面一共有多少只脚");
int foot = sc.nextInt();
for (int i = 1; i <= foot/2; i++) { //i 表示鸡的个数
int ra = sum - i; //ra 表示兔子个数
if(ra*4+i*2 == foot) {
System.out.println("兔子的个数为:"+ra+"只");
System.out.println("鸡的个数为:"+i+"只");
}
}
}
}
计算顾客年龄比例
自定义输入顾客人数和年龄
定义存放年龄大于或小于某个值的年龄 用来计算比例
比例计算公式:(((double)ageLow /num)*100) agelow为年龄低于某个值的总个数 num为顾客总人数
package com.day3;
import java.util.Scanner;
public class Customer {
public static void main(String[] args) {
//计算顾客比例
Scanner sc =new Scanner(System.in);
System.out.print("请输入顾客数目:");
int num = sc.nextInt();
int age =0;
int ageUp =0;
int ageLow=0;
for(int i=1; i<=num;i++){
System.out.print("请输入"+ i +"位顾客的年龄:");
age = sc.nextInt();
if(age<30){
ageLow++;
}
else{
ageUp++;
}
}
System.out.println("30岁以下的比例是:"+(((double)ageLow /num)*100)+"%");
System.out.println("30岁以上的比例是:"+(((double)ageUp / num)*100)+ "%");
}
}
计算100以内偶数和,奇数和类似
主要公式:i%2==0 为偶数 i%2!=0为奇数
参考代码如下:
package com.day3;
public class EvenSum {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while(i<=100){
if(i%2==0) {
sum +=i;
}
i++;
}
System.out.println(sum);
}
}
用户登陆验证
输入正确账号和密码进行登录 仅容许输入错误三次
代码如下:
package com.day3;
import java.util.Scanner;
public class LoginVerify {
public static void main(String[] args) {
System.out.println("****************用户登陆验证**********");
Scanner sc = new Scanner(System.in);
int num = 3;
for(int i = 0; i < 3; i++) {
System.out.println("请输入用户名:");
String userName = sc.next();
System.out.println("请输入密码:");
String pwd = sc.next();
if(userName.equals("jim")&&pwd.equals("123456")) {
System.out.println("欢迎登录MyShopping系统");
break;
}else {
num--;
System.out.println("输入错误!您还有"+num+"次机会");
if(num == 0) {
System.out.println("对不起,您3次均输入错误!");
}
continue;
}
}
}
}
给定一组数据,计算最大值和最小值
代码如下:
package com.day3;
public class Number3 {
public static void main(String[] args) {
//获取一批整数
int [] num = {1,13,45,24,56,34,12};
int max = num[0]; //最大值
int min = num[0]; //最小值
for (int i = 0; i < num.length; i++) {
if(num[i]>max) {
max = num[i];
}if(num[i]<min) {
min = num[i];
}
}
System.out.println("最大值为:"+max+",最小值为:"+min);
}
}
打印100以内 每次递减5的数
package com.day3;
public class NumberPrint {
public static void main(String[] args) {
int num = 100;
while(num>0) {
if(num %20 == 0) {
System.out.println();
}
System.out.print(num+"\t");
num-=5;
}
}
}
输入一个数,计算各个和,如图(*****):
代码如下:
package com.day3;
import java.util.Scanner;
public class NumberSum {
public static void main(String[] args) {
System.out.println("请输入一个值:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i=0,j=num;i<=num;i++,j--){
System.out.println(i+"+"+j+"="+(i+j));
}
}
}
循环判断输入是否继续
代码如下:
package com.day3;
import java.util.Scanner;
public class Project {
public static void main(String[] args) {
System.out.println("教师输入是否合格(合格/不合格)");
Scanner sc = new Scanner(System.in);
String flag;
do {
flag = sc.next();
if(flag.equals("合格")) {
break;
}else {
System.out.println("继续编写");
System.out.println("教师输入是否合格(合格/不合格)");
flag = sc.next();
if(flag.equals("合格")) {
break;
}
}
} while (true);
}
}
输入班级人数和学生人数,求成绩大于80的学生人数和所占比例
代码如下:
package com.day3;
import java.util.Scanner;
public class Score {
public static void main(String[] args) {
//输入学生信息
System.out.println("输入班级总人数:");
Scanner sc = new Scanner(System.in);
int classSum = sc.nextInt();
int num = 0;
double sumscore = 0;
int scoreUp = 0;
for(int i = 1;i<=classSum;i++) {
System.out.println("请输入第"+i+"位学生的成绩");
double score = sc.nextDouble();
if(score>=80) {
num++;
}else {
continue;
}
}
System.out.println("80分以上的学生所占比例为:"+(((double)num /classSum)*100)+"%");
System.out.println("80分以上的学生人数是:"+num);
}
}
商品消费单打印
代码如下:
package com.day3;
import java.util.Scanner;
public class ShoppingList {
public static void main(String[] args) {
//打印消费单
System.out.println("****************************************");
System.out.println("请选择购买的商品编号:");
System.out.println("1.T恤\t\t2.网球鞋\t\t3.网球拍");
System.out.println("******************************************");
Scanner sc = new Scanner(System.in);
int id ,num;
double Tsum = 0,Psum = 0,P_sum = 0;
while(true) {
System.out.println("请输入商品编号:");
id = sc.nextInt();
System.out.println("请输入购买数量:");
num = sc.nextInt();
System.out.println("是否继续(y/n):");
String flag = sc.next();
if(flag.equals("y")) {
switch(id) {
case 1:
//T恤
int Tprice = 245;
Tsum = Tprice*num;
System.out.println("T恤\t"+"¥"+Tprice+"\t数量"+num+"\t合计¥"+Tsum);
break;
case 2:
//网球鞋
int Pprice = 570 ;
Psum = Pprice*num ;
System.out.println("网球鞋\t"+"¥"+Pprice+"\t数量"+num+"\t合计¥"+Psum);
break;
case 3:
//网球拍
int P_price = 320 ;
P_sum = P_price*num ;
System.out.println("网球拍\t"+"¥"+P_price+"\t数量"+num+"\t合计¥"+P_sum);
break;
}
}else {
double rebate = 0.8;
System.out.println("输入实际交费:");
double payment = sc.nextDouble();
System.out.println("折扣: "+rebate*10+"折");
double sumPay = (Psum+Tsum+P_sum)*rebate;
System.out.println("消费总金额:"+sumPay);
System.out.println("实际交费:"+"¥"+payment);
System.out.println("找钱:"+"¥"+(payment - sumPay));
break;
}
}
}
}
通过循环判断是否要继续购物
代码如下:
package com.day3;
import java.util.Scanner;
public class ShopSettle {
public static void main(String[] args) {
System.out.println("MyShopping管理系统 > 购物结算");
System.out.println("************************************");
ifContinue();
System.out.println("是否继续(y/n)");
Scanner sc = new Scanner(System.in);
String flag = sc.next();
if(flag.equals("n")) {
System.out.println("程序结束!");
}else {
while(true) {
if(flag.equals("y")) {
ifContinue();
break;
}else {
System.out.println("程序结束!");
break;
}
}
}
}
public static void ifContinue() {
System.out.println("请选择要购买的商品编号:");
System.out.println("1.T恤\t\t2.网球鞋\t\t3.网球拍");
Scanner sc = new Scanner(System.in);
System.out.println("请输入商品编号:");
int id = sc.nextInt();
switch (id) {
case 1:
System.out.println("T恤\t\t¥245.0元");
break;
case 2 :
System.out.println("网球鞋\t\t¥570.0元");
break;
default:
System.out.println("网球拍\t\t¥320.0元");
break;
}
}
}
计算学生增长量 到多少年达到100万(*****)
package com.day3;
public class StudentGrowth {
public static void main(String[] args) {
//先给定年度和增长人数
int year = 2012;
double stuNum = 250000;
int i=0;
while(true) {
i++;
stuNum = stuNum*1.25;
if(stuNum>=1000000) {
System.out.println("到"+(year+i)+"年,达到100万");
break;
}
}
}
}
摄氏度和华氏度转换表 摄氏度达到250 结束(*****)
代码如下:
package com.day3;
public class Temper {
public static void main(String[] args) {
//温度转化
double temp = 0;
double h_temp = 0;
do {
if(temp % 20 == 0) {
System.out.println();
}
System.out.print("摄氏温度:"+temp+"℃\t");
h_temp = temp*9/5.0+32;
System.out.print("华氏温度:"+h_temp+"℉\t");
temp++;
} while (temp<=250);
}
}