第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

是否可以在 for 或 while 循環(huán)中聲明引用變量?

是否可以在 for 或 while 循環(huán)中聲明引用變量?

小怪獸愛(ài)吃肉 2022-12-15 15:22:47
我寫(xiě)了一段簡(jiǎn)短的代碼,從名為“ChosenCompanies”的類(lèi)中聲明了 6 個(gè)引用變量,以練習(xí) Java 中的類(lèi)和構(gòu)造函數(shù)。它是以下內(nèi)容:public static void main(String[] args) {    String[] FinalCompaniesName = new String[6];    ChosenCompanies com1 = new ChosenCompanies();    ChosenCompanies com2 = new ChosenCompanies();    ChosenCompanies com3 = new ChosenCompanies();    ChosenCompanies com4 = new ChosenCompanies();    ChosenCompanies com5 = new ChosenCompanies();    ChosenCompanies com6 = new ChosenCompanies();    Scanner scanner = new Scanner(System.in);    int choice;    int count = 1;    while(count <= 2) {        switch(count) {        case 1:            System.out.println("Choose one:");            System.out.println("1. " + com1.name);            System.out.println("2. " + com2.name);            System.out.println("3. " + com3.name);            choice = scanner.nextInt();            switch(choice) {            case 1:                FinalCompaniesName[0] = com1.name;                break;            case 2:                FinalCompaniesName[0] = com2.name;                break;            case 3:                FinalCompaniesName[0] = com3.name;                break;            }        break;        case 2:            System.out.println("Choose one:");            System.out.println("1. " + com4.name);            System.out.println("2. " + com5.name);            System.out.println("3. " + com6.name);            choice = scanner.nextInt();            switch(choice) {            case 1:                FinalCompaniesName[1] = com4.name;                break;            case 2:                FinalCompaniesName[1] = com5.name;                break;            case 3:                FinalCompaniesName[1] = com6.name;                break;            }            break;        }            count++;    }        System.out.println("You have chosen: " + FinalCompaniesName[0] + ", " + FinalCompaniesName[1]);}我想知道我是否可以通過(guò)使用 for 或 while 循環(huán)來(lái)最小化上面的代碼量,因?yàn)橐米兞康拿Q(chēng)增加了 1,就像公共 for 語(yǔ)句中的整數(shù)“i”一樣。簡(jiǎn)而言之,是否可以使用循環(huán)來(lái)聲明引用變量?
查看完整描述

2 回答

?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

您的示例令人困惑,所以我不知道您到底想完成什么,但這里有一個(gè)示例,說(shuō)明如何提示用戶(hù)從現(xiàn)有公司名稱(chēng)列表中為每個(gè)公司選擇一個(gè)新名稱(chēng):


    public class Company {


    /**

     * Array of available company names used to construct

     * initial companies. These names are also used as possible

     * choices when changing company names through {@link #changeCompanyNames()}

     */

    private static final String[] COMPANY_NAMES = new String[]

            { "Alphabet", "Microsoft", "IBM", "Amazon", "Oracle", "Apple" };


    /**

     * <p>

     * Array of Company objects initialized with a fixed number of

     * new companies equal to the number of String entries in {@link #COMPANY_NAMES}.

     * </p><p>

     * Each company entry will inheriting a name from the mentioned array

     * in the initialization process done in {@link #initializeCompanies()}

     * </p>

     */

    public static final Company[] COMPANIES = initializeCompanies();


    private String name;


    /**

     * Internal constructor with private access to

     * prevent class construction outside this class

     */

    private Company(String name) {

        this.name = name;

    }


    /**

     * Should only be used internally on class loading to

     * construct an array of companies from a list of company names.

     */

    private static Company[] initializeCompanies() {


        Company[] companies = new Company[COMPANY_NAMES.length];

        for (int i = 0; i < COMPANY_NAMES.length; i++) {

            companies[i] = new Company(COMPANY_NAMES[i]);

        }

        return companies;

    }


    /**

     * Change any or all company names by prompting the user to choose

     * a new name for each company from the list of available companies.

     */

    public static void changeCompanyNames() {


        java.util.Scanner scanner = new java.util.Scanner(System.in);

        /*

         * Create a new array of company names that is identical to the existing

         * array of company names. We will change names here on user input and

         * then update each new company name to values from this array.

         */

        final String[] finalCompanyNames = COMPANY_NAMES.clone();

        /*

         * Iterate through an array of companies with a for-loop

         * accessing and processing each company entry

         */

        for (int i1 = 0; i1 < COMPANIES.length; i1++)

        {

            /* Prompt the user to choose a new company name for the

             * company at index i1 from COMPANIES array.

             */

            System.out.printf("Choose a new company name for %s company:%n", COMPANIES[i1].name);

            /*

             * Again iterate through all companies and print their names to

             * console offering the user a list of possible names to choose from

             */

            for (int i2 = 0; i2 < COMPANIES.length; i2++) {

                System.out.printf("%d. %s%n", i2 + 1, COMPANIES[i2].name);

            }

            /*

             * Get user input and validate it, then either update the array of

             * final names with the new entry or print an error and move the index

             * to the previous position if the input was an invalid number

             */

            int input = scanner.nextInt();

            if (input > 0 && input <= COMPANIES.length) {

                finalCompanyNames[i1] = COMPANY_NAMES[input - 1];

                System.out.println("You have choosen company name " + finalCompanyNames[i1]);

            }

            else {

                System.out.printf("Error: input is not in range (1-%d)%n", COMPANIES.length);

                /*

                 * It's imperative that we move the index to the previous

                 * position so we can iterate over this company entry again

                 */

                i1 -= 1;

            }

        }

        // Print simple line separator

        System.out.println("");


        /* Print each choosen name that is different then the original

         * company name and update the appropriate company name field value

         */

        for (int i = 0; i < finalCompanyNames.length; i++)

        {

            if (!finalCompanyNames[i].equals(COMPANY_NAMES[i])) {

                System.out.printf("Company %s has changed name to %s%n", COMPANY_NAMES[i], finalCompanyNames[i]);

                COMPANIES[i].name = finalCompanyNames[i];

            }

        }

    }

}

