簡體   English   中英

如何在不將它們相乘的情況下驗證 3 個數字的乘積符號?

[英]How can I verify the sign of product of 3 numbers without multiplying them?

我有一個聽起來像這樣的練習:

計算 3 個數字的乘積符號:

  • 讀取 3 個浮點數
  • 打印輸入的 3 個數字的乘積符號:

嘗試在不乘以 3 個數字的情況下執行此操作

我試過這種方式:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

       Scanner in = new Scanner(System.in);

       double a = in.nextDouble();
       double b = in.nextDouble();
       double c = in.nextDouble();


       switch(){
          case a:
          case b:
          case c:
             if (a > 0 && b > 0 && c > 0){
                System.out.printf("positive");
             }else if ( a > 0 && b < 0 && c > 0){
                System.out.printf("negative");
             }else if ( a > 0 && b > 0 && c < 0){
                System.out.printf("negative");
             }else if ( a < 0 && b > 0 && c > 0){
                System.out.printf("negative");
             }else if ( a == 0 && b == 0 && c == 0){
                System.out.printf("zero");
             }
       }


}

}

假設if , else if條件正確,我應該在 switch 的括號中放入什么條件才能使該程序工作?

如果他們不是,我可以用 switch case 做什么才能成功地解決這個練習?

我的輸入是例如:

2
3
-1

我的輸出

negative

考慮到所有 3 個數字都是非零的

如果您有 1 或 3 個負數,則乘積將為負數。 否則,產品將是積極的。

int count = 0;
if(a < 0)
    count++;
if(b < 0)
    count++;
if(c < 0)
    count++;

if(count % 2 == 1)
    System.out.println("Negative");
else
    System.out.println("Positive");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM