java - Read in 5 numbers from a user and compute the frequency of positive numbers entered -


having difficulty trying write code problem above. please find code below. have read in 5 numbers , compute frequency of positive numbers entered.

import java.util.scanner;  public class lab02ex2partb {      public static void main (string [] args){         scanner input = new scanner(system.in);         system.out.println("please enter  positive integer");         int number = input.nextint();         for(int = -2 ; < 4 ; i++)              system.out.println("positive count is: " + i);      }            } 

your problem have task needs repeated (about user entering value); loop (the perfect mean things repeatedly) ... doesn't cover part!

for(int i=-2 ; i<4 ; i++) system.out.println("positive count is: " +i);

instead, like:

for (int loops = 0; loops < 5; loops++) {   int number = input.nextint(); 

then of course, need remember 5 values, easiest way there: use array; turning code into:

int loopcount = 5; int numbers[] = new[loopcount];  (int loops = 0; loops < loopcount; loops++) {   numbers[loops] = input.nextint(); 

and then, finally, when asked numbers, check data got in array compute frequencies. simple approach work this:

for (int number : numbers) {   if (number > 0) {     system.out.println("frequency " + number + " is: " + computefrequency(number, numbers));   } 

with little helper method:

private int computefrequency(int number, int allnumbers[]) {   ...  

please note: meant going - don't intend homework you. should still sit down , figure "computing frequency" means; , how that.


Comments

  1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog.
    It is very nicely explained. Your article adds best knowledge to our Java Online Training from India.
    or learn thru Java Online Training from India Students.

    ReplyDelete

Post a Comment

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -