評價: 2 回應: 3 閱覽: 225
置頂

JAVA程式的問題

請用 button 、textfield、if…elseif…elseif 結構寫一個程式

並且完成以下要求: 

1. 輸入一個0 100的分數

2. 當分數大於90分時,輸出甲 

3. 當分數介於80 89時,輸出乙 

4. 當分數介於70 79時,輸出丙 

5. 當分數介於0 69時,輸出丁 

熱門回應

package tw.com.1111;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class Fraction extends JFrame {

 /**
  * User 1111
  */
 private static final long serialVersionUID = 1L;
 private JPanel contentPane;
 private JTextField textField;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     Fraction frame = new Fraction();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the frame.
  */
 public Fraction() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 300);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel lblNewLabel = new JLabel("請輸入0-100");
  lblNewLabel.setFont(new Font("新細明體", Font.PLAIN, 18));
  lblNewLabel.setBounds(149, 10, 101, 29);
  contentPane.add(lblNewLabel);
  
  textField = new JTextField();
  textField.setHorizontalAlignment(SwingConstants.CENTER);
  textField.setFont(new Font("新細明體", Font.PLAIN, 18));
  textField.setBounds(149, 42, 96, 34);
  contentPane.add(textField);
  textField.setColumns(10);
  
  JLabel result = new JLabel("");
  result.setHorizontalAlignment(SwingConstants.CENTER);
  result.setForeground(Color.RED);
  result.setFont(new Font("新細明體", Font.BOLD, 18));
  result.setBounds(100, 173, 230, 39);
  contentPane.add(result);
  
  JButton btnNewButton = new JButton("submit");
  btnNewButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    
    int num = Integer.parseInt(textField.getText());
    
    String word="";

     if(num>=90 && num<=100){
      word="甲";
     }else if(num>=80 && num<=89){
      word="乙";
     }else if(num>=70 && num<=79){
      word="丙";
     }else if(num>=0 && num<=69){
      word="丁";
     }else if(num>100){
      word="輸入錯誤";
     }
     
     result.setText("結果:"+num+word);
     

   }
  });
  
  btnNewButton.setBounds(149, 100, 101, 23);
  contentPane.add(btnNewButton);

 }
 

}

import javax.swing.JOptionPane; 
public class Grade { 
int a; 
String number; 
public void get_Score(){ 
number = JOptionPane.showInputDialog("請輸入0~100(含)之間分數"); 
a= Integer.parseInt(number); 
} 
public void put_Class(){ 
if(a>90){ 
JOptionPane.showMessageDialog(null,"你的分數等級 是: 甲等"); 
} else if(a>80 &&a<89){ 
JOptionPane.showMessageDialog(null,"你的分數等級 是: 乙等"); 
} else if(a>70 &&a<79){ 
JOptionPane.showMessageDialog(null,"你的分數等級 是: 丙等"); 
} else if(a>0 &&a<69){ 
JOptionPane.showMessageDialog(null,"你的分數等級 是: 丁等"); 
} else{ 
JOptionPane.showMessageDialog(null,"輸入錯誤"); 
} 
} 
public static void main(String[] args){ 
Grade gr =new Grade(); 
gr.get_Score(); 
gr.put_Class(); 
} 
} 

 

以下code給你做參考

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 


public class Y2820 { 
public static void main(String[] args) { 
JFrame jf = new JFrame("Test"); 
jf.setSize(200, 200); 
jf.setLocation(300, 300); 
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

JLabel jl = new JLabel("Insert an integer(0~100): "); 
jf.add(jl, BorderLayout.NORTH); 

final JTextField jtf = new JTextField(); 
jf.add(jtf, BorderLayout.CENTER); 

JButton jb = new JButton("OK"); 
jb.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent ae) { 
int x = Integer.parseInt(jtf.getText()); 
String s = ""; 
if (x >= 90) s = "甲"; 
else if (x >= 80 && x < 90) s = "乙"; 
else if (x >= 70 && x < 80) s = "丙"; 
else if (x >= 0 && x < 70) s = "丁"; 
JOptionPane.showMessageDialog(null, s); 
} 
}); 
jf.add(jb, BorderLayout.SOUTH); 

jf.setVisible(true); 
} 
}

 

會員登入 (先登入會員才能回覆留言喔!)

Facebook留言