第二章 判断

2.1铁路买票

package hellow;
import java.awt.SystemTray;
import java.util.Scanner;
public class Main {
        public static void main(String[] args ) {
               // TODO Auto-generated method stub
//初始化
Scanner in = new Scanner(System. in );
//读入投币金额
System. out .println( "请投币:" );
int amount = in .nextInt();
System. out .println( amount >=10);
//打印车票
System. out .println( "****************" );
System. out .println( "**java城际铁路专线**" );
System. out .println( "****无指定座位票****" );
System. out .println( "****票价:10元*****" );
System. out .println( "****************" );
//计算并打印找零
System. out .println( "找零:" +( amount -10));
       }
}

2.2比较double1.0和double  0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1的方法
         System. out .println(Math.abs( b - c )<1e6);
1.代码
               int a =1;
               double b =1.0;
               double c =0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1;
              System. out .println( a == b );   //int10=double 10.0
              System. out .println( b == c );   //double 10.0!=double 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1
              System. out .println( "a=" + a + "b=" + b + "c=" + c );
              System. out .println(Math.abs( b - c )<1e-6);   //比较double 10.0 和double 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1的方法

2.3判断语句
1.if语句
if(total>=amount)
total+=amount+10;
2.if-else语句
if     (total>=amount)
          total+=amount+10;
else     total+=amount-10;
3.if-else嵌套使用
在多级if-else嵌套中,else和最近的if对应
代码:
Scanner in = new Scanner(System. in );
        int a =0;
        int b =0;
        int c =0;
       System. out .println( "请输入三个数字" );
        a = in .nextInt();
        b = in .nextInt();
        c = in .nextInt();
        int max ;
        if ( a > b )
       {
               max = a ;
       }
        else
       {
               max = b ;
       }
        if ( c > max )
       {
               max = c ;
       }
        else
       {
               max = max ;
       }
       System. out .println( "三个数字中,最大的数字是;" + max );
                                                                     }
4.if-else if级联使用
if(exp1)
 st1;
else if(exp2)
 st2;
else if (exp3)
 se3;
5.注释
     5.1//
     5.2ctrl+/
2.3.1switch-case级联的
switch(控制表达式)
     {
          case 常量:语句
          case 常量:语句
          default     语句
          break;
     }
1.代码
       Scanner in = new Scanner(System. in );
        int type =0;
        type = in .nextInt();
        switch ( type )
       {
        case 1:
        case 2:System. out .println( "你好?" );
        break ;
        case 3:System. out .println( "您不要好" );
        break ;
        case 4:System. out .println( "真美" );
        break ;
        case 5:System. out .println( "真漂亮" );
        break ;
        default :System. out .println( "乖" );
        break ;
       }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值