目录
1.输入
Scanner
java.util.Scanner
import javax.swing.*;
import java.util.Scanner;
public class Output3 {
public static void main(String[] args) {
String n = JOptionPane.showInputDialog("请输入姓名");
System.out.println("欢迎:" + n);
//输入Scanner
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.nextLine();
System.out.print("请输入年龄:");
int age = sc.nextInt();
System.out.println();
//System.out.println("欢迎:" + name);
System.out.printf("姓名:%s,年龄:%d岁。%n", name, age);
}
}
2.输出
import javax.swing.*;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class Output4 {
public static void main(String[] args) {
System.out.print(2 * 3);
System.out.print(2 * 3);
System.out.println();
System.out.println(2 * 3);
System.out.println(2 * 3);
System.err.println("success");
//JOptionPane.showMessageDialog(null, "hello world", "商城平台",JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null, "hello world", "商城平台",JOptionPane.WARNING_MESSAGE);
//JOptionPane.showMessageDialog(null, "hello world", "商城平台",JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog(null, "hello world", "商城平台",JOptionPane.QUESTION_MESSAGE);
//信息内容输出到文件中,将字符信息写入文件,(了解,跟io有关)
try (var out = new PrintWriter(new FileOutputStream("user.txt"))) {
out.write("hello world 中文\r\n");
} catch (Exception e) {
}
}
}
控制输出颜色
import java.util.Random;
public class Output5 {
public static void main(String[] args) {
Random rand = new Random();
for (int i = 1; i <= 55; i++) {
System.out.printf("\033[%d;%dm hello world \033[0m ", rand.nextInt(30,
39), rand.nextInt(40, 49));
if (i % 5 == 0) {
System.out.println();
System.out.println();
}
}
//控制输出颜色 背景 前景
System.out.println("\033[30m hello 30 world \033[0m");
System.out.println("\033[31m hello 31 world \033[0m");
System.out.println("\033[32m hello 32 world \033[0m");
System.out.println("\033[33m hello 33 world \033[0m");
System.out.println("\033[34m hello 34 world \033[0m");
System.out.println("\033[35m hello 35 world \033[0m");
System.out.println("\033[36m hello 36 world \033[0m");
System.out.println("\033[37m hello 37 world \033[0m");
System.out.println("\033[38m hello 38 world \033[0m");
System.out.println("\033[39m hello 39 world \033[0m");
//文字颜色为30 黑色 背景为44 紫色
System.out.println("\033[30;44m hello 30 world \033[0m");
System.out.println("\033[41m hello 31 world \033[0m");
System.out.println("\033[42m hello 32 world \033[0m");
System.out.println("\033[43m hello 33 world \033[0m");
System.out.println("\033[44m hello 34 world \033[0m");
System.out.println("\033[45m hello 35 world \033[0m");
System.out.println("\033[46m hello 36 world \033[0m");
System.out.println("\033[47m hello 37 world \033[0m");
System.out.println("\033[48m hello 38 world \033[0m");
System.out.println("\033[49m hello 39 world \033[0m");
}
}
输出效果