学习Java的日常
日期:2019-8-6
day2学习目的:美女选妃
今天学着用Java敲了九九乘法表以及美女选妃代码。
九九乘法表:
import java.util.*;
public class Menu {
public static void main(String[] args)
{
System.out.println("hello world");
Scanner sc=new Scanner(System.in);
int a=1,b=1;
for(a=1;a<10;a++)
{
for(b=1;b<=a;b++)
{
System.out.print(b+"X"+a+"="+a*b+" ");
}
System.out.println();
}
}
}
美女选妃:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class ListStudy {
int height1,height2,height3,weight1,weight2,weight3;
public ListStudy(int height1,int height2,int height3,int weight1,int weight2,int weight3)
{
this.height1=height1;
this.height2=height2;
this.height3=height3;
this.weight1=weight1;
this.weight2=weight2;
this.weight3=weight3;
}
public void getLady()
{
ArrayList <Integer> height=new ArrayList<>();
ArrayList<Integer>weight =new ArrayList<>();
height.add(height1);
height.add(height2);
height.add(height3);
weight.add(weight1);
weight.add(weight2);
weight.add(weight3);
System.out.println(height);
System.out.println(weight);
}
public Map<String,Integer> avg ()
{
int heighttotal=(height1+height2+height3)/3;
int weighttotal=(weight1+weight2+weight3)/3;
Map<String,Integer> map=new HashMap<>();
map.put("heights",heighttotal);
map.put("weights",weighttotal);
return map;
}
}
import java.util.*;
public class ListStudyTest {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int height1,height2,height3,weight1,weight2,weight3;
Map<String,Integer> map=new HashMap<>();
System.out.println("请输入身高");
height1=sc.nextInt();
height2=sc.nextInt();
height3=sc.nextInt();
System.out.println("请输入体重");
weight1=sc.nextInt();
weight2=sc.nextInt();
weight3=sc.nextInt();
ListStudy lady=new ListStudy(height1,height2,height3,weight1,weight2,weight3);
lady.getLady();
map=lady.avg();
String hw;
int avg;
System.out.println("请输入您要获取的平均值数据heights/weights");
hw=sc.next();
System.out.println(map.get(hw));
}
}
构造方法:
构造方法是一个与类同名的方法,对象的创建就是通过构造方法完成的。每当类实例化一个对象时,类都会自动调用构造方法。
构造方法的特点如下:
构造方法没有返回值。
构造方法的名称要与本类的名称相同。
Java——Map集合:
Map集合没有继承Collection接口,其提供的是key到value的映射,Map中不能包含相同的key值,每个key只能影射一个相同的value.key值还决定了存储对象在映射中的存储位置.但不是key对象本身决定的,而是通过散列技术进行处理,可产生一个散列码的整数值,散列码通常用作一个偏移量,该偏移量对应分配给映射的内存区域的起始位置,从而确定存储对象在映射中的存储位置.Map集合包括Map接口以及Map接口所实现的类。