【java基础】键盘键入内容

来源:互联网 发布:tophat算法 编辑:程序博客网 时间:2024/06/12 01:29
<span style="font-size:14px;">/*    步骤        1)导入类  import java.util.Scanner;2)创建对象 Scanner sc = new Scanner(System.in);3)接收数据 int x = sc.nextInt*/import java.util.Scanner;class  Dome1{public static void main(String[] args){int x ;Scanner scanner = new Scanner(System.in);System.out.println("请输入X的值");x = scanner.nextInt();   //只能键入int型。System.out.println("X的值是"+x);}}</span>

实例:
 

/*北京出租车计费问题。  规则:   1.起步价13元,包含3公里。   2.每公里2.3元。   3.晚上23(包含)至次日凌晨5点(不包含),每公里加收20%的服务费。   3.每次乘车加收1元钱的燃油附加税。  从控制台输入打车的时间,距离,计算本次打车的费用。*/import java.util.Scanner;class ExericeDemo4{   public static void main(String[] args){       int hours ;//打车的时间       int distance;//打车距离       int startPrice=13;//起步价       double perPrice = 2.3;//每公里的收费,默认是2.3元       double totalPrice =0.0; //总费用              Scanner sc = new Scanner(System.in);       System.out.println("请输入打车的时间(0-23):");       hours = sc.nextInt();       System.out.println("请输入打车的距离:");       distance = sc.nextInt();              //先判断时间是否合法       if(hours<0||hours>=24)       {          System.out.println("时间非法,程序退出....");          System.exit(0); //程序无条件退出       }       //判断每公里收费,是否要多收20%;       if(!(hours>=5&&hours<23))  //表示你白天打车       {          perPrice *=1.2; //每公里加收20%; perPrice = perPrice * 1.2;       }       //再判断你打车有没有超过3公里       if(distance>3)       {          totalPrice = startPrice+(distance-3)*perPrice;       }       else       {          totalPrice = startPrice;       }       //加收一元钱的燃油税       totalPrice++;       System.out.println("您本次打车的总费用是:"+totalPrice+"¥");   }}


0 0