I labeled the problems in the coding.
Problem #1 is easy and I'm probably just missing it. I have to key entries between 0 - 500 and if the entry doesn't fall with in that range it should be kicked back for a valid entry. This should also keep with the correct valid entries for the average.
Problem #2 I'm totally at a loss. It should display as follows:
Histogram of 10 inputs:
0 - 99: **
100 - 199: *
200 - 299: ++
300 - 399: ***
400 - 500: **
Average: 271.4
the asterisk[] coding i have is messing with me. I can not figure what to put in [] to get it to display properly.
And Problem #3 is how to get to display the * as + for the range the average falls under.
Thanks, any help is appreciated.
import java.util.Scanner;
public class project2histogram2{
public static void main(String[] args){
int[] rangeInput;
rangeInput = getRangeData(); //Method 1 - Input
processRangeData(rangeInput); //Method 2 - Output
}
//Method 1
public static int[] getRangeData(){
//User input number of inputs to be entered.
Scanner input1 = new Scanner(System.in);
System.out.println("Number of inputs: ");
int numInputs = input1.nextInt();
Scanner input2 = new Scanner(System.in);
int[] rangeInput = new int[numInputs];
System.out.println("Enter " + rangeInput.length + " numbers to be sorted: ");
for(int i = 0; i < rangeInput.length; i++){
if(rangeInput[i] < 0 || rangeInput[i] > 500)
System.out.println("Please specifiy a number between 0 and 500.");
//Problem #1
//above is suppose to delete and replace invalid entries.
else{
if (rangeInput[i] >= 0 || rangeInput[i] <= 500)
System.out.println("You entered: " + rangeInput[i]);
}
rangeInput[i] = input2.nextInt();
}
return rangeInput;
}
//Method 2
public static void processRangeData(int[] rangeInput){
double averageRange = 0;
int num = 0;
int count = 0;
//Calculates average
for(int i = 0; i < rangeInput.length; ++i){
averageRange += rangeInput[i];
}
averageRange /= rangeInput.length;
//Outputs Array Length
System.out.println("Histogram of " + rangeInput.length + " inputs: ");
//0-500 Display
String[] asterisk = {" 0- 99 | ", "100- 199 | ", "200- 299 | ", "300- 399 | ", "400- 500 |"};
for (int i = 0; i < rangeInput.length; ++i){
num=rangeInput[i];
if (num >=0 && num <= 99) asterisk [0] +="*";
else if (num >= 100 && num <= 199) asterisk[1] +="*";
else if (num >= 200 && num <= 299) asterisk[2] +="*";
else if (num >= 300 && num <= 399) asterisk[3] +="*";
else if (num >= 400 && num <= 500) asterisk[4] +="*";
}
for (int i =0;i < rangeInput.length;++i)
System.out.println(asterisk[]);
// Problem #2
// asterisk[?] can't get to display properly.
//Display Average
System.out.printf("\nAverage is: %.2f", averageRange, ".");
}
}

New Topic/Question
Reply



MultiQuote




|