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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確嵌套循環(huán)

如何正確嵌套循環(huán)

長風(fēng)秋雁 2022-09-01 16:43:42
我很難弄清楚為什么當(dāng)我運行我的代碼時,它會跳過所有的if語句,只是把無效。我有一個2d數(shù)組,其值表示電影院,我應(yīng)該出售門票,用戶應(yīng)該輸入一筆錢來確定他們將坐在哪里,這是他們從選擇中可以負擔(dān)得起的最昂貴的座位。該人獲得的座位應(yīng)從任何數(shù)字更改為0。最后,我需要打印帶有零的新數(shù)組。這是我嘗試過的:public static void main(String[] args) {    Scanner in = new Scanner (System.in);    boolean done = false;        //initial seating chart    int [] [] table =         {            {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},            {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},            {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},            {20, 20, 20, 20, 20, 20, 20, 20, 20, 20},            {20, 20, 20, 20, 20, 20, 20, 20, 20, 20},            {30, 30, 30, 30, 30, 30, 30, 30, 30, 30},            {40, 40, 40, 40, 40, 40, 40, 40, 40, 40},            {40, 40, 40, 40, 40, 40, 40, 40, 40, 40},            {50, 50, 50, 50, 50, 50, 50, 50, 50, 50},        };    while (!done) {        int row; int col;        //search seating chart        for (row=0; row<9; row++) {            for (col=0; col<8; col++) {                System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");                int amount = in.nextInt();                if (table[row][col]==10 && 10<=amount && amount<20) {                table[row][col]=0;                    System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row+1, col+1);                    System.out.print("Would you like to purchase additional tickets? (Y/N) " + in.next());                    if (in.hasNext("y")) {                        done = false;                    }                    else {done = true;                    }                    }
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經(jīng)驗 獲得超5個贊

這是一個更好地理解的示例,我在這里涵蓋了一個案例,因為其余的都相當(dāng)相同


//private variable here so we could find and book seat through method findAvailableSeat()

private static int[][] table = { { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },

        { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 },

        { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 }, { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30 },

        { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 }, { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 },

        { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50 }, };

public static void main(String[] args) {


    Scanner in = new Scanner(System.in);

    boolean done = false;

    // initial seating chart


    while (!done) {

        int row;

        int col;

        // search seating chart

        System.out.printf("Enter maximum amount that you would like to spend on the tickets: ");

        int amount = in.nextInt();

        if (10 <= amount && amount < 20) {

            String location = findAvailableSeat(10);//store the indexes in a variable to show the user later

            if(location != null) {

                String[] locationList = location.split(",");

                row = Integer.parseInt(locationList[0]);

                col = Integer.parseInt(locationList[1]);

                System.out.printf("Ticket located at Row %d Seat %d purchased for 10\n", row + 1, col + 1);

                System.out.print("Would you like to purchase additional tickets? (Y/N) ");

                String resp = in.next(); //this statement is needed as sometimes with in.next() rushes to user input without printing text

                if (resp.equals("Y")) {

                    done = false;

                } else {

                    done = true;

                }

            }

            else 

                System.out.printf("No available seat found\n");

        }

    }

}

在方法中,我們有


private static String findAvailableSeat(int i) {

    // TODO Auto-generated method stub

    for (int row=0; row<9; row++) {

        for (int col=0; col<8; col++) {

            if(table[row][col]==i) {

                //find the seat location, set location to 0 and then return indices to show in user result

                table[row][col] = 0;

                return row + "," + col;

            }

        }

    }

    return null;

}


查看完整回答
反對 回復(fù) 2022-09-01
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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