本文目錄一覽:
用JAVA語言編寫一個程序,要求如下:
import java.util.Random;
import java.util.Scanner;
public class T {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int difficulty;//難度
int mode;//運算類型
int answer;//答案
int amount;//挑戰題目數量
int score = 0;//得分
System.out.println(“請輸入難度(1:一位數、2:兩位數、3:三位數):”);
difficulty = in.nextInt();
System.out.println(“請輸入運算類型(1:加、2:減、3:乘、4:除):”);
mode = in.nextInt();
System.out.println(“請輸入想要挑戰的題目數量:”);
amount = in.nextInt();
Random random = new Random();
for (int i = 0; i amount; i++) {
if (difficulty == 1) {
if (mode == 1) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
int x = random.nextInt(10);
int y = random.nextInt(10);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” – ” + y + ” = “);
answer = in.nextInt();
if (answer == (x – y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x – y) + “\n”);
}
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else if (difficulty == 2) {
if (mode == 1) {
int x = random.nextInt(100);
int y = random.nextInt(100);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else if (difficulty == 3) {
if (mode == 1) {
int x = random.nextInt(1000);
int y = random.nextInt(1000);
System.out.println(“第” + i + “題:”);
System.out.print(x + ” + ” + y + ” = “);
answer = in.nextInt();
if (answer == (x + y)) {
System.out.println(“答對了\n”);
score++;
} else {
System.out.println(“答錯了,答案是:” + (x + y) + “\n”);
}
} else if (mode == 2) {
} else if (mode == 3) {//乘法
} else if (mode == 4) {//除法 考慮小數的問題
} else {
throw new Exception(“運算類型輸入值不合法”);
}
} else {
throw new Exception(“難度輸入值不合法”);
}
}
System.out.println(“挑戰結束,您的分數為:” + score);
}
}
我就只舉了加法的例子,其他運算的寫法都是類似的,你照葫蘆畫瓢即可
運行結果:
Java程序設計語言是什麼意思?
一、Java語言是一種目前正在全世界得到迅速傳播與廣泛應用的面向對象的計算機程序設計語言。
二、基礎篇介紹了Java作為一種程序設計語言所具有的基本組成、語法規則、例外和線程等內容。應用篇介紹了若干類JavaApplet的實際應用。
三、本書內容詳實、資料豐富、結構有致、由淺及深。既可作為初學者的入門教材,也可作為深入學習者的輔助資料,還可以作為編程人員的一本工具參考書。
補充:
《JAVA程序設計語言》 是清華大學出版社出版的圖書, ISBN是9787302025375。
如何用JAVA語言編寫計算器小程序?
具體代碼如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator extends JFrame implements ActionListener {
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//對圖形組件實例化
jf=new JFrame(“任靜的計算器1.0:JAVA版”);
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton(“清除”);
jtf=new JTextField(25);
jtf.setEditable(false);
String str=”123+456-789*0.=/”;
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件監聽
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText(“”);
}
});
}
//事件處理
public void actionPerformed(ActionEvent e) {
//在這裡完成事件處理 使計算器可以運行
String action=e.getActionCommand();
if(action==”+”||action==”-“||action==”*”||action==”/”){
}
}
public void setFontAndColor(){
Font f=new Font(“宋體”,Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
java語言如何編寫程序?
如下:
(1) 一個Java語言開發工具包(Java Devekopment Kit),其中包括Java 編譯器和Java運行環境。
(2) 一份Java語言API文檔,目前版本的Java語言API文檔同樣可以免費。
(3) 一個Java語言集成開發環境,能夠在其中編輯Java代碼,並且進行編譯與調試。推薦使用的集成開發環境是JCreator。
建議去找老師讓老師 帶你做 項目 我這幾天正在做的 感覺收穫好多的 上課學到的東西其實更本就不夠用的 只有實踐了 你才能知道自己的差距
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/231519.html