Jump to content

Need help with Java homework

- - - - -

  • Please log in to reply
1 reply to this topic

#1
bkim33

bkim33

    Newbie

  • Members
  • Pip
  • 4 posts
Hi guys,

Im having some trouble figuring out how to write this program

Write a program to take a sequence of integer values between zero to 200 (not inclusive of zero; inclusive of 200) from the console. Stop requestiing values when the input is less than o equal to he immediate, previously enetered value. The program should then output the total number of valid data entered ( ending input not included in data), and the decimal average of all data displayed to two places after the decimal.

Example run

Enter value: 10
Enter value: 0
** Invalid ** Enter value: -10
** Invalid ** Enter value: 20
Enter value : 34
Enter value: 29

Data count = 3
Average of data = 21.33

Heres what I got so far

	import java.util.*;

public class Aw {

	public static void main(String [ ] args) {

		Scanner console = new Scanner(System.in);

		

		int value = 1;

		int prevalue = 0;

		while (value > prevalue) {

			System.out.print("Enter value: ");

			int v = console.nextInt();

			

		}	

	}


}

	

Im having trouble setting the variables for value and prevalue so that i can make this while loop stop.

Any help at all would be greatly appreciated, Ive spent so much time reading my textbook. My instructor is doing a bad job explaining things to our class and its hard to learn. thank you

#2
cycronica

cycronica

    Newbie

  • Members
  • Pip
  • 5 posts
If I understand what your trying to say here,
you need to update value and prevalue inside the while loop,
making a new int and not using it doesn't make any sense.
Set prevalue to value and then update value to console.nextInt() inside the while loop.

For instance:

while (value > prevalue) {

	prevalue = value;

	System.out.print("Enter value: ");

	value = console.nextInt();






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users