请问java编程如何一次输入读取两个数值
写一个程序,让别人一次输入两个数值然后计算出两者相加多少:例如请输入两个数值:25两者之和为:7请问这个程序应该怎么写...
写一个程序,让别人一次输入两个数值然后计算出两者相加多少:
例如
请输入两个数值:2 5
两者之和为:7
请问这个程序应该怎么写 展开
例如
请输入两个数值:2 5
两者之和为:7
请问这个程序应该怎么写 展开
6个回答
展开全部
可以让用户输入两个数字,中间用空格间隔。
读取的时候用readLine()方法。读到数据之后,用字符串分割,subString(“ ”)方法就可以得到这两个数字组成的数组。
Scanner console = new Scanner(System.in);
System.out.print("输入两个数字用空格间隔:");
String nums = console.readLine();
String[] ss = nums.subString("\\s");
int num1 = Integer.parseInt(ss[0]);
int num2 = Integer.parseInt(ss[1]);
int result = num1+num2;
System.out.println(result);
没有调试,不过基本应该可以用 。直接放到main()方法中就能用
读取的时候用readLine()方法。读到数据之后,用字符串分割,subString(“ ”)方法就可以得到这两个数字组成的数组。
Scanner console = new Scanner(System.in);
System.out.print("输入两个数字用空格间隔:");
String nums = console.readLine();
String[] ss = nums.subString("\\s");
int num1 = Integer.parseInt(ss[0]);
int num2 = Integer.parseInt(ss[1]);
int result = num1+num2;
System.out.println(result);
没有调试,不过基本应该可以用 。直接放到main()方法中就能用
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Computer extends JFrame{
private Container contentPane = null;
private JPanel computerPanel = null;
private JPanel buttonPanel = null;
private JTextField sourceField1 = null;
private JTextField sourceField2 = null;
private JTextField resultField = null;
private JLabel addLabel = null;
private JLabel resultLabel = null;
private JButton submitButton = null;
private JButton resetButton = null;
public Computer(){
this.setSize(200,200);
this.setLocation(350,150);
this.init();
this.setVisible(true);
}
public void init(){
contentPane = this.getContentPanel();
contentPane.setLayout(new GridLayout(2,1));
computerPanel = new JPanel(new FlowLayout(20,20));
buttonPanel = new JPanel(new FlowLayout(50,25));
sourceField1 = new JTextField(5);
sourceField2 = new JTextField(5);
resultField = new JTextField(5);
resultField.setEnable(true);
addLabel = new JLabel("+",JLabel.CENTER);
resultLabel = new JLabel("=",JLabel.CENTER);
submitButton = new JButton("确定");
submitButton.addActionListener(new ActionListener(){
public void actionPerform(ActionEvent e){
try{
int source1 = Integer.parseInt(sourceField1.getText());
int source2 =Integer.parseInt(sourceField2.getText());
resultField.setText(String.valueOf(source1+source2));
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"请先输入数字");
}
catch(NullPointException e){
JOptionPane.showMessageDialog(null,"输入为空");
}
}
});
resetButton = new JButton("取消");
resetButton.addActionListener(new ActionListener(){
public void actionPerform(ActionEvent e){
sourceField1.setText("");
sourceField2.setText("");
resultField.setText("");
}
});
computerPanel.add(sourceField1);
computerPanel.add(addLabel);
computerPanel.add(sourceField2);
computerPanel.add(resultLabel);
computerPanel.add(resultField);
buttonPanel.add(submitButton);
buttonPanel.add(resetButton);
contentPanel.add(computerPanel);
contentpanel.add(buttonPanel);
}
public static void main(String args[])
{
Computer computer = new Computer();
}
}
Ps.有可能有小错误,(有些字母拼写错误)。因为没有编译器~-~
三个文本框,两个输入,一个输出,还有防错输入(Null)的功能
import java.awt.*;
import java.awt.event.*;
public class Computer extends JFrame{
private Container contentPane = null;
private JPanel computerPanel = null;
private JPanel buttonPanel = null;
private JTextField sourceField1 = null;
private JTextField sourceField2 = null;
private JTextField resultField = null;
private JLabel addLabel = null;
private JLabel resultLabel = null;
private JButton submitButton = null;
private JButton resetButton = null;
public Computer(){
this.setSize(200,200);
this.setLocation(350,150);
this.init();
this.setVisible(true);
}
public void init(){
contentPane = this.getContentPanel();
contentPane.setLayout(new GridLayout(2,1));
computerPanel = new JPanel(new FlowLayout(20,20));
buttonPanel = new JPanel(new FlowLayout(50,25));
sourceField1 = new JTextField(5);
sourceField2 = new JTextField(5);
resultField = new JTextField(5);
resultField.setEnable(true);
addLabel = new JLabel("+",JLabel.CENTER);
resultLabel = new JLabel("=",JLabel.CENTER);
submitButton = new JButton("确定");
submitButton.addActionListener(new ActionListener(){
public void actionPerform(ActionEvent e){
try{
int source1 = Integer.parseInt(sourceField1.getText());
int source2 =Integer.parseInt(sourceField2.getText());
resultField.setText(String.valueOf(source1+source2));
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null,"请先输入数字");
}
catch(NullPointException e){
JOptionPane.showMessageDialog(null,"输入为空");
}
}
});
resetButton = new JButton("取消");
resetButton.addActionListener(new ActionListener(){
public void actionPerform(ActionEvent e){
sourceField1.setText("");
sourceField2.setText("");
resultField.setText("");
}
});
computerPanel.add(sourceField1);
computerPanel.add(addLabel);
computerPanel.add(sourceField2);
computerPanel.add(resultLabel);
computerPanel.add(resultField);
buttonPanel.add(submitButton);
buttonPanel.add(resetButton);
contentPanel.add(computerPanel);
contentpanel.add(buttonPanel);
}
public static void main(String args[])
{
Computer computer = new Computer();
}
}
Ps.有可能有小错误,(有些字母拼写错误)。因为没有编译器~-~
三个文本框,两个输入,一个输出,还有防错输入(Null)的功能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Scanner input=new Scanner(System.in);
System.out.print("请输入两个操作数:");
num1=input.nextDouble();
num2=input.nextDouble();
System.out.println(num1+num2);
这样就可以直接相加了。
System.out.print("请输入两个操作数:");
num1=input.nextDouble();
num2=input.nextDouble();
System.out.println(num1+num2);
这样就可以直接相加了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不用SWING,简单一点的程序如下:
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("请输入第一个数字:");
double x=s.nextDouble();
System.out.println("请输入第二个数字:");
double y=s.nextDouble();
double result=x+y;
System.out.println("结果为:"+result);
}
}
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("请输入第一个数字:");
double x=s.nextDouble();
System.out.println("请输入第二个数字:");
double y=s.nextDouble();
double result=x+y;
System.out.println("结果为:"+result);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
count(x,y){
system.out.println(x+y);
}
system.out.println(x+y);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询