求解java题 有追加 在线等

Prompt for the weight of a parcel and determine and display the delivery charge for the parcel. The delivery charge is calculated based on the following table.

Parcel weight (kg) Cost per kg ($)
Less than 2.5 $3.50 per kg
Between 2.5 kg and 5 kg $2.85 per kg
Greater than 5 kg $2.45 per kg

package math;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pacel {

/**
* Prompt for the weight of a parcel and determine
* and display the delivery charge for the parcel.
* The delivery charge is calculated based on the following table.
*
* Parcel weight (kg) Cost per kg ($)
* Less than 2.5 $3.50 per kg
* Between 2.5 kg and 5 kg $2.85 per kg
* Greater than 5 kg $2.45 per kg
*/
public static double paid(double weight){
if(weight<0)
return -1;
else
if(weight<2.5)
return 3.5*weight;
else
if(weight<=5)
return 2.85*weight;
else
return 2.45*weight;

}
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO 自动生成方法存根
System.out.println("please input the weight:");
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
double weight=Double.parseDouble(in.readLine());
double money_paid=paid(weight);
System.out.println("You should pay :"+money_paid);

}

}追问

能不能简单写
我没学过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
只用if else 和 double就行

追答

这三个是导入的包,用来读输入的。如果你实在是不知道这个,可以用Scanner代替。
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("please input the weight:");
Scanner cin = new Scanner(System.in);
double weight=cin.nextDouble();
double money_paid=paid(weight);
System.out.println("You should pay :"+money_paid);

}
但是使用的时候也需要导入import java.util.Scanner;这个包

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-28
简单点的
import java.io.IOException;
import java.util.Scanner;

public class Pacel {

public static void main(String[] args) throws NumberFormatException,
IOException {

System.out.print("please input the weight:");

double weight = new Scanner(System.in).nextDouble();

double money = 0;

if (weight < 0) {
money = 0;
} else if (weight < 2.5) {
money = 3.5 * weight;
} else if (weight <= 5) {
money = 2.85 * weight;
} else {
money = 2.45 * weight;
}

System.out.println("You should pay :" + money + "$");

}

}

-----------------------测试
please input the weight:10
You should pay :24.5$
第2个回答  2011-10-28
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("Please enter the weight:");//要求输入
double weight = cin.nextDouble();//获得键盘输入
if(weight<2.5){//小于2.5
System.out.println("The price is 3.5/kg.");
}else if(weight>2.5&&weight<5) {//介于2.5和5
System.out.println("The price is 2.85/kg.");
}else{//其他
System.out.println("The price is 2.45/kg.");
}
}
}
第3个回答  2011-10-28
这是什么java题目?public class Money {
public static void main(String[] args)
{

DecimalFormat decFmt = new DecimalFormat("#0.00");

System.out.println(decFmt.format(new Money().getMoney(2.3)));
}

public double getMoney(double f){
if(f<2.5){
return (3.5*f);
}else if(f<=5&&f>=2.5){
return (2.85*f);
}else{
return (2.45*f);
}

}
}追问

设置包裹的重量并且顯示郵寄費用。郵寄費計算表如下:
重量(kg) 每kg價格
小於2.5kg $3.5/kg
2.5-5kg $2.85/kg
5kg以上 $2.45/kg

第4个回答  2011-10-28
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class DeliveryCalculator {
public static void main(String[] args) {
System.out.println("Please input the weight(kg) of a parcel:");
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String str = input.readLine();
double weight = Double.parseDouble(str);
double deliveryCharge;
if(weight < 2.5){
deliveryCharge = weight * 3.50;
}else if(weight > 5){
deliveryCharge = weight * 2.45;
}else{
deliveryCharge = weight * 2.85;
}
NumberFormat nFormat =new DecimalFormat("#.##");
System.out.println("The delivery charge is :"+nFormat.format(deliveryCharge));
} catch (Exception e) {
e.printStackTrace();
}
}
}