java - Array Tester - Beginner -
i long time lurker , first time user of overflow, able guide me in right direction. there more issues program i'd admit, however, i'm getting there! main concerns @ moment below...
my first question looping program beginning. example, if user inputs integer < 1 program outputs error message. program stops there. how can loop prompt user input integer.
i having issue when program prompts user "enter list of () integers:" reason, have enter more integers should have to. in end, program takes appropriate number, pretty weird. (see program test - notice 1 through 5 integer 6 below that. hit enter after 5 program doesn't continue. continues when hit enter, type 6 , hit enter again. array below accurate though.)
let me know if should change else.
hopefully able post updated code when figured out! in advance if have read point! /** * n irwin - programming - lab 23 * array tester - 4/21/2016 */
import java.util.scanner; public class arraytest { public static void main(string[] args) { /** * beginning of program , output. */ system.out.println(" * * * array test * * *"); system.out.println(" written n irwin"); system.out.println(); /** * scanner object. */ scanner input = new scanner(system.in); /** * program output - user required provide input. */ system.out.print("enter number of integers read: "); int integernumber = input.nextint(); system.out.println(); /** * program begins if integer greater or equal 1. */ if(integernumber >= 1) { /** * program output - user required provide input. */ final int length = 100; int[] values = new int [length]; int currentsize = 0; system.out.print("enter list of " + integernumber + " integers: "); /** * */ while (input.hasnextint() && currentsize < integernumber) { if (currentsize < values.length) { values[currentsize] = input.nextint(); currentsize++; } } /** * program output - integers in listed formatting. */ system.out.println(); system.out.println("your integers are:"); system.out.println(); int listnumber = 1; (int = 0; < currentsize; i++) { system.out.printf("%6d", listnumber); system.out.println(". " + values[i]); listnumber++; } /** * program output - integers in listed formatting in reverse. */ system.out.println(); system.out.println("your integers in reverse are:"); system.out.println(); int listreverse = 1; (int = 0; < currentsize; i++) { system.out.printf("%6d", listreverse); system.out.println(". " + values[i]); listreverse++; } /** * program output - total of integers. */ double totalinteger = 0; (int element : values) { totalinteger += element; } system.out.println(); system.out.println("the total of these " + integernumber + " numbers is: " + totalinteger); system.out.println(); /** * program output - average of integers. */ double averageinteger = 0; if (values.length > 0) { averageinteger = (totalinteger / integernumber); } system.out.print("the average of these " + integernumber + " numbers is: "); system.out.printf("%.1f", averageinteger); system.out.println(); system.out.println(); /** * program output - minimum integer. */ int minimuminteger = values[0]; for(int = 1; < values.length; i++) { if (values[i] < minimuminteger) { minimuminteger = values[i]; } } system.out.println("the minimum value in these " + integernumber + " numbers is: " + minimuminteger); system.out.println(); /** * program output - maximum integer. */ int maximuminteger = values[0]; for(int = 1; < values.length; i++) { if (values[i] > maximuminteger) { maximuminteger = values[i]; } } system.out.println("the maximum value in these " + integernumber + " numbers is: " + maximuminteger); system.out.println(); /** * program output - pairs have been summed. */ system.out.println("summing pairs of numbers produces: "); system.out.println(); } /** * program produces error message if integer less 1. */ else { system.out.println("error: integer entered invalid."); } } }
***** program test ***** * * * array test * * * written nathan irwin enter number of integers read: 5
enter list of 5 integers: 1 2 3 4 5 6
your integers are:
1. 1 2. 2 3. 3 4. 4 5. 5
your integers in reverse are:
1. 1 2. 2 3. 3 4. 4 5. 5
the total of these 5 numbers is: 15.0
the average of these 5 numbers is: 3.0
the minimum value in these 5 numbers is: 0
the maximum value in these 5 numbers is: 5
summing pairs of numbers produces:
if want user mandatorily enters number > 1
can this:
int integernumber; do{ integernumber = input.nextint(); if(integernumber < 1) system.out.println("error"); }while(integernumber < 1);
so input display until user enters number > 1
.
for second question, can read full line of numbers , split (divide) string have read taking reference space put between each number. this:
string string = input.nextline(); //reads full line string[] parts = string.split(" "); //here divide string directly in array taking reference space between each number (int = 0; < parts.length; i++) { system.out.println(parts[i]); //you see each input have done }
i expect helpful you. not doubt ask if have questions.
Comments
Post a Comment