上一篇 java的基本程序设计结构1
6、输入输出
6.1 控制台输入与输出
我们可以通过构造Scanner对象,并与“标准输入流”System.in关联,实现通过控制台输入
通过System.out.println(),实现通过控制台输出
/**
* @author: 橘
* @date: 2020-04-27 15:26
*/
public class ScannerDemo {
public static void main(String[] args){
Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);
System.out.print("请输入你的年龄:");
//输入整型数据
int i = scanner1.nextInt();
System.out.println("---------");
System.out.print("请输入你的用户名:");
//输入一行字符串
String s = scanner2.nextLine();
}
}
执行结果:
6.2 文件输入与输出
想要对文件进行读取,就需要用一个File对象构造一个Scanner对象,例如:
Scanner scanner = new Scanner(Paths.get(“D:\个人\1.png”));
想要写入文件,就需要构造一个PrintWriter对象,例如:
PrintWriter printWriter = new PrintWriter(“D:\个人\2.png”);
7、控制流
7.1 条件语句
public class ConditionDemo {
/**
* if(判断条件){
* 执行语句
* }
*/
public static boolean compare1(int a){
if (a == 1){
return true;
}
return false;
}
/**
* if(判断条件){
* 执行语句
* }else {
* 执行语句
* }
*/
public static int compareMax(int a, int b){
if (a > b){
return a;
}else {
return b;
}
}
/**
* if(判断条件){
* 执行语句
* } else if(判断条件){
* 执行语句
* } ...
* else{
* 执行语句
* }
*/
public static char scoreMethod(int score){
if (score >= 85){
return 'A';
}else if (score >= 70){
return 'B';
}else if (score >= 60){
return 'C';
}else {
return 'D';
}
}
/**
* switch语句
*/
public static void getWeek(int week){
switch (week){
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("出错了");
break;
}
}
public static void main(String[] args){
boolean a = compare1(1); //true
int max = compareMax(14, 39); //39
char c = scoreMethod(89); //A
getWeek(4); //星期四
}
}
7.2 循环语句 & 中断控制流程语句
continue:跳出当前循环,继续执行下一条循环
break:跳出当前整个循环体
return:结束整个执行语句
public class WhileDemo {
/**
* 九九乘法表
*/
public static void multiplication(){
for (int i = 1; i <= 9; i++){
for (int j = 1; j <= i; j++){
System.out.print(j + "*" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
/**
* continue break return的比较
* continue 跳出当前循环,继续执行下一条循环
* break 跳出当前整个循环体
* return 结束整个执行语句
*/
public static int test(){
for (int i = 1; i < 4; i++){
for (int j = 1; j<5; j++){
System.out.print(i + "-----" + j + "; ");
if (j == 1){
System.out.print("continue; ");
//结束当前j=1的循环,继续下一条j++,j=2的循环
continue;
}
if (j == 3){
System.out.print("break; ");
//跳出当前整个j的循环,继续下一条i++,i的循环
break;
}
if (i == 3){
System.out.println("return; ");
//直接结束,返回i
return i;
}
}
System.out.println();
}
return 0;
}
public static void main(String[] args){
multiplication();
System.out.println("=================================");
test();
}
}
执行结果:
8、大数值
如果基本的整数和浮点数不能满足需求,可以使用java.math包中的BigInteger和BigDecimal两个类。这两个类可以处理包含任意长度数字序列的数值;
使用静态的valueOf()方法可以将普通的数值转换为大数值;
/**
* @description:大数值
* @date: 2020-04-28 09:38
*/
public class BigIntegerTest {
public static void main(String[] args){
Scanner sc1 = new Scanner(System.in);
System.out.print("请输入一个整数a:");
//输入一个整数
int a = sc1.nextInt(); //10
System.out.println();
Scanner sc2 = new Scanner(System.in);
System.out.print("请输入一个小数b:");
//输入一个小数
double b = sc2.nextDouble(); //2.5
//将a/b转化为大数值类型
BigInteger bigInteger = BigInteger.valueOf(a); //10
BigDecimal bigDecimal = BigDecimal.valueOf(b); //2.5
//进行加、减、乘、除、取余的运算
BigDecimal add = bigDecimal.add(bigDecimal); //5.0
BigDecimal subtract = bigDecimal.subtract(bigDecimal); //0.0
BigInteger multiply = bigInteger.multiply(bigInteger); //100
BigInteger divide = bigInteger.divide(bigInteger); //1
BigInteger mod = bigInteger.mod(bigInteger); //0
//比较 相等返回0,反之根据大小返回正数或负数
int i = bigDecimal.compareTo(bigDecimal); //0
System.out.println(bigInteger + "==" + bigDecimal + "==" + add + "==" + subtract + "==" + multiply + "==" + divide + "=="+ mod + "=="+ i);
}
}
9、数组
9.1 数组初始化
数组是一种数据结构,用来存储同一类型值的集合,通过一个整型下标可以访问数组中的每一个值
1、声明数组变量:需要指出数组类型[] 数组变量名称 eg: int[] a;
2、初始化:int[] a = new int[100] 或 int[] a = {1, 4, 3, 6};
9.2 foreach循环
foreach循环可以用来依次处理数组中的每个元素而不必指定下标值(其他类型的元素集合亦可)
相比于for循环更加简洁一些
eg:
int[] array = {1, 4, 3, 6};
for (int i : array){
System.out.println(i);
}
9.3 数组排序
可以使用Arrays.sort()方法,对数组进行排序
这个方法使用了优化的快速排序算法
java.util.Arrays
package com.java01.day01;
import java.util.Arrays;
import java.util.Scanner;
/**
* @description: 数组
* 数组是一种数据结构,用来存储同一类型值的集合,通过一个整型下标可以访问数组中的每一个值
* 1、声明数组变量:需要指出数组类型[] 数组变量名称 eg: int[] a;
* 2、初始化:int[] a = new int[100] 或 int[] a = {1, 4, 3, 6};
* @date: 2020-04-28 10:07
*/
public class ArrayDemo {
public static void main(String[] args){
//定义初始化数组a
int[] a = {2, 7, 5, 9};
//使用foreach循环 循环输出数组a中的元素
for (int i : a){
System.out.print(i + "; "); //2; 7; 5; 9;
}
System.out.println();
//使用Arrays.copyOf()方法拷贝数组a到数组copy中
int[] copy = Arrays.copyOf(a, a.length);
//使用Arrays.toString()方法输出打印数组
System.out.println(Arrays.toString(copy)); //[2, 7, 5, 9]
//使用Arrays.sort()方法,对数组copy进行排序
Arrays.sort(copy);
System.out.println(Arrays.toString(copy)); //[2, 5, 7, 9]
//使用二分法查找
int index = Arrays.binarySearch(copy, 7);
System.out.println(index); //2
System.out.println("-----我是分割线---------");
//随机抽取
randomTest();
}
/**
* 随机抽取
*/
public static void randomTest(){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您要抽取几个数字:");
int k = scanner.nextInt();
System.out.println();
System.out.print("请输入您抽取的最大范围数字:");
int n = scanner.nextInt();
System.out.println();
//定义被抽取的数组 大小为前面输入的最大范围
int[] numbers = new int[n];
//将1~n放进被抽取的数组中
for (int i = 0; i<numbers.length; i++){
numbers[i] = i + 1;
}
//定义抽取后的结果数组,大小为前面输入的要抽取的数量
int[] result = new int[k];
for (int i = 0; i< result.length; i++){
//获取随机数
int index = (int) (Math.random() * n);
//将随机数作为索引获取数组中的值,作为抽取结果
result[i] = numbers[index];
//减少一个被抽取数组中的元素
numbers[index] = numbers[n-1];
n--;
}
//排序
Arrays.sort(result);
System.out.print("抽取的结果为: ");
//打印抽取结果
for (int re : result){
System.out.print(re + " - ");
}
System.out.println();
}
}
运行结果: