.Java多分支;if……else…… 每吨货物每公里运费P与运输距离S有关,路途越远,每公里运价越低。两种解决方案

每吨货物每公里运费P与运输距离S有关,路途越远,每公里运价越低。公式如下:
p=10 s<100
8 100<=s<150
7 150<=s<200
6 200<=s<300
5.5 300<=s<500
5 s>=500
如果所付的总运费超过5000元时,再给予九五折优惠,
从键盘输入货物吨数、运输公里数,求应付的运费,要求精确到角、分。


//不用方法
import java.util.Scanner;
class Dome6
{
 public static void main(String[] args){
   Scanner sc=new Sacnner(System.in);
   System.out.println("请输入你要运输的吨数:");
   double x=sc.nextInt();
   System.out.println("请输入你要运输的距离:");
   double s=sc.nextInt(); 
   if(s<100){
        int p=10;
      if(x*p<5000){
               System.out.println("需要的运费为:"+(x*p));
      }else{   System.out.println("需要的运费为:"+(x*p)*0.95); }
   }else if(s>=100&&s<150){
        int p=8;
      if(x*p<5000){
               System.out.println("需要的运费为:"+(x*p));
      }else{   System.out.println("需要的运费为:"+(x*p)*0.95); }


   }else if(s>=150&&s<200){
        int p=7;
      if(x*p<5000){
               System.out.println("需要的运费为:"+(x*p));
      }else{   System.out.println("需要的运费为:"+(x*p)*0.95); }


   }else if(s>=200&&s<300){
        int p=6;
      if(x*p<5000){
              System.out.println("需要的运费为:"+(x*p));
     }else{   System.out.println("需要的运费为:"+(x*p)*0.95); }


   }else if(s>=300&&s<500){
        int p=5.5;
      if(x*p<5000){
              System.out.println("需要的运费为:"+(x*p));
      }else{  System.out.println("需要的运费为:"+(x*p)*0.95); }


   }else if(s>=500){
        int p=5;
      if(x*p<5000){
               System.out.println("需要的运费为:"+(x*p));
      }else{   System.out.println("需要的运费为:"+(x*p)*0.95); }
    }
   }
 }
}


//使用方法后
import java.util.Scanner;
class Dome6
{
 public static void main(String[] args){
   Scanner sc=new Scanner(System.in);
   System.out.println("请输入你要运输的吨数:");
   double x=sc.nextInt();
   System.out.println("请输入你要运输的距离:");
   double s=sc.nextInt(); 
   if(s<100){
        double p=10;      
        Freight(x,p); 
   }else if(s>=100&&s<150){
        double p=8;
        Freight(x,p);
   }else if(s>=150&&s<200){
        double p=7;
        Freight(x,p); 
   }else if(s>=200&&s<300){
        double p=6;
        Freight(x,p); 
   }else if(s>=300&&s<500){
        double p=5.5;
        Freight(x,p);
   }else if(s>=500){
        double p=5;
Freight(x,p);
    }
   }
   public static void Freight(double x,double p){
  if (x*p>5000)
  {
   System.out.println("需要的运费为:"+(x*p)*0.95);
  }  else{
   System.out.println("需要的运费为:"+(x*p));
      } 
   }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值