i have make java program
, user type in total numbers of students, made code:
import java.util.*; import java.lang.*; import java.io.*; /* name of class has "main" if class public. */ class ideone { public static void main (string[] args) throws java.lang.exception { // code goes here int numreaders = 0; scanner scan = new scanner(system.in); system.out.println("enter number of magazin readers:"); numreaders = scan.nextint();
now, after adding total number of students, should add names array:
//creating array of names, length total number entered user string[] namestr = new string[numreaders]; int[] ages = new int[numreaders]; for(int i=0; i<numreaders; i++) { scanner n = new scanner(system.in); system.out.println("enter name of reader: "+i); namestr[i] = n.next(); }
after that, should add correspondingly age of each name, made portion of code:
for(int i=0; i<numreaders; i++) { scanner = new scanner(system.in); system.out.println("enter age of reader: "+i); ages[i] = a.nextint(); } //display results system.out.println("number of readers is: "+numreaders); (int i=0; i<numreaders; i++) { system.out.println("the name of reader "+i+" "+namestr[i]+ " , age "+ages[i]); }
after making code, tested using ideone , command prompt , works properly:
now, need call method according selection of user: if typed 'a' method should called specify name , age of oldest student. if typed 'b' method called see how many students have age specified user , if typed 'c', function called calculate average age of them all.
i new methods don't know how add arrays methods , make statements.
here full code:
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* name of class has "main" if class public. */ class ideone { public static void main (string[] args) throws java.lang.exception { // code goes here int numreaders = 0; scanner scan = new scanner(system.in); system.out.println("enter number of magazin readers:"); numreaders = scan.nextint(); //creating array of names, length total number entered user string[] namestr = new string[numreaders]; int[] ages = new int[numreaders]; for(int i=0; i<numreaders; i++) { scanner n = new scanner(system.in); system.out.println("enter name of reader: "+i); namestr[i] = n.next(); } for(int i=0; i<numreaders; i++) { scanner = new scanner(system.in); system.out.println("enter age of reader: "+i); ages[i] = a.nextint(); } //display results system.out.println("number of readers is: "+numreaders); (int i=0; i<numreaders; i++) { system.out.println("the name of reader "+i+" "+namestr[i]+ " , age "+ages[i]); } //choosing statistic //if a: system.out.println("please choose a, b or c:"); scanner stat = new scanner(system.in); char x; x = stat.next().charat(0); if(x=='a') system.out.println(x+x); else if(x=='b') //system.out.println(x); //scanner newage = new scanner(system.in); //int agetosearchfor = newage.nextint(); //maxage(agetosearchfor); else system.out.println(x); } }
right, start user enters input
, example 'a
', let's go this:
firstly, need create method name of oldest student displayed, let's call 'getoldeststudent
' - when naming methods typical naming convention, starting lowercase , moving uppercase each new word - try , make them intuitive possible.
when making method signature, need give visibility , going return. in case, using 1 class, give private, visible class.
now need return 2 things, can either put these string
or put them array
, recommend, going return
array
. however, want input
array
search through, goes in tbe brackets parameters
(or arguments
). therefore our method signature
following:
private string[] getoldeststudent(string[] students, int[] ages)
then inside method, can code need find oldest student, add name , age array , return
this.
need anymore drop comment.
on side note, have been better off creating 'student
' object , giving object 'name' property , 'age' property , making array
of students
, getters
, setters
(or accessors
, mutators
) these properties.
Comments
Post a Comment