haha

2006年3月29日 #

java作业1

package temp;
/*1.编写一个Application,接收用户输入的一行字符串,并重复输出三行。
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;

public class Input extends JFrame implements ActionListener {
 /**
  *
  */
 private static final long serialVersionUID = 1L;

 static JTextField text_in;

 static JTextArea text_out;

 static JButton in;

 String s;

 public static void pain() {
  Input frame = new Input();
  frame.setLayout(null);
  frame.setTitle("Input");
  text_in = new JTextField("please input a string", 1);
  text_in.setBounds(10,10,300,30);
  frame.add(text_in);
  in = new JButton("in");
  in.setBounds(10,150,50,30);
  frame.add(in);
  in.addActionListener(frame);

  text_out = new JTextArea(3, 993);
  text_out.setBounds(10,50,300,90);
  frame.add(text_out);
  frame.pack();
  frame.setVisible(true);
  
 }

 public static void main(String args[]) throws IOException {
  Input.pain();
 }

 public void actionPerformed(ActionEvent e) {
  // TODO Auto-generated method stub
  if (e.getActionCommand().equals("in"))
   s = text_in.getText();
  text_out.setText(s + "\n" + s + "\n" + s);
 }
}
/*1.编写一个Application,接收用户输入的一行字符串,并重复输出三行。
 */
import java.io.*;
public class Input {
 public static void main(String args[]) throws IOException {
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  String s = bf.readLine();
  // for(int i=0;i<3;i++)
  // System.out.println(s);
  System.out.print(s + "\n" + s + "\n" + s);
 }
}
package temp;

/*2.编写一个程序由用户从键盘输入整数,计算它们的和,并显示结果。
 * 当用户输入了一个整数并按下回车键后,该数被程序读入并累加到总和中。
 */
import java.io.*;
public class InputSum {
 static int sum;
 public static void main(String args[]) throws IOException {
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  String ss = bf.readLine();
  String input=ss;
  int s = Integer.parseInt(ss);
  for (;;) {
   if (!ss.endsWith("q")) {
    s = Integer.parseInt(ss);
    sum = sum + s;
   } else {
    System.out.print(input+"="+sum);
    System.exit(0);
   }
   ss = bf.readLine();
   if(!ss.endsWith("q"))input+="+"+ss;
  }
 }
}
package temp;
/*2.编写一个程序由用户从键盘输入整数,计算它们的和,并显示结果。
 * 当用户输入了一个整数并按下回车键后,该数被程序读入并累加到总和中。
 */
import javax.swing.*;
public class InputSum2 {
 static String num1, num2;
 static int n1, n2,sum;

 public static void main(String args[]) {
  num1 = JOptionPane.showInputDialog("Please input a number");
  num2=JOptionPane.showInputDialog("please input second number");
  n1=Integer.parseInt(num1);
  n2=Integer.parseInt(num2);
  sum=n1+n2;
  JOptionPane.showMessageDialog(null, num1+"+"+num2+"="+sum);
 }
}

posted @ 2006-03-29 11:51 咖啡 阅读(206) | 评论 (0)编辑 收藏

仅列出标题