用戶(hù)完成選擇后的示例控制臺(tái)輸出:


Company Alphabet has changed name to IBM

Company Microsoft has changed name to Amazon

Company IBM has changed name to Alphabet

Company Amazon has changed name to Oracle

Company Oracle has changed name to Microsoft

Company Apple has changed name to Amazon

在這里,我們可以設(shè)置任意數(shù)量的公司,并讓用戶(hù)為他們選擇任何提供的名稱(chēng)。代碼對(duì)于初學(xué)者來(lái)說(shuō)可能看起來(lái)有點(diǎn)令人生畏,但這實(shí)際上是最簡(jiǎn)單的方法,無(wú)需涉及任何更復(fù)雜的 Java 概念。


請(qǐng)隨時(shí)提出有關(guān)此實(shí)施的任何問(wèn)題。我很樂(lè)意提供幫助。


編輯:更新了代碼以包含詳細(xì)的注釋和更易于理解的更全面的結(jié)構(gòu),并修復(fù)了索引問(wèn)題。


查看完整回答
反對(duì) 回復(fù) 2022-12-15
?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

在 Java 中不可能創(chuàng)建動(dòng)態(tài)變量(但有反射,請(qǐng)參見(jiàn)此處

您可以創(chuàng)建一個(gè)數(shù)組并使用ias 索引,例如chosenCompanies[i].

否則,您可以使用列表或地圖。

編輯:

那看起來(lái)像這樣。除了代碼的意義之外,這個(gè)例子只展示了你將如何使用數(shù)組:

  String[] choosenCompanieNames = new String[2]; // you only store two values, not 6 values


    //lets store the values to chose in arrays

    String[] possibleCompanieNames = new String[6]; // 6 possible values to choose (com1 - com6)

    possibleCompanieNames[0] = "com1";

    possibleCompanieNames[1] = "com2";

    possibleCompanieNames[2] = "com3";

    possibleCompanieNames[3] = "com4";

    possibleCompanieNames[4] = "com5";

    possibleCompanieNames[5] = "com6";


    //I deleted the while loop, as it only has two ways and every way has its own code. 


    Scanner scanner = new Scanner(System.in);

    int choice;


    System.out.println("Choose one:");

    System.out.println("1. " + possibleCompanieNames[0]);

    System.out.println("2. " + possibleCompanieNames[1]);

    System.out.println("3. " + possibleCompanieNames[2]);

    choice = scanner.nextInt();

    choosenCompanieNames[0] = possibleCompanieNames[choice-1]; //you must subtract one, as the array index starts at 0 and ends on 5


    System.out.println("Choose one:");

    System.out.println("1. " + possibleCompanieNames[3]);

    System.out.println("2. " + possibleCompanieNames[4]);

    System.out.println("3. " + possibleCompanieNames[5]);

    choice = scanner.nextInt();

    choosenCompanieNames[1] = possibleCompanieNames[3+choice-1]; //here you want com4, com5 and com6, so you can e.g. add 3 to index and substract one like code above. Or better add only 2, as 3-1=2



    System.out.println("You have chosen: "

         + choosenCompanieNames[0] + ", " + choosenCompanieNames[1]);


查看完整回答
反對(duì) 回復(fù) 2022-12-15
  • 2 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)