473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help on program

8
I'm trying to make a program where it counts the number of integers greater than 20 entered by the user then it user will stop the program with -1.


public class count
{

public static void Main (String [] args)

{

int integer = 0;
int count = 0;
int sum = 0;

Console.WriteLine(" enter an integer greater than 20, enter -1 when finished");
integer = int.Parse (Console.ReadLine());

while (count > 20)

{
sum+= count + count;
count ++;


}



Console.WriteLine("The number of integers entered is {0}", integer);
}

}



-- I ran this program but it doesn't count the number of integers entered.
Dec 5 '06 #1
5 1122
willakawill
1,646 1GB
I'm trying to make a program where it counts the number of integers greater than 20 entered by the user then it user will stop the program with -1.


public class count
{

public static void Main (String [] args)

{

int integer = 0;
int count = 0;
int sum = 0;

Console.WriteLine(" enter an integer greater than 20, enter -1 when finished");
integer = int.Parse (Console.ReadLine());

while (count > 20)

{
sum+= count + count;
count ++;


}



Console.WriteLine("The number of integers entered is {0}", integer);
}

}



-- I ran this program but it doesn't count the number of integers entered.
Hi. A couple of quick things.
Firstly this code will not work because count begins at 0 and is never greater than 20;
Expand|Select|Wrap|Line Numbers
  1. while (count > 20)
  2.  
  3. {
  4.    sum+= count + count;
  5.    count ++;
  6. }
  7.  
secondly the math to compute the difference between 20 and the entered integer, x, is x - 20

good luck
Dec 6 '06 #2
pearls
8
using System;

public class count
{

public static void Main (String [] args)

{

int integer = 0;
int x = 0;
int count = 0;

Console.WriteLine("Please enter an integer greater than 100, enter -1 when finished");
integer = int.Parse (Console.ReadLine());

while (20 > x)

{

count = x - 20;
}

Console.WriteLine("The number of integers entered is {0}", integer);

}

}

Now when I run it it sticks after all the numbers are entered.
Dec 6 '06 #3
willakawill
1,646 1GB
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public class count
  4. {
  5.  
  6. public static void Main (String [] args)
  7.  
  8. {
  9.  
  10.    int integer = 0;
  11.    int x = 0; 
  12.    int count = 0;
  13.  
  14.    Console.WriteLine("Please enter an integer greater than 100, enter -1 when finished");
  15.    integer = int.Parse (Console.ReadLine());
  16.  
  17.    while (20 > x)
  18.  
  19.    {
  20.  
  21.       count = x - 20;
  22.    }
  23.  
  24.    Console.WriteLine("The number of integers entered is {0}", integer);
  25.  
  26. }
  27.  
  28. }
This will not work and the loop is not necessary.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public class count
  4. {
  5.  
  6. public static void Main (String [] args)
  7.  
  8. {
  9.  
  10.    int integer = 0;
  11.    int x = 0; 
  12.    int count = 0;
  13.  
  14.    Console.WriteLine("Please enter an integer greater than 100, enter -1 when finished");
  15.    integer = int.Parse (Console.ReadLine());
  16.  
  17.    x = integer - 20;
  18.    Console.WriteLine("The number of integers entered is {0}", x);
  19.  
  20.  
  21. }
  22. }
This is better
Dec 6 '06 #4
pearls
8
When I use the run the code you gave me the program stops after only 1 number is entered and the end result will always be the number I ended.

ex.

I entered 250. The result is 250, but it should be 1 since only 1 number was entered.

If I enter 250,251,300,351. Then the answer should be 4, since 4 numbers are entered.
Dec 6 '06 #5
willakawill
1,646 1GB
When I use the run the code you gave me the program stops after only 1 number is entered and the end result will always be the number I ended.

ex.

I entered 250. The result is 250, but it should be 1 since only 1 number was entered.

If I enter 250,251,300,351. Then the answer should be 4, since 4 numbers are entered.
OK, I misunderstood what you were doing.
the parse method will only work on one number at a time so I assume you will be looping the input and checking for the -1 input. It will not work with a string of numbers separated by a comma as you have indicated

This will take one number at a time and output the number of entries that were greater than 20 after the user enters -1

Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public class count
  4. {
  5.  
  6. public static void Main (String [] args)
  7.  
  8. {
  9.  
  10.    int integer = 0;
  11.    int count = 0;
  12.  
  13.    Console.WriteLine("Please enter an integer greater than 20, enter -1 when finished");
  14.    while (true) {
  15.       integer = int.Parse (Console.ReadLine());
  16.       if (integer = -1) {
  17.          Console.WriteLine("The number of integers entered is {0}", count);
  18.          return 0;
  19.      }
  20.       if (integer > 20)
  21.          count++;
  22.    }
  23.  
  24.  
  25.  
  26. }
  27. }
Dec 6 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
2
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
6
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.