Java

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsWeb Design & DevelopmentJava

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old July 10th, 2010, 04:19 PM
Bewitched1 Bewitched1 is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 2 Bewitched1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 24 m 46 sec
Reputation Power: 0
Unhappy Looping without a "while"

I need help with writing a counting program that involves looping. I have already written 2 a do/while and a while. I need a 3rd that does not involve a while statement.

This is what I have so far and it does not keep going until I enter a -1. It is suppose to count the number of integers entered, add them, and give the average all without counting the -1.

import java.io.*;

public class Assign7_Cheshire {
public static void main(String[] args)throws IOException {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader (isr);
/* Read and process the user's input*/
String input="";


int count=0;
double average;
int sum=0;
System.out.print("Please enter a number: ");
input=br.readLine();


int number=Integer.parseInt(input);

for(number=0; number< 0; number++)continue;

sum++;
count++;
average= sum/count;
if(number==-1){
}
System.out.println("The sum of the numbers you entered is: " + sum);
System.out.println("The total of numbers entered: " + count);
System.out.println("Their average is: " + average);



}

}

Reply With Quote
  #2  
Old April 7th, 2011, 12:56 PM
hamidd hamidd is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Apr 2011
Posts: 1 hamidd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 25 sec
Reputation Power: 0
here is what i came up with :

import java.io.*;

public class Assign7_Cheshire{
public static void main(String[] args)throws IOException {
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br= new BufferedReader (isr);
/* Read and process the user's input*/
String input="";
int count = 0;
double average = 0;
int sum = 0;
int temp = 0;

for (int i = 0; i < 10 ; i++){
System.out.println("Please enter a namber:");
input = br.readLine();
temp = Integer.parseInt(input);
if (temp == -1){
System.out.println("END");
break;
}
else{
sum = sum + temp;
count = count + 1;
}
}
average = sum / count ;

System.out.println("The sum of the numbers you entered is: " + sum);
System.out.println("The total of numbers entered: " + count);
System.out.println("Their average is: " + average);

}
}

Reply With Quote
Reply

Viewing: Tutorialized ForumsWeb Design & DevelopmentJava > Looping without a "while"


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap