Java练习1

Java练习1简易收货:importjava.util.Scanner;publicclassExcise01{ Stringname; doubleprice; intnumber; publicdoubletShirt(){

练习一:

简易收货:

Java练习1

import java.util.Scanner;
public class Excise01 {
	String name;
	double price;
	int number;
	public double tShirt(){
		this.name="T恤";
		this.price=289.9;
		this.number=2;
		double sum1=price*number;
		System.out.println(name+"\t"+"$"+ price+"\t"+ number+"\t"+ sum1 );
		return sum1;
	}
	public double wShoes(){
		this.name="球鞋";
		this.price=245;
		this.number=1;
		double sum2=price*number;
		System.out.println(name+"\t"+"$"+ price+"\t"+ number+"\t"+ sum2 );
		return sum2;
	}
	public double wRacket(){
		this.name="球拍";
		this.price=468;
		this.number=1;
		double sum3=price*number;
		System.out.println(name+"\t"+"$"+ price+"\t"+ number+"\t"+ sum3 );
		return sum3;
	}
	public static void main(String[] args) {
		Excise01 bo =new Excise01();
		System.out.println("**********消费单**********");
		System.out.println("购买物品\t单价\t个数\t总金额");
		double sum1=bo.tShirt();
		double sum2=bo.wShoes();
		double sum3=bo.wRacket();
		double ssum=sum1+sum2+sum3;
		System.out.println("折扣前价格:"+ssum);
		Scanner input=new Scanner(System.in);
		System.out.println("请输入折扣:");
		double discount=input.nextDouble();
		double zsum=ssum*discount;
		System.out.println("折扣后价格:"+zsum);
		Scanner jfei=new Scanner(System.in);
		System.out.println("实际缴费金额:");
		double jfeib=jfei.nextDouble();
		double zling = jfeib-zsum;
		System.out.println("找零:"+zling);
		double jfen = zsum/100;
		int jfen2=(int)jfen;
		System.out.println("本次积分:"+jfen2);
	}

练习二:

会员中奖:

Java练习1

import java.util.Scanner;
public class excise02 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		System.out.println("请输入4位数字的会员号:");
		double members = input.nextDouble();
		double one=Math.floor(members/1000);
		double tow=Math.floor((members%1000)/100);
		double three=Math.floor((members%100)/10);
		double four=Math.floor(members%10);
		double sum=one+tow+three+four;
		if(sum==24){
			System.out.println("恭喜您中奖了"+"(四位会员号相加等于24)!");
		}else{
			System.out.println("很遗憾啊!");
		}
	}

练习三:

简易租车系统之继承练习:

Java练习1

父类

package exciseZucar;
public class CarFather {
	int noum;
	String name;
	double cargo;//载货量
	int passenger;//载客量
	double price;//租用价格
	int number;//可租数量
	public CarFather(int noum,String name , double cargo,int passenger ,double price,int number){
		this.noum = noum;
		this.name = name;
		this.cargo = cargo;
		this.passenger = passenger;
		this.price = price;
		this.number = number;
	}
	public String toString(){
		return  noum + "\t"+name+"\t"+cargo+"\t"+passenger+"\t"+price+"\t"+number;
	}
}


子类1

package exciseZucar;
public class Cargo extends CarFather {
	public Cargo(int noum, String name , double cargo , int passenger , double price, int number){
		super(noum , name , cargo , 0 , price , number);
	}
}


子类2

package exciseZucar;
public class Passenger extends CarFather {
	public Passenger(int noum, String name , double cargo , int passenger , double price, int number){
		super(noum , name , 0 , passenger , price , number);
	}
}

调试:

package exciseZucar;
import java.util.*;
public class Inital {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double weights = 0;//载货量
		int passens = 0;//载客量
		double prices = 0;//租用价格
		CarFather[] arrys = new CarFather[5];
		arrys[0] = new Cargo(1, "轻型5T" , 5 , 0, 500 , 3 );
		arrys[1] = new Cargo(2, "重型20t" , 20 , 0, 1500 , 5 );
		arrys[2] = new Passenger(3, "小客5人" , 0 , 5, 800 , 5 );
		arrys[3] = new Passenger(4, "大客36人" , 0 , 36, 1200 , 2 );
		arrys[4] = new CarFather(5, "皮卡货人" , 3 , 2, 900 , 4 );
		System.out.println("欢迎使用租车系统!!!\n您是否要租车(租车[1]不租车[0])");
		Scanner input = new Scanner(System.in);
		int bo = input.nextInt();
		if(bo==1){
			System.out.println("可租车辆为:\n"+"序号\t" + "名称\t"+ "载货量\t"+"载客量\t" +"价格\t" +"可租车辆");
			for(int i=0;i<arrys.length;i++){
				System.out.println(arrys[i]);
			}
			System.out.println("请输入需要租几辆车:");
			int bnum = input.nextInt();
			for(int j=1;j<=bnum;j++){
				System.out.println("请输入要租的第"+j+"辆车:");
				int bq = input.nextInt();
				weights += arrys[bq-1].cargo;
				passens += arrys[bq-1].passenger;
				prices += arrys[bq-1].price;
			}
			System.out.println("您租的车辆的总载货、载客、价格为:\n"+"总载货\t" + "总载客\t"+ "总价格");
			System.out.println(weights+ "\t" +passens +"\t"+prices);		
		}else{
			System.out.println("我们一直都在,欢迎您下次光临!");
		}		
	}
}

今天的文章Java练习1分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/64139.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注