API
Scanner类
练习题
1.求两数之和
package com.wang.API.Scanner.Demo1.Demo2;
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
int c;
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
c = a + b;
System.out.println(c);
}
}
2.求三数比大小:
package com.wang.API.Scanner.Demo1.Demo2;
import java.util.Scanner;
public class Max {
public static void main(String[] args) {
int max;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int a = scanner.nextInt();
System.out.println("请输入第二个数字:");
int b = scanner.nextInt();
System.out.println("请输入第三个数字:");
int c = scanner.nextInt();
if (a>b){
if (a>c){
max = a;
}else{
max = c;
}
}else{if (b>c){
max = b;
}else {
max = c;
}
}
System.out.println("最大值为:"+max);
}
}
匿名对象
package com.wang.API.Scanner.Demo1.Demo3;
/*
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
new 类名称();
注意事项:匿名对象只能使用一次,下次使用又需要再次创建
*/
public class Demo3 {
public static void main(String[] args) {
Person one = new Person();
one.name="高圆圆";
one.showName();
System.out.println("============");
new Person().name = "赵又廷";
new Person().showName();
//出来的是我叫null
//因为此对象又被new了一次
}
}
用匿名对象使用Scanner
package com.wang.API.Scanner.Demo1.Demo3;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
//匿名对象
int num = new Scanner(System.in).nextInt();
System.out.println("输入的是:"+num);
}
}
Random
package com.wang.API.Scanner.Demo1.Demo4;
/*
Random类用来生成随机数字
1.导包
2.创建
Random r = new Random();
3,使用
获取一个随机的int数字:
int num = r.nextInt() (此时生成的数的范围是int的范围)
int num = r.nextInt(n) (此时的范围是【0,n)
*/
import java.util.Random;
public class Demo1 {
public static void main(String[] args) {
Random random =new Random();
int a = random.nextInt(100);
System.out.println("随机数字为:"+a);
}
}
使用Randmom创建的1-100之间猜数游戏练习
将随机值控制在1-100之间
将可以猜测的次数控制在10次之内
package com.wang.API.Scanner.Demo1.Demo4;
import java.util.Random;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Random random = new Random();
int a = random.nextInt(100) + 1;
//变为1-100的范围
int i = 0;
int n = 0;
System.out.println("请输入你猜的数字:");
do {
n++;
int num = new Scanner(System.in).nextInt();
if (num > a) {
System.out.println("猜大了");
} else if (num < a) {
System.out.println("猜小了");
} else {
i++;
System.out.println("恭喜你,猜对啦!");
break;//猜中跳出循环
}
if (n>10){
System.out.println("您猜测的次数超过十次");
break;
}else {
System.out.println("请输入你再次猜测的数字:");
}
} while (i == 0);
if (n <= 10){
System.out.println("您猜对数字一共花费了"+n+"次。");
System.out.println("游戏结束!");
}else {
System.out.println("游戏失败!");
}
}
}
对象数组
main方法
package com.wang.API.Array;
/*
题目:
要求定义一个数组,用来存储三个Person对象
数组有一个缺点
一旦创建,数组的长度不能发生改变了
*/
public class Demo1 {
public static void main(String[] args) {
Person[] array = new Person[3];
Person one = new Person("迪丽热巴",18);
Person two = new Person("古力娜扎",28);
Person three = new Person("马尔扎哈",38);
array[0] = one;//将one中的地址值 赋值到0号元素中
array[1] = two;
array[2] = three;
System.out.println(array[1].getName());
}
}
Person类
package com.wang.API.Array;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}