使用C#将if条件转换为三元

问题描述

我想使用三元运算符来分解是否满足条件。

我知道此过程:structure(list(Period = c("2016-2017","2017-2018","2018-2019","2019-2020","2020-2021","2021-2022","2022-2023","2023-2024","2024-2025","2025-2026","2026-2027","2027-2028","2028-2029","2029-2030","2030-2031","2016-2017","2030-2031"),City = c("Adelaide","Adelaide","Brisbane","Canberra","Darwin","Hobart","Melbourne","Perth","Sydney","Sydney"),`Growth_Rate` = c(2.51626610011951,2.55164820931287,2.57657088727502,2.61958997722096,2.64870864204937,2.66803039158387,2.68123985996072,2.69161469161469,2.71284349187497,2.72003363906336,2.71964386225247,2.72484993399587,2.72773561574085,2.72847432024169,2.7272309530374,2.85954484097852,2.87789660293085,2.89473978672694,2.90356257340467,2.91463234206244,2.92245132670665,2.93225581163324,2.9311130281383,2.93051331067019,2.92850281322904,2.92517732387606,2.92149192120694,2.91156234267495,2.89441500203832,2.88034865293185,2.88832690003993,2.92367399741268,2.92860734037205,2.9551837831237,2.95338631241846,2.94930875576037,2.96553267681289,2.96706879686991,2.98712265146717,2.99272317310649,2.99532291770325,2.98550724637681,2.96463082840792,2.97949886104784,2.97292514599186,1.28352176525206,1.27804141501294,1.25658910601139,1.2515118052269,1.24642949883147,1.24134393434214,1.25652328114708,1.24093069802352,1.24054762022439,1.23511033001367,1.22968606838019,1.22427591463415,1.2282930961457,1.24128312412831,1.22606419617027,1.76262396187406,1.79407713498623,1.82334833057068,1.85382059800664,1.88857720660187,1.92400038415981,1.94735850241849,1.97177891428924,1.99407819203577,2.00545055986729,2.02700740525628,2.0465089801611,2.05846256833649,2.06613828915004,2.05376747175066,2.29848866498741,2.34602099791389,2.39917131687105,2.4506444770762,2.49394827366544,2.54203051679667,2.60023638512592,2.62885836656328,2.65649733774644,2.67466636761243,2.69783190431981,2.72002127093858,2.73859135971838,2.73865611851553,2.74167443229192,2.5951334823608,2.58959653384758,2.6145545980519,2.63834039111196,2.65164684885149,2.67572876916477,2.68736805066854,2.70698590825374,2.71070520038367,2.7284908035243,2.73111734714043,2.73353339489074,2.73382642074712,2.73033810261551,2.72519205862002,1.61106690334823,1.67106420404573,1.73128880883538,1.78452864913977,1.83220608599123,1.88240318266616,1.93255898606837,1.9727515718166,2.00674929960098,2.03801379482538,2.06867722925715,2.09867047577189,2.11814659726842,2.13361181103117,2.1471989794004)),row.names = c(NA,-120L),class = "data.frame")```

我具有以下if条件:

package theContest;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;

public class theContest {

    // The main() method
    public static void main(String[] args) throws FileNotFoundException {

        //
        String fileName = "contest.csv";
        File file = new File(fileName);
        if (!file.isFile()) {
            System.err.println("Cannot open file: " + fileName + ".");
            System.exit(0);
        }

        
        //
        int numContest = 0;
        Scanner input = new Scanner(file);
        while (input.hasNext()) {
            input.nextLine();
            numContest++;
        }
        input.close();
        System.out.println("Total of " + numContest + " contestants.");
        
        //
        int winner = 0;
        Random random = new Random();
        winner = random.nextInt(numContest) + 1;
        System.out.println("The winner is contestant number " + winner + ".");
        
        //
        String winnerDetails = "";
        input = new Scanner(file);
        for (int lineCount = 0; lineCount < winner; lineCount++) {
            winnerDetails = input.nextLine();
        }
        input.close();
        System.out.println("Winner is: " + winnerDetails);
        
        //
        String id = "";
        String name = "";
        String seats = "";
        String trans = "";
        String rate = "";
        String price = "";

        input = new Scanner(winnerDetails);
        input.useDelimiter(",");
        id = input.next();
        name = input.next();
        seats = input.next();
        trans = input.next();
        rate = input.next();
        price = input.next();
        input.close();
        System.out.println("Details are:");
        System.out.printf("%-5s : %s\n","ID",id);
        System.out.printf("%-5s : %s\n","Name",name);
        System.out.printf("%-5s : %s\n","Seating",seats};
        System.out.printf("%-5s : %s\n","Transfer",trans};
        System.out.printf("%-5s : %s\n","Rate",rate};
        System.out.printf("%-5s : %s\n","Price",price};
    }
}

我想写:

condition ? consequent : alternative

作为替代我必须写些什么?

解决方法

如果要检查多个字段,只需创建一个小的辅助方法即可根据条件(布尔值)返回字符串(或为空):

➜ az acr repository delete --name MyRegistry --image Myrepository:Mytag
This operation will delete the manifest 'sha256:c88ac1f98fce390f5ae6c56b1d749721d9a51a5eb4396fbc25f11561817ed1b8' and all the following images: 'Myrepository:Mytag'.
Are you sure you want to continue? (y/n): y
➜
,

如果您只想在条件为真的情况下更改值(否则就不要更改),则不要添加任何内容作为“替代”部分:

messageErreur = !myField.IsNullOrNa() ? messageErreur + myField.IsDecimalWithMessage(nameof(myField)) : messageErreur;