当前位置: 代码迷 >> Java相关 >> java范例练习
  详细解决方案

java范例练习

热度:33   发布时间:2016-04-22 20:52:52.0
java实例练习

1.不使用中间变量交换两个数

public class Exchange {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("请输入一个整数:");        Long num1 = scanner.nextLong();        System.out.println("请输入一个整数:");        Long num2 = scanner.nextLong();        System.out.println("交换前num1:"+num1+"     交换前num2:"+num2 );        num1 = num1^num2;        num2 = num2^num1;        num1 = num1^num2;        System.out.println("交换后num1:"+num1+"     交换后num2:"+num2 );    }}
Exchange

2.利用三目运算符判断奇偶性

public class ParityCheck {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("请输入一个整数:");        Long num = scanner.nextLong();        String s = (num%2==0)?"这个数是:偶数":"这个数是:奇数";        System.out.println(s);    }}
ParityCheck

 

  相关解决方案