很简单的JAVA题,急等,谢谢!~

来源:百度知道 编辑:UC知道 时间:2024/07/06 18:05:38
打Taxi的价格是,起租价7元,超过3公里后超出的每公里收1.8元。编写一个Java程序,判断给定公里数对应的费用并输出(输入输出用对话框GUI)。另外,要求程序能够处理非法的输入值(小于0的数的处理,就是提示错误)。
import javax.swing.JOptionPane;
public class TaxiPrice {
public static void main(String[] args) {
double z ;
int h;
double fPrice ;
String str= JOptionPane.showInputDialog("请输入你的公里数:");
z=Double.parseDouble(str);
h=(int) z;
if(h-z<0)
str="公里数为:"+z+"是错误的!";
else if(h<=3)
{
fPrice=7.0 ;
str="你的公里数是"+z+"公里数,\n需要的费用是:"+fPrice+"元";
}
else if(h>=3)
{
fPrice=7.0+1.8*(h-3);
str="你的公里数是"+z+"公里数,\n需要的费用是:"+fPrice+"元";
}
JOptionPane.showMessageDialog(null, str);
System.exit(0);
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
/**
* @author Otacon
*/

public class Price extends JFrame implements ActionListener{
private JLabel disLabel;
private JTextField disField;
private JButton calButton;

public Price(){
super("Enter distance");

disLabel = new JLabel("Enter distance in kilo-meter: ");
disField = new JTextField(20);
disField.addActionListener(this);
calButton = new JButton("Show Price");
calButton.addActionListener(this);

this.setLayout(new FlowLayout());
this.add(disLabel);
this.add(disField);
this.add(calButton);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300,120);
this.setLocation(100,100);
this.setVisible(true);

System.out.println("Program Started");
}