当前位置: 技术问答>java相关
请问,怎样得到用户输入的一个整数?
来源: 互联网 发布时间:2015-11-09
本文导语: 如题。 | int s=0; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str = ""; str = in.readLine(); s = Inte...
如题。
|
int s=0;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
str = in.readLine();
s = Integer.parseInt(str);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
str = in.readLine();
s = Integer.parseInt(str);
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
|
try{
int iget = Integer.parseInt(request.getParameter("int"));
}catch(Exception ex){
System.out.println(ex.getMessage));
}
int iget = Integer.parseInt(request.getParameter("int"));
}catch(Exception ex){
System.out.println(ex.getMessage));
}
|
自己根据需要改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.JOptionPane;
public class textField extends JFrame{
JTextField text;
JButton button1;
textField(String s){
super(s);
text=new JTextField(12);
text.addKeyListener(new KeyListener1());
button1=new JButton("OK");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"save successful","OK",JOptionPane.INFORMATION_MESSAGE);
}
});
JPanel p=new JPanel();
p.setLayout(new FlowLayout());
p.add(text);
p.add(button1);
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
setSize(300,300);
c.add(p);
setVisible(true);
}
class KeyListener1 extends KeyAdapter{
public void keyReleased(KeyEvent ee){
int i=ee.getKeyCode();
if ((iee.VK_9) && i!=ee.VK_ENTER && i!=ee.VK_PERIOD && i!=ee.VK_BACK_SPACE && i!=ee.VK_KP_LEFT && i!=ee.VK_KP_RIGHT && i!=ee.VK_DELETE){
JOptionPane.showMessageDialog(null,"intput Error","Error",JOptionPane.ERROR_MESSAGE);
text.setText("0");
return;}
}
}
public static void main(String args[]){
new textField("test");
char c='\';
int n=c;
System.out.println(n);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.JOptionPane;
public class textField extends JFrame{
JTextField text;
JButton button1;
textField(String s){
super(s);
text=new JTextField(12);
text.addKeyListener(new KeyListener1());
button1=new JButton("OK");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null,"save successful","OK",JOptionPane.INFORMATION_MESSAGE);
}
});
JPanel p=new JPanel();
p.setLayout(new FlowLayout());
p.add(text);
p.add(button1);
Container c=this.getContentPane();
c.setLayout(new FlowLayout());
setSize(300,300);
c.add(p);
setVisible(true);
}
class KeyListener1 extends KeyAdapter{
public void keyReleased(KeyEvent ee){
int i=ee.getKeyCode();
if ((iee.VK_9) && i!=ee.VK_ENTER && i!=ee.VK_PERIOD && i!=ee.VK_BACK_SPACE && i!=ee.VK_KP_LEFT && i!=ee.VK_KP_RIGHT && i!=ee.VK_DELETE){
JOptionPane.showMessageDialog(null,"intput Error","Error",JOptionPane.ERROR_MESSAGE);
text.setText("0");
return;}
}
}
public static void main(String args[]){
new textField("test");
char c='\';
int n=c;
System.out.println(n);
}
}
|
try{
int i=Integer.parseInt(text.getText());
}
catch(Exception e){System.out.print("error");}
int i=Integer.parseInt(text.getText());
}
catch(Exception e){System.out.print("error");}