如果要把循环得到的多种结果列在一次不同的分类中,利用好字符串很好的解决了这个问题。小白上路,大佬们有更简单的方法请发在评论区吧!
class Demo1{
public static void main(String[] args){
int threeFiveCount=0;
int threeCount=0;
int fiveCount=0;
String threeFiveStr="";
String threeStr="";
String fiveStr="";
for(int a = 1;a<101;a++){
if (a%30&&a%50){
threeFiveStr+=a+" “;
threeFiveCount++;
}else if (a%30){
threeStr+=a+" ";
threeCount++;
}else if (a%50){
fiveStr+=a+” ";
fiveCount++;
}
}
System.out.println(“能被三整除的个数为:”+threeCount);
System.out.println(“能被三整除的数为:”+threeStr);
System.out.println(“能被五整除的个数为:”+fiveCount);
System.out.println(“能被五整除的数为:”+fiveStr);
System.out.println(“能被三和五整除的个数为:”+threeFiveCount);
System.out.println(“能被三和五整除的数为:”+threeFiveStr);
}
}