首页 > 后端开发 > 正文

在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢

2024-04-03 15:10:01 | 我爱编程网

今天我爱编程网小编为大家带来了在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢,希望能帮助到大家,一起来看看吧!

本文目录一览:

在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢

用java编写一个程序,程序运行时弹出一个输入对话框,用户使用该对话

package cn.fu;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JOptionPane;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.Window.Type;

public class Login extends JFrame {

private JPanel contentPane;

private JTextField textField;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

Login frame = new Login();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public Login() {

setTitle("工具");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setToolTipText("");

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

textField = new JTextField();

textField.setBounds(121, 86, 194, 21);

contentPane.add(textField);我爱编程网

textField.setColumns(10);

JLabel lblNewLabel = new JLabel("请输入10位数以内的字符串");

lblNewLabel.setBounds(145, 59, 194, 15);

contentPane.add(lblNewLabel);

JButton btnNewButton = new JButton("确定");

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String ca = textField.getText();

int n = ca.length();

if (n > 10) {

JOptionPane.showMessageDialog(null, "对不起,您输入的字符串长度超过10",

"错误提示", JOptionPane.ERROR_MESSAGE);

} else if (n >= 0 || n <= 10) {

JOptionPane.showMessageDialog(null, "字符串长度为" + n, "提示",

JOptionPane.PLAIN_MESSAGE);

}

}

});

btnNewButton.setBounds(172, 130, 93, 23);

contentPane.add(btnNewButton);

}

}

在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢

在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢

import java.util.Scanner;

public class aa {

public static void main(String[] args) {

int ri=0;

Scanner sc =new Scanner(System.in );

System.out.println("请输入一个数值:");

ri=sc.nextInt();

System.out.println(ri);

}

}

/*

1: 最简单的 控制台输入,然后在打印出来

2: 你可以查看JDK 直接查找Scanner 会找到很多方法

3: 必须学会使用JDK,而且要常看

*/

在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢

用java编写有输入和输出功能的控制台程序,只在main函数中写程序(越简

我爱编程网(https://www.52biancheng.com)小编还为大家带来用java编写有输入和输出功能的控制台程序,只在main函数中写程序(越简的相关内容。

import java.io.*;

public class Io

{

public static void main(String[] args)throws IOException

{

int a;

System.out.print("请输入一个整数:");

BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));

a=Integer.parseInt(strin.readLine());

System.out.println("输入的数是:"+a);

Double b;

System.out.print("请输入一个double型:");

BufferedReader strin1=new BufferedReader(new InputStreamReader(System.in));

b=Double.parseDouble(strin.readLine());

System.out.println("输入的double数是:"+b);

String c;

System.out.print("请输入一个字符型:");

BufferedReader strin2=new BufferedReader(new InputStreamReader(System.in));

c=strin.readLine();

System.out.println("输入的字符是:"+c);

}

}

以上就是我爱编程网整理的在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢相关内容,想要了解更多信息,敬请查阅我爱编程网。更多相关文章关注我爱编程网:www.52biancheng.com

免责声明:文章内容来自网络,如有侵权请及时联系删除。
与“在Java中,若要由用户在程序运行时输入一个数据赋给某一个变量该怎么做呢”相关推荐