#switch case automation (?)
54 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @near river! Please use
/closeor theClose Postbutton 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.
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
am i allowed to upload here things?
i could put the thing here in .txt or .java
@vocal moss
You can paste a code block
```java
My Java code here
```
Or regular text
```
My text here
```
Hi
Eh close enough ๐ฑ
im a beginner
in coding
on discord ๐
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]++;
}
?
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
So we randomly populate the array with various ticket types and increment the relevant counter (int array position)
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) {}
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
Ok well in the mean time...
to achieve that aim in this form of solution of the task
Can you not just do dailyVisitors[r]++?
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
Can you handle the special case as an if
Else dailyVisitors[r]++ ?
If r == 1 do this special thing otherwise the normal thing
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?
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]++;
}