I get this error message and i dont know whats wrong with it. The code doesnt have to be done with for
using System;
public class Program
{
public static void Main()
{
int i, j;
for(i=1;i<9;i++)
{
for(j=1;j<= i;j++)
{
Console.Write("" + j);
}
Console.WriteLine();
}
}
}
Perhaps try:
using System;
public class Program
{
public static void Main()
{
string current = string.Empty;
for(int i=1;i<9;i++)
{
current += i.ToString();
Console.WriteLine(current);
}
}
}
You dont need to declare "int i;" just declare it in the for loop.
You dont need the j loop as you are only adding the latest number to the output.
Related
I'm practicing methods, but the problem is I want to separate inputting and sorting, the display method will be the main, I'm having trouble fixing this calling from other class.
This is my script :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseProblem
{
class Method
{
static void MethodInput()
{
int[] array = new int[5];
int i;
// loop for accepting values in array
for (i = 0; i < 5; i++)
{
Console.Write("Enter number:\t");
array[i] = int.Parse(Console.ReadLine());
}
}
public static void MethodSort()
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
}
class Program
{
static void Main(int[]array)
{
//sorting array value;
Array.Sort(array); //use array's sort function
Method.MethodSort(array);
Console.ReadLine();
}
}
}
Thank you for your help
First of all Main() ( or "EntryPoint" as we should call it ) cannot have int[] as an input parameter but string[] instead you should learn basics of programming before starting to actually code something.
Second thing :
I want to separate inputting and sorting
You can create an object called Input
public class Input
{
public static void Write(string message)
{
Console.WriteLine(message);
}
public static int? ReadInt(string reason)
{
Write(reason);
string userInput = Console.ReadLine();
int parsed = 0;
if(int.TryParse(userInput, out parsed))
return (int?)parsed;
return null;
}
}
This will be your "InputLogic" which you can use as Input.ReadInt("Please specify your age: ");
Next you can make an Operations object :
public class Operations
{
public void Display(int[] arr)
{
foreach(int i in arr)
{
// and since you have "Input" class that can display things
Input.Write(i.ToString());
}
}
public void Sort(ref int[] arr)
{
Array.Sort(arr);
}
}
Now the last thing is to combine it within your Program
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5];
for(int i = 0; i < arr.Length; i++)
{
int? input = null;
while( !( input = Input.ReadInt("Give me number") ).HasValue ) { }
arr[i] = input.Value;
}
Operations op = new Operations();
op.Display(arr);
op.Sort(ref arr);
op.Display(arr);
}
}
Well, you are missing the parameter for the called function.
public static void MethodSort(int[] array)
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
note: not tested
Okay, you have two errors, both in this method:
public static void MethodSort()
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
as well as how you call it:
Method.MethodSort(array);
The first problem is that the method uses the variable array, which doesn't exist in that method's scope.
The second problem is that you are passing array to the call to Method.MethodSort, but that method isn't configured to take a parameter.
There are two ways to approach solving this: remove array entirely, or change the method to accept it. Now, you obviously cannot remove array, as the whole point is to do stuff with it. Therefore, the logical solution is to add array as a parameter to your method:
public static void MethodSort(int[] array)
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
using System;
namespace UnaryOperators
{
class UnaryOperators
{
//pre and post incerment checking and examples
public int a=0;
public int PreIncrement()
//shows error here(not all code paths return value)
{
//what i am trying to do here is i want to create 2 methods
//one for pre increment and other for post increment
//but when i am typing program i stuck with above error so
//i didn't complete the code
//i want to know how pre increment and post incerment work
for(a = 0; a < 10; a++)
{
Console.WriteLine("PreIncrement value of a is "+a);
return a;
}
}
public static void Main(string[]args)
{
/*
//if any one gives me a program as an example i will be really thankful
//please give me an example to understand pre and post increments
// if you can understand anything of my code help me solve it
// (but honestly think my code is shit)
*/
}
}
}
This little program shows how pre and post increments works.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("After pre {0}", PreInc());
Console.WriteLine();
Console.WriteLine("After post {0}", PostInc());
Console.ReadLine();
}
public static int PreInc()
{
int a = 0;
do {
Console.WriteLine("PreIncrement value of a is {0}", ++a);
} while (a < 10);
return a;
}
public static int PostInc()
{
int a = 0;
do {
Console.WriteLine("PostIncrement value of a is {0}", a++);
} while (a < 10);
return a;
}
}
When I came across delegates I wrote this really simple program just to practice. when I run it there is a stackoverflowexception. so if anyone can tell me what is wrong with this piece of code please do cause I have wasted a lot of time on trying to make it work but couldn't.
Here is the code:
using System;
public delegate void click();
class test
{
public click flare;
public double length;
public double Length
{
get
{
return Length;
}
set
{
Length = value;
flare();
}
}
}
class glance
{
public glance(ref test a)
{
a.flare = blank;
}
public void blank()
{
Console.WriteLine("this is blank");
}
}
class Program
{enter code here
static void Main(String[] args)
{
test know = new test();
glance x = new glance(ref know);
know.Length = 10;
}
}
It has nothing to do with delegates. You are calling setter method inside of setter in Lenght property and that causes the exception.Use the backing field you created for your property:
public double Length
{
get
{
return length;
}
set
{
length = value;
flare();
}
}
I keep trying to make this C# program work, but I keep getting an Error about the constructor taking 1 argument. I don't get it. I think it has to do with the " Test myTest = new Test(3);" but I don't know what to do with it.
Any help or steering me in the right direction would be greatly appreciated. Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Test
{
private int tally;
public void Test(int start)
{
tally = start;
}
public void AddFive()
{
tally += 5;
}
public void Display()
{
Console.WriteLine("The tally is {0}", tally);
}
public void Main(string[] args)
{
Test myTest = new Test(3);
myTest.AddFive();
myTest.Display ();
}
}
}
Constructors don't have return type.
So instead of
public void Test(int start)
{
tally = start;
}
you should have
public Test(int start)
{
tally = start;
}
In the constructor definition you said to return void. That is not required. Your constrctor should be
public Test(int strat)
{
...
}
I am picking up C# and as a beginners exercise I tried to code a simple Tekken Tournament roster. The problem is, that I cannot find a way how to apply a method of class Fighter onto the whole list of fighters, as there are no names of the variable, or at least I think that is the problem.
using System;
using System.Collections.Generic;
namespace TekkenConsoleTournament
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Enter nicknames of players, finished by 0: \n");
int numFighter=0;
String nickname;
List <Fighter> allFighters = new List<Fighter>();
while ((nickname = Console.ReadLine()) != "") {
allFighters.Add(new Fighter(nickname));
numFighter ++;
}
Console.WriteLine(allFighters.ForEach(Fighter.getName()));
Console.WriteLine(foreach(Fighter in allFighters) {getName();});
//for (int counter = 0; counter <= fighter; counter++) {
// Fighter[counter.getName();
//}
}
}
}
And the Fighter.cs
using System;
namespace TekkenConsoleTournament
{
public class Fighter
{
private int matches, won, lost, draw;
String name;
public Fighter (String name)
{
this.matches = this.won = this.lost = 0;
this.name = name;
}
public void wonMatch()
{
matches++;won++;
}
public void lostMatch()
{
matches++;lost++;
}
public void drawMatch()
{
matches++;draw++;
}
public int[] getStats()
{
int[] stats = {this.matches,this.won,this.lost,this.draw};
return stats;
}
public String getName()
{
return this.name;
}
}
}
At first, I d like to print the names of all fighters, then maybe the stats and then as an exercise try making the code remember who fought who and put them in two dimensional matrix based on the result of the fight.
Use a foreach loop ?
foreach(var fighter in allFighters)
{
// do something with fighter
var name = fighter.getName();
Console.WriteLine(name);
}
I assume there is a getName method, if there is not use your field or property name to get current fighter's name.
Your mistake is you are putting ForEach loops inside of Console.WriteLine method.You should do the opposite.And this syntax is wrong:
foreach(Fighter in allFighters)
It should be like this:
foreach(Fighter fighter in allFighters)
Or you can use var as shown in the above code.
You're close, but not quite. Try these options:
Console.WriteLine(string.Join(", ", allFighters.Select(f => f.getName())));
allFighters.ForEach(fighter => Console.Writeline(fighter.getName()));
foreach (Fighter f in allFighters)
{
Console.WriteLine(f.getName());
}