import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0,sumc=0,sumr=0,sumf=0;
for(int i=0;i<n;i++){
int a=sc.nextInt();
String t=sc.next();
sum+=a;
if("C".equals(t)) sumc+=a;
else if("R".equals(t)) sumr+=a;
else sumf+=a;
}
System.out.printf("Total: %d animals\n", sum);
System.out.printf("Total coneys: %d\n", sumc);
System.out.printf("Total rats: %d\n", sumr);
System.out.printf("Total frogs: %d\n", sumf);
System.out.printf("Percentage of coneys: %.2f %%\n", sumc * 100.0 / sum);
System.out.printf("Percentage of rats: %.2f %%\n", sumr * 100.0 / sum);
System.out.printf("Percentage of frogs: %.2f %%\n", sumf * 100.0 / sum);
}
}
利用数组
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[26];
for(int i=1;i<=n;i++)
{
int x=sc.nextInt();char c=sc.next().charAt(0);
a[(int)(c-'A')]+=x;
}
int sum=a['F'-'A']+a['C'-'A']+a['R'-'A'];
System.out.printf("Total: %d animals\n",sum);
System.out.printf("Total coneys: %d\n",a['C'-'A']);
System.out.printf("Total rats: %d\n",a['R'-'A']);
System.out.printf("Total frogs: %d\n",a['F'-'A']);
System.out.printf("Percentage of coneys: %.2f %%\n",a['C'-'A']*100.0/sum);
System.out.printf("Percentage of rats: %.2f %%\n",a['R'-'A']*100.0/sum);
System.out.printf("Percentage of frogs: %.2f %%",a['F'-'A']*100.0/sum);
}
}
标签:Scanner,int,sc,new,打卡,public
From: https://www.cnblogs.com/gyg1222/p/17375259.html