分支-09. 分段计算居民水费(求助)

为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费y(元)与月用水量x(吨)相关:当x不超过15吨时,y=4x/3;超过后,y=2.5x-17.5。请编写程序实现水费的计算。

输入格式:输入在一行中给出非负实数x。

输出格式:在一行输出应交的水费,精确到小数点后2位。

输入样例1:12
输出样例1:16.00
输入样例2:16
输出样例2:22.50

import java.text.DecimalFormat;
import java.util.Scanner;

public class IO_1009 {
    public static void main(String[] args) {
        int x;
        double y;

        DecimalFormat df = new DecimalFormat("0.00");
        Scanner input = new Scanner(System.in);
        x=input.nextInt();
        if (x < 15)
            y = (double)(x) * 4 / 3;
        else
            y = 2.5 * (double)(x) - 17.5;

        System.out.println(df.format(y));
        input.close();
    }

}
import java.text.DecimalFormat;
import java.util.Scanner;

public class IO_1009 {
    public static void main(String[] args) {
        double x;
        double y;

        DecimalFormat df = new DecimalFormat("0.00");
        Scanner input = new Scanner(System.in);
        x=input.nextInt();
        if (x < 15)
            y = (x) * 4 / 3;
        else
            y = 2.5 * (x) - 17.5;

        System.out.println(df.format(y));
        input.close();
    }

}

以上两个别人写的都可以通过PAT。

下面是我自己写的却不行,提示“返回非零”,这是为什么呢??谁能告诉我。

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main
{
    public static void main(String[] args)
    {
        double x,y;
    DecimalFormat df = new DecimalFormat("0.00");
        Scanner input = new Scanner(System.in);
        x = input.nextDouble();
      if(x <= 15)
            y = x * 4 / 3;
    else
            y = x * 2.5 - 17.5;
      System.out.print(df.format(y));
    }
}

时间: 2024-08-29 21:02:09

分支-09. 分段计算居民水费(求助)的相关文章

分支-09. 分段计算居民水费

1 /* 2 * Main.c 3 *B9-分支-09. 分段计算居民水费(10) 4 * Created on: 2014年5月28日 5 * Author: Boomkeeper 6 */ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 int main() 12 { 13 float water_consuption=0; 14 float* pw=&water_consuption; 15 16 scanf(&q

分支-09. 分段计算居民水费(10)

#include<iostream>#include<iomanip>using namespace std;int main(){    float x;    cin>>x;    cout<<setiosflags(ios::fixed)<<setprecision(2);    if(x<=15)        cout<<4*x/3<<endl;    else        cout<<2.5

分段计算居民水费 (10 分)

第2章-13 分段计算居民水费 (10 分) 为鼓励居民节约用水,自来水公司采取按用水量阶梯式计价的办法,居民应交水费y(元)与月用水量x(吨)相关:当x不超过15吨时,y=4x/3:超过后,y=2.5x?17.5.请编写程序实现水费的计算. 输入格式: 输入在一行中给出非负实数x. 输出格式: 在一行输出应交的水费,精确到小数点后2位. 输入样例1: 12 输出样例1: 16.00 输入样例2: 16 输出样例2: 22.50 代码 cost = lambda x:4*x/3 if x<=15

PHP脚本的执行时间如何分段计算出来

很多时候我们需要计算PHP脚本的执行时间,来获知脚本的效率等问题.比如有一个一大段的PHP脚本,我们就需要一个分段获取脚本执行时间的方法.先介绍要用到的函数: // 计时函数 function runtime($mode = 0) { static $t; if(!$mode) { $t = microtime(); return; } $t1 = microtime(); list($m0,$s0) = split(" ",$t); list($m1,$s1) = split(&qu

分段计算PHP脚本的执行时间

很多时候我们需要计算PHP脚本的执行时间,来获知脚本的效率等问题.比如有一个一大段的PHP脚本,我们就需要一个分段获取脚本执行时间的方法.先介绍要用到的函数: // 计时函数 function runtime($mode = 0) { static $t; if(!$mode) { $t = microtime(); return; } $t1 = microtime(); list($m0,$s0) = split(" ",$t); list($m1,$s1) = split(&qu

Python基础(2)分段计算利润

lirun=int(raw_input('input'))if lirun <=10:    jiangjin=lirun * 0.1    print 'your jinagjin is',jiangjinelif (lirun> 10 )and (lirun <=20):    jiangjin=10*0.1+(lirun-10)*0.075    print jiangjinelif (lirun>20) and (lirun <=40):    jiangjin=0.

第一次过程性考核——结构化程序设计

码云仓库的地址:https://gitee.com/wslgx/codes 7-1 Hello World! (5 分) 本题要求编写程序,输出一个短句"Hello World!". 输入格式: 本题目没有输入. 输出格式: 在一行中输出短句"Hello World!". 程序的设计思路:直接在输出代码里填上所要输出的内容. 知识点:System.out.println("要输出的内容"):. 运行结果: 7-2 求1到100的和 (10 分)

第一次过程性考核

本次考核共四道题 一:Hello World! 码云地址:https://gitee.com/xjw-xjw/codes/a24x63tqmlc1d9ihyrv0g16 运行结果      : 知识点           :输出基本数据型:System.out.println(): 二:求1到100的和. 本题要求编写程序,计算表达式 1 + 2 + 3 + ... + 100 的值. 码云地址:https://gitee.com/xjw-xjw/codes/tel7b3xgdjk1pr8q9n

第一次过程性考核-邹彤

1.Hello World! https://gitee.com/ztsxxny/codes/kradjqwlb2gopvfn6ch8559 解析:简单的输出程序,注意语法即可. 2.求1到100的和 https://gitee.com/ztsxxny/codes/k8v0nxi3s9czow4mhre7q65 解析:利用while实现1到100的累加 public class Main{  public static void main(String[] args){    int i=1;