Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to print
*
**
***
****
*****
and
*****
****
***
**
*
using 'While' and 'do - while'.
But I have no idea how to approach this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a = 0;
int b = 0;
while (a <= 0)
{
while (b <= a)
{
Console.Write("*");
b++;
}
a++;
Console.WriteLine();
}
Console.ReadLine();
}
}
}
I've kept trying to approach like above, but I think It never works ever!
P.S How would I improve my logic about programming? I feel like I'm lack of thinking logically.
I figure this is a homework problem and I usually don't answer them, but I think this may help you understand how to program better, so let me try explain...
Think through what you're trying to do - You want to print 1 star first and then stop at 5 stars and then print the reverse.
So, firstly, name your variables to make sense:
int numStars = 1;
int maxNumStars = 5;
Next, you can loop something along the lines of:
while( numStars <= maxNumStars) { ... }
Firstly, it lets you understand your problem better, secondly, it becomes readable and debug-able.
Your final procedure can look something as follows:
static void Main(string[] args)
{
int numStars = 1;
int maxNumStars = 5;
// Print the ascending number of stars
while(numStars <= maxNumStars)
{
// Write numStars number of stars to the console using the string constructor:
Console.WriteLine(new string('*', numStars));
numStars++;
}
// Print the descending number of stars
while (numStars >= 1)
{
// Write numStars number of stars to the console using the string constructor:
Console.WriteLine(new string('*', numStars));
numStars--;
}
}
Again, not a fan of doing work for a person, but I hope it makes enough sense to actually help you figure out similar problems like this in the future.
Edit:
For completeness sake, to use loops everywhere, your code / loop could look something like this:
// Declare a variable for writing the stars to the console:
int numWritten;
// Print the ascending number of stars
while(numStars <= maxNumStars)
{
// Reset the number of stars written:
numWritten = 0;
// Write the stars with a loop:
while (++numWritten <= numStars)
Console.Write("*");
// End the line and increment the numStars variable:
Console.WriteLine();
numStars++;
}
This is one of many ways to do it:
class Program
{
static void Main(string[] args)
{
int a = 0;
int b = 0;
int c = 4;
while (a <= c)
{
b = 0;
while (b <= a)
{
Console.Write("*");
b++;
}
a++;
Console.WriteLine();
}
Console.WriteLine();
if (a > c)
{
a--;
while (a >= 0)
{
b = a;
while (b >= 0)
{
Console.Write("*");
b--;
}
a--;
Console.WriteLine();
}
}
Console.ReadLine();
}
}
}
Probably work in this direction:
int a = 0;
int b = 0;
string s = string.Empty;
while (a < 5)
{
while (b <= a)
{
s += "*";
b++;
}
Console.Write(s);
s = string.Empty;
a++;
b = 0;
}
Hope it gives you some idea...
using while loops can be confusing so I came up with this recursive solution (for science)
public static void Main()
{
test();
}
public static void test(string lastOutPut = "",int maxBound = 5,bool goingup = true,int cursor = 0,int maxOutputs = 10){
if(cursor>maxOutputs)return;
Console.Write("\n");
if(goingup){
Console.Write(lastOutPut+="*");
cursor++;
test(lastOutPut,maxBound,lastOutPut.Length <= maxBound,cursor,maxOutputs);
}else{
lastOutPut=lastOutPut.Substring(0,lastOutPut.Length-1);
Console.Write(lastOutPut);
cursor++;
test(lastOutPut,maxBound,lastOutPut.Length <= 0,cursor,maxOutputs);
}
}
Related
A guy in my class made a program to determine the difference between 2 numbers. The way he did it was really ineffective and I told him that. He didn't believe me and wanted me to write better code. I wrote all of the code I need in Visual Studio but every time I try to run the code I get the error message
CS5001: does not contain a static 'main' method suitable for an entry point
I have been looking in the internet for solutions for quite a long time now and I tried a lot out but none of it has worked.
I code in C#.
I will be really thankful if anyone here can help me.
this is my code:
namespace Determine_difference
{
public class Program
{
public static void Main()
{
}
int a, b, c, d;
public void Start()
{
a = 0;
b = 1; //this is the difference between the numbers
c = 1; //c is used to determine how big or small the searched number is. This is kind of unnecessary but I was tired when i wrote this
d = 1; //d is used so that the console doesn't get spammed with the answers
}
public void Update()
{
if (a < b) //this next part only gets executed when the searched number is larger than 0
{
if(b-c > a) //the next part then only gets triggered if the searched number minus c are larger than 0
{
c += 1; //c gets 1 higher
}
else if(b-c==a) //if the searched number minus c are equal to 0 this part gets executed
{
if (d == 1) // then this part gets executed if d is equal to 1, this is so that the console only prints once
{
Console.Write("+"); //the console writes "+"
Console.Write(c); //and the searched number
d = 0; //d is now equal to 0 so that this part of the code doesn't get triggered again
}
}
}
if (a > b)
{
if (b - c < a)
{
c += 1;
}
else if (b + c == a)
{
if (d == 1)
{
Console.Write("+-c");
Console.Write(c);
d = 0;
}
}
}
if (a == b)
{
if (d == 0)
{
Console.WriteLine("+/ -0");
d = 0;
}
}
}
}
}
You have to change your code to this:
public class Program
{
public static int a,b,c,d;
public static void Main()
{
Start();
Update();
}
public static void Start()
{
.... your code
}
public static void Update()
{
.... your code
}
}
and use "dotnet run" inside of a VS Code terminal to execute the program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sisesta suvaline tekst-->");
string tekst1 = Console.ReadLine();
// string tekst2 = ("ja");
char jtaht = ('j');
char ataht = ('a');
int jsidOntekstis = 0;
int asidOnTekstis = 0;
int tekstipikkus = tekst1.Length;
int jasidonTekstis = jsidOntekstis + asidOnTekstis;
int jasidEiOleTekstis=1;
for (int i = 0; i < tekstipikkus; i++)
{
if (tekst1[i] == jtaht)
{
jsidOntekstis++;
}
if (tekst1[i] == ataht)
{
asidOnTekstis++;
}
}
// for (int k = 0; i < tekstipikkus; i++)
{
{
if (jasidonTekstis > jasidEiOleTekstis)
{
Console.Write("Ja on tekstis olemas");
}
else
{
Console.Write("Ja-sid ei ole tekstis");
}
}
}
Console.ReadKey();
}
}
}
So This is my code and it isn't working the way it should. My teacher asked me to search for "ja" in text without contain method so we would think more logically. I completed all other exercises but this one. Thank you!
StackOverflow is actually not a place where people DO something for you.
They help you and tell you HOW to do this. This issue contains only the wrong piece of code and question "what's wrong".
First of all, I need to tell you that the first problem is, obviously, the algorithm.
I can't understand what is your code supposed to do because even you don't understand it.
using System;
using System.Text;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Where to search -->");
string text = Console.ReadLine();
string pattern = "ja"; // Probably, it is better to get it from Console as well
for (int i = 0; i < text.Length; i++)
{
for (int j = 0; j < pattern.Length; j++)
{
if (text[i+j] == pattern[j] && j == pattern.Length - 1)
Console.WriteLine(i);
if (text[i+j] != pattern[j]) break;
}
}
}
}
}
Here is the (not a best) code which searches for the pattern in the text without Contains method. It iterates through the text string - and if it meets the first character of pattern string - it goes further comparing characters one by one in a row. If the inner loop iterated till the end then it means that text string contains pattern string and outputs it's position. If in any moment characters are not equal then the inner loop breaks and continues the main loop.
Research it and understand it. Then you can solve the problem.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How can I write a code in C# to find the sum of letters
If A=0;B=1,C=A+B,D=B+C,E=C+D.....
Example CD=1+2=3,
I have tried this way where input is string and output is sum of letter
using System;
public class Test
{
public static (int output1)
public static void Main(string input1)
{
// your code goes here
}
}
An answer without using dictionary list
class Program
{
static void Main(string[] args)
{
string test = "abcdef";
int sum = 0;
foreach (char c in test)
{
int letterNumber = char.ToUpper(c) - 64;
sum += rtnDegint(letterNumber);
}
Console.WriteLine(sum);
}
int rtnDegint(int n)
{
int first = 0, second = 1, next = 0, c;
for (c = 0; c < n; c++)
{
if (c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
}
return next;
}
}
private void button1_Click(object sender, EventArgs e)
{
String UserNumber = this.textBox1.Text;
int NewUserNumber = Convert.ToInt32(UserNumber);
int result = 0;
int second = 0;
while (NewUserNumber >= 1)
{
result = NewUserNumber * (NewUserNumber - 1);
NewUserNumber--;
}
String i = Convert.ToString(result);
this.textBox2.Text = i;
}
}
While I understand this is homework for me, I am stuck. I really really don't want this solved, I want to do it myself.
I don't understand why it's not working.. It's outputting 2 no matter what I put in.
I could do this in Java easily but the converting gets to me..
Any help would be great.
Your problem is not in the conversion. Please look at
result = NewUserNumber * (NewUserNumber - 1);
while (NewUserNumber > 1)
{
result = result * NewUserNumber;
NewUserNumber--;
}
Initialize result as
int Result=1;
And write for loop as follows
while (NewUserNumber >= 1)
{
result = result * NewUserNumber;
NewUserNumber--;
}
OR
you can even use follwing
public int Factorial(int num)
{
if(num==1)
return 1;
return num * Factorial(num-1);
}
You need to look at your while loop structure and how you are setting the result value outside it and the multiplication
while (NewUserNumber >= 1)
{
result = NewUserNumber * (NewUserNumber - 1);
NewUserNumber--;
}
String i = Convert.ToString(result);
Also you have declared another int second = 0; - dont see why you are declaring that.
My advice is to try running over the code manually with different inputs.
You should run over the loop and try to figure what is the value of result and NewUserNumber is in each iteration.
Good luck...
You should try to run a debugger on your code then step through using the watch window. This is easy to do and will show you exactly what each value is at each point (you set break points). What IDE are you using?
int Factorial(int input)
{
int answer = 0;
if (input > 0)
{
count = 1;
while (count <= input)
{
if (count == 1)
{
answer= 1;
count++;
}
else
{
answer = count * answer;
count++;
}
}
}
else
{
MessageBox.Show("Please enter only a positive integer.");
}
return answer;
}
static void Main(string[] args)
{
int number, fact;
Console.WriteLine("enter the number for geting factorial");
number = Convert.ToInt32(Console.ReadLine());
fact = number;
for (int i = fact - 1; i > 0; i--)
{
fact = fact * i;
}
Console.WriteLine(fact);
Console.ReadLine();
}
i have done coding in C# but not much inside the Console App (teacher is making us do an assignment in it)
I have a problem where my static method works fine the first time it is called (each question is asked), but the second time through the console closes. I need this function to execute 10 times and not sure why it wont. Here is what i have and thanks in advance!:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab2
{
class Program
{
//Create the arrays
static string[] questions = new string[5]; //For questions
static int[] tableHeader = new int[10]; //Table Header
static int[,] responses = new int[5, 10]; //For answers
//Int for the number of times the questions have been asked
static int quizCount = 0;
static int answer;
static bool isGoing = true;
static void Main(string[] args)
{
//Set the questions in an array
questions[0] = "On a scale of 1-10, how do you feel about the drinking age in Wisconsin?";
questions[1] = "On a scale of 1-10, how often do you drink a week?";
questions[2] = "On a scale of 1-10, how important is this class?";
questions[3] = "On a scale of 1-10, how would you rate this campus?";
questions[4] = "On a scale of 1-10, how would you rate this command prompt?";
while(isGoing)
Questions();
}
static void Questions()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(questions[i]);
answer = Convert.ToInt16(Console.ReadLine());
responses[i, quizCount] = answer;
}
if (quizCount < 10)
{
Console.WriteLine("Enter more data? (1=yes, 0=no)");
int again = Console.Read();
if (again != 1)
Environment.Exit(0);
}
else
isGoing = false;
DisplayResults();
}
static void DisplayResults()
{
Console.WriteLine(tableHeader);
for (int i = 0; i < 5; i++)
{
for (int x = 0; x < 10; x++)
{
Console.Write(responses[i, x]);
}
Console.Write("\n");
}
}
}
}
First off Console.Read() returns an int representing the ascii value of what was entered. If the user enters 1, Console.Read() returns 49. (See this ascii table)
You could use Console.ReadKey()
Second, you need some fixes in the way you loop and ask to continue....
int again = Console.Read();
Your problem is here - Console.Read() returns the first character entered (as represented by its ASCII code), not the number you type in. I leave the solution for your homework.