判斷一個數是正數還是負數還是0、判斷最大值
阿新 • • 發佈:2021-12-13
判斷一個數是正數還是負數還是0、判斷最大值
package com.guoba.testhello; import java.util.Scanner; public class ZuiDaZhi { public static void main(String[] args) { System.out.println("1.判斷一個數是正數還是負數還是0"); System.out.println("請輸入一個數:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); if (i > 0){ System.out.println("您輸入的數是正數"); }else if (i == 0){ System.out.println("您輸入的數是零"); }else if (i < 0){ System.out.println("您輸入的數是負數"); } System.out.println("2.判斷最大值"); System.out.println("請輸入三個數:"); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); System.out.println("方法一"); //判斷最大值 if (a > b) { if (a > c) { System.out.println("您輸入的最大值是" + a); } else { System.out.println("您輸入的最大值是" + c); } } else { if (b > c) { System.out.println("您輸入的最大值是" + b); } else { System.out.println("您輸入的最大值是" + c); } } System.out.println("方法二"); //方法二 int max = ((a > b) ? ((a > c) ? a: c):((b > c) ? b : c)); System.out.println("您輸入的最大值是"+max); System.out.println("方法三"); System.out.println(((a>b?a:b)>c)?(a>b?a:b):c); } }