程序的输入输出

目录

1.输入

2.输出


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");
    }
}

输出效果

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值