#switch case automation (?)

54 messages ยท Page 1 of 1 (latest)

junior kernelBOT
#

โŒ› This post has been reserved for your question.

Hey @near river! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

vocal moss
#

Without more details (switch contents, where new entries can come from) it's difficult to make a good recommendation but you can have a map which you update and do a lookup on that instead of a potentially large switch statement

near river
#

am i allowed to upload here things?

#

i could put the thing here in .txt or .java

#

@vocal moss

vocal moss
modest drum
#

Hi

near river
#

@vocal moss awesome thanks

vocal moss
#

Eh close enough ๐Ÿฑ

near river
#

im a beginner
in coding
on discord ๐Ÿ™‚

vocal moss
#

Is it this bit here

switch (r) {
                case 0 -> getDailyVisitors[0]++;
                case 1 -> {getDailyVisitors[1]++;
                    boolean b = rand.nextBoolean();
                    if (b) {
                        getDailyVisitors[0]++;
                        i++;
                    } else {
                        getDailyVisitors[3]++;
                        i++;
                    }}
                case 2 -> getDailyVisitors[2]++;
                case 3 -> getDailyVisitors[3]++;
                case 4 -> getDailyVisitors[4]++;
            }

?

near river
#

yes this is the part what id like to automatize
and update in the .add statement simply insert new values

#

in constants()

#

the idea sound like this might be not possible but thought worth a question

vocal moss
#

So we randomly populate the array with various ticket types and increment the relevant counter (int array position)

near river
#

yes
thank you

#

but than i need to convert to array?

#

or use array?

vocal moss
#

Actually in this case I think what would help you is to use an object and update it accordingly so you don't have to keep bouncing between different arrays

#

Contain all the data in one place

#

Actually a record would be better:

public record Ticket (String id, String name, int dailySold) {}
near river
#

well.. i guess after new year we got to things like that

#

im a student

#

and during lesson came up the question from the teachers side and he told he will check it if possible

vocal moss
#

Ok well in the mean time...

near river
#

to achieve that aim in this form of solution of the task

vocal moss
#

Can you not just do dailyVisitors[r]++?

near river
#

the problem is that one from the many [r] requres a boolean

#

that is the 7 or younger

#

so there but only there i needed to insert an if statement

vocal moss
#

Can you handle the special case as an if

#

Else dailyVisitors[r]++ ?

#

If r == 1 do this special thing otherwise the normal thing

near river
#

can you help me out with a basic example syntax how would that look like please?

#

switch (r){

#

case default

#

case special case

#

}

#

and then i have only two case

#

thats how you meant?

vocal moss
#

a switch with 2 conditions is really an if-else, so:

#
            int r = rand.nextInt(tickets.size()); // ***
            if (r == 1) {
                
                    boolean b = rand.nextBoolean();
                    if (b) {
                        getDailyVisitors[0]++;
                        i++;
                    } else {
                        getDailyVisitors[3]++;
                        i++;
                    }
            } else {
                getDailyVisitors[i]++;
            }
near river
#

yes

#

but if i understand you well above we talk about default ticket type and small kid

#

but i have more

#

meanwhile this switch case suppose to count for more different type of tickets

#

ok, thanks anyway

#

bye

vocal moss
#

sorry was afk

#

so why is r==1 special and lets me increment different counters?

#

i'm sort of confused about the logic