java判断a b c顺序_java判斷三個數字的大小(判斷a,b,c按順序從大到小,從小到大輸出)...

通過鍵盤輸入方式錄入a,b,c三個值,按從大到小(從小到大)順序輸出。

1:剛好學到了if else if語句判斷,通過else if多次判斷來輸出。

2:進行多次判斷,匹配對應的值來輸出,

假設C

用代碼表示為 if (c < a && c < b && b < a){

System.out.println("老大" + a,"老二"+b+"老三"+c);

} else if(比較條件){

}

按這個思路一一判斷過去,得出A B C 三個值的大小。

題目源碼

package work;

import java.util.Scanner;//導包,獲取鍵盤輸入

/**

* 鍵盤輸入A、B、C三個值,按從大到小順序輸出。

* 螞蟻2017/11/29

**/

public class Demo01 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("輸入數字a");

int a = sc.nextInt();

System.out.println("輸入數字b");

int b = sc.nextInt();

System.out.println("輸入數字c");

int c = sc.nextInt();

if (c < a && c < b && b < a) {// 通過比較c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用线程的 wait() 和 notify() 方法来实现按顺序打印ABC的需求。具体实现如下: ```java public class PrintABC { private int times; // 打印次数 private int state; // 当前状态:0-A,1-B,2-C public PrintABC(int times) { this.times = times; this.state = 0; } public synchronized void printA() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 0) { wait(); } System.out.print("A"); state = 1; notifyAll(); } } public synchronized void printB() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 1) { wait(); } System.out.print("B"); state = 2; notifyAll(); } } public synchronized void printC() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 2) { wait(); } System.out.print("C"); state = 0; notifyAll(); } } } ``` 在主函数中创建三个线程,分别调用 PrintABC 类中的 printA()、printB() 和 printC() 方法: ```java public static void main(String[] args) { PrintABC printABC = new PrintABC(10); Thread threadA = new Thread(() -> { try { printABC.printA(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread threadB = new Thread(() -> { try { printABC.printB(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread threadC = new Thread(() -> { try { printABC.printC(); } catch (InterruptedException e) { e.printStackTrace(); } }); threadA.start(); threadB.start(); threadC.start(); } ``` 执行结果: ```java ABCABCABCABCABCABCABCABCABCABC ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值