格式化输出
package chapter_3;
public class print_format {
public static void main(String[] args) {
double x=10000.0/3.0;
System.out.printf("%8.2f",x);
int a=1;
System.out.printf("%d",a);
System.out.println();
System.out.printf("%x\n",11);
System.out.printf("%o\n",11);
System.out.printf("%.2e",1.59e+02);
System.out.println();
System.out.printf("%,.2f\n",x);
System.out.printf("%(d",-1000);
System.out.println("左对齐");
System.out.printf("%-8d",100);
System.out.println("分组分隔符");
System.out.printf("%,d",333333333);
System.out.println("数字前补0");
System.out.printf("%010d",100);
}
}
System.out.printf("%1$d %2$f",3,0.3);
这个能指定要格式化的参数索引。
System.out.printf("%d%<x",11);
将以十进制和十六进制打印同一个数值。
java文件输入和输出
package chapter_3;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Scanner;
public class file_in_out {
public static void main(String[] args)throws IOException {
Scanner in = new Scanner(Path.of("D:\\javacode\\day01\\1.txt"), StandardCharsets.UTF_8);
PrintWriter out=new PrintWriter("D:\\javacode\\day01\\1.txt",StandardCharsets.UTF_8);
out.print("hello world");
}
}
块作用域
在c++中,可以在嵌套的块中重定义一个变量,在内层定义的变量会覆盖在外层定义的变量,这就有可能带来编程错误,因此java不允许这样做。
带标签的break
package chapter_3;
import java.util.Scanner;
public class label_break {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n;
n=scanner.nextInt();
read_date:
while(n>0)
{
if(n==5)
{
break read_date;
}
n--;
}
if(n==5)
{
System.out.println("yes");
}else
{
System.out.println("no");
}
}
}
package chapter_3;
import java.util.Scanner;
public class label_break {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n;
n=scanner.nextInt();
int j=0;
read_date:
while(n>0)
{
if(n==5) break;
j=10;
while(j>0) {
if (j == 5) {
n=5;
continue read_date;
}
j--;
}
n--;
}
if(j==5)
{
System.out.println("yes");
}else
{
System.out.println("no");
}
}
}
带标签的continue.
大数
package chapter_3;
import java.math.BigInteger;
import java.util.Scanner;
public class big_number {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("How many numbers do you need to draw?");
int k=scanner.nextInt();
System.out.print("What is the highest number you can draw?");
int n=scanner.nextInt();
BigInteger lotteryodds=BigInteger.valueOf(1);
for(int i=1;i<=k;++i)
{
lotteryodds=lotteryodds.multiply(BigInteger.valueOf(n-i+1)).divide(BigInteger.valueOf(i));
}
System.out.println("Yours odds are 1 in "+lotteryodds+". Good luck!");
}
}
java数组
package chapter_3;
import java.util.Arrays;
public class array_shuzu {
public static void main(String[] args) {
int[] a = new int[100];
int[] b = {2, 3, 4, 1, 2};
//匿名数组
a = new int[]{1, 2, 3, 4, 5};
///允许定义长度为0的数组
int[] c = new int[0];
int[] d = {};
int[] e = new int[]{};
for each 循环
for (int i : a) {
System.out.println(i);
}
System.out.println(Arrays.toString(a));
System.out.println("数组的拷贝");
int[] f = Arrays.copyOf(a, 2 * a.length);
for (int i : f) {
System.out.println(i);
}
}
}
二维数组
package chapter_3;
import java.util.Arrays;
public class twodimensional_array {
public static void main(String[] args) {
double[][]balances=new double[3][3];
int[][]b={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
System.out.println(Arrays.deepToString(b));
for(int[]i:b)
{
for(int j:i)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
类
- 在一个源文件中,只能有一个公共类,但是可以有任意数目的非公共类。-
- 构造器总是伴随着new操作符一起调用
- java对象都是在堆中构造的。
- 利用var定义变量,例如。
var a=1;
var b="flaksdjflsa";
var只能用于方法中的局部变量,参数和字段的类型必须声明。
接受null引用的宽容型和严格型
package chapter_4;
import java.util.Objects;
public class var_1 {
public static void main(String[] args) {
var a=1;
var b="flaksdjflsa";
Student one=new Student(null);
}
}
class Student
{
private String name;
Student(String n)
{
Objects.requireNonNull(n,"The name cannot be null");
name=n;
}
}
这会直接抛出一个异常。
宽容型就改成这样
name=Objects.requireNonNullElse(n,"unknown");
java中的方法必须在类的内部定义,但并不代表他们是内联方法。