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 8824485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:38:46+00:00 2026-06-14T06:38:46+00:00

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GameEdition3 { class Program {

  • 0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameEdition3
{
class Program
{
    static void Main(string[] args)
    {

        Data classData = new Data();

        Console.WriteLine("{0}", classData.TitleMenu());

        while (true) 
        {
            ConsoleKeyInfo PressKey;
            PressKey = Console.ReadKey();

            while (PressKey.Key == ConsoleKey.Backspace)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.TitleMenu());
                PressKey  = Console.ReadKey();
            }



            while (PressKey.Key == ConsoleKey.I)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.Information());
                PressKey =Console.ReadKey();
            }

            while (PressKey.Key == ConsoleKey.D1)
            {
                Console.Clear();
                Console.Write("Please type in the Item you want. Warhammer, Heavy Armor, Boots, or Sword: ");
                Console.WriteLine("{0}", classData.myFunction());
                Console.WriteLine("\nPress Backspace to go back to the menu and you can view your item in the Display all Items tab");
                PressKey = Console.ReadKey();
            }

            while (PressKey.Key == ConsoleKey.D2)
            {
                Console.Clear();
                Console.WriteLine("{0}", classData.result);
                PressKey = Console.ReadKey();


            }

            while (PressKey.Key == ConsoleKey.D3)
            {
                Console.Clear();
                PressKey = Console.ReadKey();

            }


            if (PressKey.Key == ConsoleKey.D4)
            {
                return;

            }

            Console.Read();
        }
    }


    }
}

**/

Hello. I made this noob game where you press a key to get a different menu and set of directions up. The problem is, once I press a key and go to that specific method, I can’t go back. I would like PressedKey to keep looping so I can press keys to go to the different parts of the program. I hope I explained this good enough.

Example: I press the key I. Key I goes to the set of directions. I need to hit backspace to go to the Main menu. Backspace doesn’t work. How do I make these ConsoleKeys to work?

PS: I tried while, if statements, do while, and while(true).

Here is the class that comes with it if someone wants to try it out:
http://pastebin.com/GivANrwC
Name the class Data.cs.

Thanks.

  • 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-14T06:38:47+00:00Added an answer on June 14, 2026 at 6:38 am

    One problem is the line Console.Read(); at the end of the loop. This tells the program to wait for a line of text to be entered, followed by the Enter key. The first letter of the text that was typed is not saved anywhere and is lost.

    After removing that line, you still call Console.ReadKey() too much. Each time you call that method, it eats the key that is typed. So if, for example, you press ‘I’, then you enter this section:

        while (PressKey.Key == ConsoleKey.I)
        {
            Console.Clear();
            Console.WriteLine("{0}", classData.Information());
            PressKey =Console.ReadKey();
        }
    

    After the WriteLine, it calls ReadKey(), and if it’s not ConsoleKey.I, it exits the small loop and then restarts the bigger loop. But at the beginning of the bigger loop, you call ReadKey() again, without ever checking the value of the last key pressed.

    Try this version instead:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GameEdition3
    {
    class Program
    {
        static void Main(string[] args)
        {
    
            Data classData = new Data();
    
            Console.WriteLine("{0}", classData.TitleMenu());
    
            while (true) 
            {
                ConsoleKeyInfo PressKey;
                PressKey = Console.ReadKey();
    
                if (PressKey.Key == ConsoleKey.Backspace)
                {
                    Console.Clear();
                    Console.WriteLine("{0}", classData.TitleMenu());
                }
                else if (PressKey.Key == ConsoleKey.I)
                {
                    Console.Clear();
                    Console.WriteLine("{0}", classData.Information());
                }
                else if (PressKey.Key == ConsoleKey.D1)
                {
                    Console.Clear();
                    Console.Write("Please type in the Item you want. Warhammer, Heavy Armor, Boots, or Sword: ");
                    Console.WriteLine("{0}", classData.myFunction());
                    Console.WriteLine("\nPress Backspace to go back to the menu and you can view your item in the Display all Items tab");
                }
                else if (PressKey.Key == ConsoleKey.D2)
                {
                    Console.Clear();
                    Console.WriteLine("{0}", classData.result);
                }
                else if (PressKey.Key == ConsoleKey.D3)
                {
                    Console.Clear();
                }
                else if (PressKey.Key == ConsoleKey.D4)
                {
                    return;
                }
            }
        }
    
    
    }
    }
    

    This also shows that there’s no need to use small while loops when you’re checking which key was pressed. Let all the looping be done by the outer loop.

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

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Diagnostics { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Program { class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication5 { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LearningKeys { class Program {
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Assignment2 { class Program {
C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program

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.