首页 > 代码库 > 分支-09. 分段计算居民水费(求助)
分支-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)); }}
分支-09. 分段计算居民水费(求助)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。