(用java编写)我有两个来自控制台的输入(历史、地理)。我可以将历史/地理的值输入为任何整数,也可以输入任意次数。当我决定停止接受控制台输入时,我需要了解有多少历史和地理。我已成功获取控制台输入。然而,我不知道我的代码应该如何存储(记住)我为历史和地理输入的内容,然后把它吐出来。请帮忙。在下面的例子中,我不知道如何实现用注释的第二部分
控制台>
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
你问的问题不是很清楚,但据我所知,这可能对你有帮助
您也可以在这里使用SwitchCase语句!
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 subjectn1.Historyn2.Geographyn -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
}
}
*/