8.6
今天学习了几个idea程序的书写,比如数组、判断是否是动物、九九乘法表(正三角形)以及“美女选妃”。
1.数组
public class Test{
public static void main(String []args){
int [] arr= {1,2,3,4,5,6,7,8,9,10};
for(int a:arr){//第一轮循环就输出arr数组的第0个元素, 第二轮就输出arr数组的第一个元素,第三轮就输出arr数组的第二个元素……
System.out.println(a+"");
}
}
}
2.判断是否是动物
public class Test {
public static void main(String[] args){
An xiaohua=new An();
xiaohua.legs=2;
xiaohua.say=false;
if(xiaohua.legs ==4 && !xiaohua.say ){
System.out.println(“xiaohua是小狗”);
}else{
System.out.println(“xiaohua不是小狗”);
}
}
}
3.九九乘法表(正三角形)
public class Test {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + “*” + j + “=” + (i * j) + “\t”);
}
System.out.println();
}
}
}//九九乘法表正三角形
4.“美女选妃”
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Beautyfulleaies { //1.从宁波财经学院选10个美女
void getLadies() {
List hights = new ArrayList<>();
List wights = new ArrayList<>();
hights.add(180);
hights.add(150);
hights.add(155);
wights.add(55);
wights.add(50);
wights.add(70);
System.out.println(hights);
System.out.println(wights);
Map<String, Integer> hightwightAvg = getHightWightAvg(hights,wights);
System.out.println(hightwightAvg);
boolean result = getResult(hightwightAvg);
if (result) {
System.out.println(“是美女”);
} else {
System.out.println(“不是美女”);
}
}
//2.计算10个美女的平均身高和体重
Map<String,Integer> getHightWightAvg(List hights,List wights){
int sumH = 0;
for (int h : hights) {
sumH = sumH + h;
}
int sumW = 0;
for (int w : wights) {
sumW = sumW + w;
}
System.out.println(“sumH:” + sumH);
System.out.println(“sumW:” + sumW);
int avgH = sumH / 3;
int avgW = sumW / 3;
System.out.println(“avgH:” + avgH);
System.out.println(“avgw:” + avgW);
Map<String, Integer> map = new HashMap<>();
map.put(“avgH”, avgH);
map.put(“avgW”, avgW);
return map;
}
//3.判断平均身高》150,体重《60
boolean getResult(Map<String,Integer> map){
int avgH = map.get(“avgH”);
int avgW = map.get(“avgW”);
if(avgH>150 && avgW<60){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
Beautyfulleaies beautyfulleaies=new Beautyfulleaies();
beautyfulleaies.getLadies();
}
}