Java program how to read multiple Doubles in one line? -
alright,so i've got assignment requires me have method variable number of inputs string input. inputs have on 1 line in scanner, , method has return number of values entered,the average value,the max value,the min value,and string entered.
this example of terminal window should like.
please enter name of course: coursenamehere please enter scores csc 201 on single line , type -1 @ end 71 02 81 44 84 17 38 11 20 05 93 -1 course name : coursenamehere number of scores : 11 average score : 42.37 minimum score : 02 maximum score : 93
the average score has rounded 2 decimal places(which think can handle) problem me getting variable number of inputs scanned on single line,and how have program count number of inputs if i'm not hitting enter between inputs. have far.but have no idea go here. can ask sequential values,but aren't on same line
i know put return in method? im new java please help
this program calculates code without reading 1 line reading them in separate lines need how read them in same line:
system.out.println("please enter number of courses to\n" + "calculate average score, minimum score and\n" +"the maximum score: "); // prompt user enter number of courses average, min , max neeed calculated. //create scanner object read keyboard scanner input = new scanner(system.in); //declares new scanner called input take input keyboard int count = input.nextint(); // read user input , store in count variable input.nextline(); //read end of line , throw away (int = 0; < count; i++) { system.out.println("please enter name of course: "); // prompt user enter course name string coursename = input.nextline(); // stores user input keyboard variable coursename system.out.print("please enter score "+ coursename + " or type -1 indicate\n" + "that there no more score course: "); // prompt user enter grade in class double score = input.nextdouble(); // stores user input score variable double int numberofscores = 0; // declare integer variable called numberofscored , set value 0 double total = 0; // declare double variable called total , set value 0 double min = 100; // declare double variable called min , set value 100 double max = 0 ; // declare double called max , set value 0 while (score!=-1) // start while loop when score not equal -1 { if ( score < min) // start of if statement { min = score; } //end of if //start of if statement if ( score > max) { max = score; } // end of if statement total = total + score; // sets value of variable total equal + score numberofscores++; // adds 1 variable numberofscores system.out.print("please enter score "+ coursename + " or type -1 indicate\n" + "that there no more score course: "); // prompt user enter grade in class score = input.nextdouble(); // sets value of score next input double user } double average = total/numberofscores; // declare variable average , set value total divided buy number of scores system.out.println("the course name: " + coursename); //print messege shows user course name entered system.out.println("number of scores: " + numberofscores); // prints messege number of scores system.out.printf(" average score: %.2f" , average); // prints messege average score 2 decimal places system.out.println(); // goes next line system.out.printf(" minimum score: %.2f" , min);// prints message minimum score 2 decimal places system.out.println(); // goes next line system.out.printf(" maximum score: %.2f" , max); // prints messege maximum score 2 decimal places system.out.println();// goes next line input.nextline(); //read end of line , throw away }
this code work please ask me if feel confused :
scanner input = new scanner(system.in); linkedlist<integer> list = new linkedlist<integer>(); system.out.println("please enter name of course:"); string course=input.next(); system.out.println("please enter scores csc 201 on single line , type -1 @ end"); int max=0; int min=integer.max_value; int sum=0; int grade; that: while(true){ grade=input.nextint(); if(grade==-1) break that; list.add(grade); sum=sum+grade; if(grade>max) max=grade; if(grade<min) min=grade; } system.out.println("the course name: "+ course); system.out.println("number of scores :"+list.size()); system.out.println("the average score :"+(double)sum/list.size()); system.out.println("the minimum score: "+min); system.out.println("the maximum score :"+max);
ps : integer.max_value huge value maybe 100 if limit of grade 100.
why using linked list ?
because it's easy store values 1 one .
i can reach elements when ever want.
also method size of list number of elements has don't need counter count how many grades have .
also can print them after if want because grades stored in list.
if don't need these grades in next , can ignore list , without .
good luck .
Comments
Post a Comment