Reputation: 87
(Writing in java)I have two inputs(History, Geography) taken from the console. I can enter the values of History/Geography as any integer and I can do it any number of times I want. When I decide to stop taking console input I need to find out how many History and how many Geography. I have successfully taken the console inputs. However, I don't know how my code should store (remember) what all I entered for History and Geography and then spit it out. Help Please. IN the example below I cannot figure out how to achieve the second part annotated with
console>
Select 1.Subject 2.Subject Count //console message
1 //console input
Which Subject? 1.History 2. Geography //console message
1 //console input
How many History ? //console message
5 //console input
[Another round]
console>
Select 1.Subject 2.Subject Count //console message
2 //console input
5 History // console message
Upvotes: 1
Views: 102
Reputation: 698
The question you have asked isnt very clear, but from what I understood, this might be of help to you
You could also use a SwitchCase statement here!
import java.io.*;
public class Subject{
public static void main(String[] args)throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
boolean flag = true;
int hist = 0, geog = 0, val = 0;
while(flag){
System.out.println("Enter the number for subject\n1.History\n2.Geography\n -1.Exit");
val = Integer.parseInt(in.readLine());
if(val == 1){
hist ++;
}
else if(val == 2){
geog ++;
}
else if(val == -1){
System.out.print("Total History: ", hist);
System.out.print("Total Geography: ", geog);
flag = false;
continue;
}
else{
System.out.println("invalid option");
continue;
}
}
}
}
/*
int hist[] = new int[100];
int geog[] = new int[100];
int i = 0;
int j = 0
while(true){
//if history
//then do this
hist[i] = i + 1;
i++;
//if geography
//do this
geog[j] = i + 1;
j++
//if user wants to exit
for(int m = 0; m<=i; m++){
//calculate sum like this: Sum += hist[m];
//print sum
}
for(int n = 0; m<=j; n++){
//calculate sum like this: Sum2 += geog[n];
//print Sum2
}
}
*/
Upvotes: 2