Results 1 to 16 of 16
Thread: can someone look at this
- 02-02-2011, 04:52 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
can someone look at this
i want to make sure this is right i have to:
1. prompt the user to input 5 decimal numbers (1,2,3,4,5)
2. print the 5 decimal numbers
3, convert decimal to nearest integer
4. add the 5
5. prints the sum and average
import java.util.*;
public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);
int number1;
int number2;
int number3;
int number4;
int number5;
double sum;
double average;
number1 = 1;
number2 = 2;
number3 = 3;
number4 = 4;
number5 = 5;
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;
System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;
System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;
System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;
System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;
System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;
sum = number1 + number2 + number3 + number4 + number5;
System.out.print("sum is")
average = sum / 5;
outfile.printf(number1, number2, number3, number4, number5, sum, average)
}
input.close();
outfile.close();
}
- 02-02-2011, 04:59 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Why don't you use an array int[] numbers= new int[5]? and what is that outfile thing near the end of your code?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-02-2011, 05:02 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
cool ill try that out where would i put the int [] at?
- 02-02-2011, 05:04 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2011, 05:14 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I don't see the step where you convert from decimal to integer, do you mean to be able to take 1.34 as an input and convert it to 1? If so you may want to declare a float array and store everything as floats, then use Math.round(float num) to round them to ints.
- 02-02-2011, 05:14 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
So I Would put it after the scanner input
- 02-02-2011, 05:15 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
Yea that's the part I need help on right now
- 02-02-2011, 05:20 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
instead of
it would be much better to declare JUSTJava Code:int num1; int num2; .. int numn;
You can then make a loop to prompt for numbers and store items in the array.Java Code:float[] nums = new float[5];
Hope this helps, this will allow you to ask for numbers, and store the inputs.Java Code:for(int i = 0; i < nums.length; i++){ //prompt //store input in array positions }
- 02-02-2011, 05:53 PM #9
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
so it would be this :
import java.util.*;
public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);
float[] nums = new float [5];
double sum;
double average;
for (int i = 0; i < nums.length; i ++0){
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;
System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;
System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;
System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;
System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;
System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;
sum =Last edited by d2011; 02-02-2011 at 05:56 PM.
- 02-02-2011, 05:56 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 02-02-2011, 06:11 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Thats close, except the loop will print all that code 5 times.
Is there a way you can print one statement where the item number changes with each pass through the loop? What happens if you print i? How can you include i to create one print statement which will change?
Also, you got rid of the variables number1, number2, ...numbern. To reference an item in an array you do
Java Code:arrayName[index number]
- 02-02-2011, 06:20 PM #12
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
so it would be this :
import java.util.*;
public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);
float[] nums = new float [5];
double sum;
double average;
for (int i = 0; i < nums.length; i ++0){
arrayName[index number]
do i get rid of all of the
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;
- 02-02-2011, 06:26 PM #13
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
no.
are place holders, you want to use your arrays name for arrayName, and some index number in between bracketsJava Code:arrayName[index number]
are all validJava Code:arr[i]; arr[0]; arr[1];
which one sounds the most important to you?
also, this simple references that item in the array, if you want to store something there you need
you can figure out what add something should be yourself.Java Code:arr[index] = add something;
Also, EACH time through the loop it should prompt a message asking for a number
are all valid things you can print, choose one which you like the best.Java Code:System.out.println("Please enter a number:"); System.out.println("Please enter number " + something + ": ");
also, proper loop structure is
I usually useJava Code:for(initialization; condition; some change){ statements }
Try reading these two thingsJava Code:for(int i = 0; i < something; i++){ statements }
http://download.oracle.com/javase/1....il/Arrays.html
http://download.oracle.com/javase/tu...ts/arrays.htmlLast edited by sunde887; 02-02-2011 at 06:28 PM.
- 02-02-2011, 08:38 PM #14
Member
- Join Date
- Feb 2011
- Posts
- 7
- Rep Power
- 0
i looked over the web pages you gave me about arrays and came up with this
import java.util.*;
public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);
float[] nums = new float [5];
double sum;
double average;
arrayName[index number]
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
arr[4] = 4;
arr[5] = 5;
for(int i = 0; i < something; i++){
statements
}
System.out.println("Please enter a number:");
System.out.println("Please enter number " + something + ": ");
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;
System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;
System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;
System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;
System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;
System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;
sum = number1 + number2 + number3 + number4 + number5;
System.out.print("sum is")
average = sum / 5;
}
}
-
So you've solved your problem then?
Also you'll want to use code tags when posting code in this forum so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
- 02-03-2011, 02:03 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Im sorry to sound rude if you really just don't understand, but, are you even trying this, or just taking what I say and adding it in word for word?
If you are trolling, oh well, but if you really are trying to understand this and just having some trouble I will continue to help.
When I say something like statements or something, I am giving you a general idea of how the program should look. However, you must decide what something and statements should be equal to.
This program should consist of 4 important things, a scanner, an array and 2 for loops. One loop should prompt for input and add something to the array with each pass. The second loop should print out the array.
This is the basic part of the program, to add the functionality you need you need 2 additional int items, sum and average. and another for loop. This third loop should loop through the array, round the number, then add it to sum.
Finally compute the avg with the sum.
This is basically a step by step procedure for this, however, you will need to translate it into java yourself.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks