下面是我正在使用的代码,我将显示我收到的输入和输出与我想要的输出。我正在尝试获取代码,以便最终给我现场和非现场工作人员的总数。
string name, DOB, status;
int hours;
System.out.print("how many worker: ");
int number = sr.nextInt();
int countworker = 0;
for (int l = 0; l < number; l++) {
System.out.print("What is ther name ");
name = sr.next();
System.out.print("What is their DOB ");
DOB = sr.next();
System.out.print("How many hours is he/she working ");
hour = sr.nextInt();
System.out.println("What is their Status: fulltime_onsite, parttime_onsite, offsite ");
status = sr.next();
int countoffsite = 0, countonsite = 0;
switch (status) {
case "fulltime_onsite":
System.out.println("pa " + 3465 ) ;
countonsite++;
break;
case "parttime_onsite":
System.out.println("pay" + 1305) ) ;
countonsitee++;
break;
case "offsite ":
System.out.println("pay" + 1620) ) ;
countoffsite++;
break;
}
这是我的输出,我需要帮助让程序告诉我3个学生中有多少是非现场的,有多少是现场的。
name: tom
DOB: 12/3/98
hour: 45
status: fulltime_onsite
pay: 3465
name: jerry
DOB: 3/6/90
hour: 45
status: parttime_onsite
pay: 1305
name: harry
DOB: 6/7/89
hour: 15
status: offsite
pay: 1620
我希望代码以结尾结尾
Off Site: 1
On site: 2
如果显示System.out.println()在for循环之外,请将countonsite,countoffside变量放在for循环之外。
这些变量的作用域对于for循环而言是局部的。意味着在for循环之后它们不存在。通过将它们放置在for循环之外,将允许您访问这些变量在循环外保存的值。因此,您可以使用它们来显示。