简单代码gui确认对话框_20160909

本文通过多个实例介绍了Java编程的基础概念,包括布尔类型测试、日期显示、数值相加、随机数练习及选择结构的应用。每个示例均附带了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package Chapter03;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.JOptionPane;

public class chapter03_example01 {

    public static void main(String[] args) {
        //booleantypetest();
        //dateshijianxianshi();
        //lianggeshuxiangjia();
        //randomexercise();
        //switchyuju();
        confirmed();
    }

    public static void booleantypetest() {
        boolean right = true, wrong = false;
        char c1 = 'a', c2 = 'A';
        if (c1 > c2) {
            System.out.println("a > A is " + right);
            System.out.println(c1 + "\t" + (int) c1);
            System.out.println(c2 + "\t" + (int) c2);
        } else
            System.out.println("a > A is " + wrong);
        System.out.println(2 < 0);// 输出boolean类型结果

        int xx = 101;
        boolean even1 = true;
        if (xx % 2 == 0)
            even1 = true;
        else
            even1 = false;
        System.out.println("even1 = " + even1);
        // 可以代替上面的if语句判断,将判断结果直接赋值到boolean类型的变量中。
        boolean even2 = xx % 2 == 0;
        System.out.println("even2 = " + even2);
    }

    public static void dateshijianxianshi(){
        /*
         * System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数
         * Date();
         * */
        Date nowtime = new Date(System.currentTimeMillis());
        SimpleDateFormat dateformat = new SimpleDateFormat("z:y-MM-dd kk:mm:ss");
        String datenow = dateformat.format(nowtime);
        System.out.println(datenow);
    }

    public static void lianggeshuxiangjia() {
        System.out.println(System.currentTimeMillis());
        int number1 = (int) (System.currentTimeMillis() % 10);
        int number2 = (int) (System.currentTimeMillis() * 7 % 10);
        String output = number1 + " + " + number2 + " = ?";
        String answerString = JOptionPane.showInputDialog(null, output, "两个数的和", JOptionPane.QUESTION_MESSAGE);
        int answer = Integer.parseInt(answerString);
        String output2 = number1 + " + " + number2 + " = " + (number1 + number2);
        String output3 = number1 + " + " + number2 + " != " + answer;
        if ((number1 + number2) == answer) {
            JOptionPane.showMessageDialog(null, "回答正确!\n" + output2, "结果判断", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(null, "回答错误!\n" + output3 + "\n正确答案是\n" + output2, "结果判断",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static void randomexercise(){
        double xx = Math.random();
        System.out.println(xx);
        int number1 = (int)(Math.random()*100%10);
        int number2 = (int)(Math.random()*100%10);
        String output1 = number1 + " + " +number2;
        String answerString = JOptionPane.showInputDialog(null,output1 + " = ?","问题",JOptionPane.QUESTION_MESSAGE);
        int answer1 = Integer.parseInt(answerString);
        int answer2 = number1 + number2;

        if(answer1 == answer2){
            JOptionPane.showMessageDialog(null, output1+" = " + answer2, "正确", JOptionPane.INFORMATION_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, output1 + " != " + answer1, "错误", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static void switchyuju(){
        int number1 = 90;
        switch(number1)
        {
        case 10: System.out.println("case 10");
        break;
        case 100: System.out.println("case 100");
        break;
        default: System.out.println("no case");
        System.exit(0);
        }
    }

    //(GUI)确认对话框
    public static void confirmed(){
        int confirm = JOptionPane.showConfirmDialog(null, "confirmed?", "标题", JOptionPane.INFORMATION_MESSAGE);
        System.out.println(confirm);
        System.out.println(JOptionPane.YES_OPTION);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值