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

  • Home
  • SEARCH
  • 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 6991851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:33:30+00:00 2026-05-27T19:33:30+00:00

This simple program asks for the number of players,their names and (in the last

  • 0

This simple program asks for the number of players,their names and (in the last for) it counts their score.But I dont know why it doen’t go to the next player.Look at my code:

    Scanner scanner = new Scanner(System.in);
    HashMap<String,Integer> players= new HashMap<String,Integer>();

    System.out.printf("Give the number of the players: ");
    int numOfPlayers = scanner.nextInt();

    for(int k=1;k<=numOfPlayers;k++)
    {
        System.out.printf("Give the name of player %d: ",k);
        String nameOfPlayer= scanner.next();
        players.put(nameOfPlayer,0);//score=0
    }

    //This for finally returns the score 
    for(String name:players.keySet())
    { 
      int k=1;
      do{ 
          System.out.println("Name of player in this round: "+name);
           System.out.printf("Give me your word: ");
           String nameOfWord= scanner.next();

          //::::::::::::::::::::::
          //::::::::::::::::::::::

          int score=players.get(name)+10;
          //This will update the corresponding entry in HashMap
          players.put(name,score);
          System.out.println("The Player "+name+" has "+players.get(name)+" points ");
    }while(k>0);
}
}  }

This returns:

 Give the number of the players: 2
 Give the name of player 1: A
 Give the name of player 2: B
 Name of player in this round: A
 Give me your word: ABC
 The Player A has 10 points 
 Name of player in this round: A
 Give me your word:......
........

but i want after it counts the score for ‘A’ to count the score for ‘B’.How can i do this?What am i doing wrong?

  • 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-05-27T19:33:31+00:00Added an answer on May 27, 2026 at 7:33 pm

    You don’t change the variable k anywhere in your do ... while loop (which is the only variable contained in the loop condition) – meaning that it will loop forever with the first player!

    If I’m interpreting correctly what you want to do, you should completely remove the do { and the } while (k>0) lines – you don’t even reference the value of k anywhere inside the loop, except for in the condition; is it maybe a remainder of iterating over the players in another way?

    Or, if it should have been the loop for the doing the game rounds (one round being the collection of one turn for each player), then it’s in the wrong position – it should be around the for ... loop iterating over the players, not inside it; you’d of course also have to come up with a proper condition for the game to be over – if it really depends on the k variable you’re currently using, then this variable would also have to change somwhere, or your back to a neverending loop!

    But it’s hard to interpret your code without more information; please update your question with more information on how a full round should look like in your game, and what the condition is that the game is over!

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

Sidebar

Related Questions

I'm trying to get this simple program to work on windows, but it crashes:
I've created this simple example program, but this doesn't work. it won't connect. any
I was making this very simple lex program (just an introductory program). But on
I've this simple program and need to know on which basis should I choose
I ran this simple program, but when I convert from int to double ,
I wrote this simple program today, but I found that cin.get() refuses to work
So I was writing this simple program to calculate the day of any date
I'm trying to compile this simple program to start learning how to use timers:
This is my simple program in Java: public class Counter extends Thread { public
Trying to make a simple program to catalogue books. Something like this, for example:

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.