Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:10:57+00:00 2026-06-12T13:10:57+00:00

This is for a yahtzee game and this method is supposed to compute and

  • 0

This is for a yahtzee game and this method is supposed to compute and return the value of the five dices. But is this a dumb way of doing this, having a switch statement that get passed by parameter which category the user has selected and then a for loop for each possible category. Is there maybe an much easier way of doing this, than my design idea?

private int assignScoreToCategory(int category)
{
    int computedScore = 0;
    println("dice: "+dice1+" "+dice2+" "+dice3+" "+dice4+" "+dice5);

    //  Switches on the category the user has selected.
    switch (category)
    {
    case ONES: 
        for (int i = 0; i < 4; i++){
            if (diceArray[i] == ONES){
                println(computedScore);
                computedScore++;
            }
        }break;

    case TWOS: 



        break;
    case THREES: break;
    case FOURS: break;
    case FIVES: break;
    case SIXES: println("cat 6");   break;
    case UPPER_SCORE: break;
    case UPPER_BONUS: break;
    case THREE_OF_A_KIND: break;
    case FOUR_OF_A_KIND: break;
    case FULL_HOUSE: break;
    case SMALL_STRAIGHT: break;
    case YAHTZEE: break;
    case CHANCE: break;
    case LOWER_SCORE: break;
    case TOTAL: break;
    default: return 0; 
    }
    return computedScore;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:10:58+00:00Added an answer on June 12, 2026 at 1:10 pm

    You should most definitely employ enum instead of int. Then, as a further improvement, move the logic contained in the switch statement into an instance method of your enum. Then you can replace the switch by just a single method call. There is nice syntax to provide a different implementation for every enum member. Combine this idea with a single common method implementation that covers as much ground as possible, possibly with the help of some instance variables:

    enum Category {
      private final int score;
      ... 
      ONES(1) { 
        public int score() { 
          int computedScore = 0;
          for (int i = 0; i < 4; i++) {
           if (diceArray[i] == ONES) {
              println(computedScore);
              computedScore++;
           }
          }
          return computedScore;
        }
      },
      ...,
      SIXES(6),
      ...;
    
      private Category(int score) { this.score = score; }
    
      public int score() { return this.score; }
    }
    

    I would like to give you a fuller example, but you don’t provide much concrete logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This works. $(document).ready(function(){ $(.items article).click(function(){ window.location=$(this).find(a).attr(href); return false; }); }); However , when the
I am trying to sort a multidimensional array, but am not sure if this
This might be a bad idea but just wondering? ;with Products AS ( /*
This gives error. Any other method to extract elements from a multi dimensional array
This might seem as very stupid question but i can't find anywhere answer to
This has been asked several times, i know, but help me understand something. You
This question is related to hooking with Mobile Substrate, but as long as you
This has been posted, but I've tried lot of solutions found on SO and
This is a pretty stale question but frankly I'm yet to find it answered
This is in regards to merge XCode project.pbxproj files, but the same principle applies

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.