[英]How to do tasks by reading a string from left to right, and then do tasks assigned to each character?
我對英語翻譯程序有一個簡單的邏輯表達式,該邏輯表達式接受用戶的輸入(命題和邏輯表達式)。
我的問題是我對如何檢查邏輯表達式一無所知,以便如果字符串以p開頭,則命題“ p”將顯示在輸出中,然后繼續檢查字符串的下一個內容,讓我們假設它使用符號“Λ”,則接下來應顯示單詞“ and”。
圖片 :
一旦完成,我不知道要解決的一個小問題是如何大寫結果翻譯的第一個字母,因為三個命題p,q和r中的任何一個都可以根據用戶的不同而排在首位投入。
編輯:我添加了代碼
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Dmep extends JFrame implements ActionListener
{
JLabel Lp, Lq, Lr, Le, La;
JTextField Tp, Tq, Tr, Te, Ta;
JButton Bp, Bq, Br, Bnot, Band, Bor, Bxor, Bimp, Bico, Btl;
Font font = new Font ("Calibri", Font.BOLD, 24);
public Dmep()
{
Container C = getContentPane();
C.setLayout(new FlowLayout());
C.setBackground(Color.green);
Lp = new JLabel("Proposition p: ");
Lq = new JLabel("Proposition q: ");
Lr = new JLabel("Proposition r: ");
Le = new JLabel("Logical Expression: ");
La = new JLabel("English Trasnlation: ");
Tp = new JTextField("The window is open", 100);
Tq = new JTextField("the program is running", 100);
Tr = new JTextField("the translator will work", 100);
Te = new JTextField("pΛqΛr", 100);
Ta = new JTextField("The window is open and the program is running and the translator will work.", 100);
Bp = new JButton("p");
Bq = new JButton("q");
Br = new JButton("r");
Bnot = new JButton("¬");
Band = new JButton("Λ");
Bor = new JButton("V");
Bxor = new JButton("⊕");
Bimp = new JButton("→");
Bico = new JButton("↔");
Btl = new JButton("T R A N S L A T E");
C.add(Lp);
C.add(Tp);
C.add(Lq);
C.add(Tq);
C.add(Lr);
C.add(Tr);
C.add(Le);
C.add(Te);
C.add(La);
C.add(Ta);
C.add(Bp);
C.add(Bq);
C.add(Br);
C.add(Bnot);
C.add(Band);
C.add(Bor);
C.add(Bxor);
C.add(Bimp);
C.add(Bico);
C.add(Btl);
Bp.addActionListener(this);
Bq.addActionListener(this);
Br.addActionListener(this);
Bnot.addActionListener(this);
Band.addActionListener(this);
Bor.addActionListener(this);
Bxor.addActionListener(this);
Bimp.addActionListener(this);
Bico.addActionListener(this);
Btl.addActionListener(this);
Ta.setEditable(false);
setSize(1250,200);
setTitle("Logical Expression to English Translator");
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String pp, pq, pr, pa, pe;
pp = Tp.getText();
pq = Tq.getText();
pr = Tr.getText();
pe = Te.getText();
pa = pp + pq + pr;
if (e.getSource() == Bp)
{
Te.setText(String.valueOf(pe) + "p");
}
if (e.getSource() == Bq)
{
Te.setText(String.valueOf(pe) + "q");
}
if (e.getSource() == Br)
{
Te.setText(String.valueOf(pe) + "r");
}
if (e.getSource() == Bnot)
{
Te.setText(String.valueOf(pe) + "¬");
}
if (e.getSource() == Band)
{
Te.setText(String.valueOf(pe) + "Λ");
}
if (e.getSource() == Bor)
{
Te.setText(String.valueOf(pe) + "V");
}
if (e.getSource() == Bxor)
{
Te.setText(String.valueOf(pe) + "⊕");
}
if (e.getSource() == Bimp)
{
Te.setText(String.valueOf(pe) + "→");
}
if (e.getSource() == Bico)
{
Te.setText(String.valueOf(pe) + "↔");
}
if (e.getSource() == Btl)
{
Ta.setText(String.valueOf(pa) + "");
}
}
public static void main(String[] args)
{
Dmep M = new Dmep();
M.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
因此,您想將各種字符串與各種字符相關聯。 這就是Map<Character, String>
的用途。 如果您不知道Map是什么,請閱讀Java集合教程和Map接口的javadoc 。
現在,您有了一個包含字符的字符串。 通過閱讀String的javadoc ,您應該能夠弄清楚如何遍歷該字符串的所有字符。 現在,您所需要做的就是從地圖中獲取與每個字符相對應的String,並將這些字符串連接在一起。
這是一個最小的示例(但是,如果您對流和lamda表達式不滿意,則可以用其他各種方式來實現它):
Map<Character, String> map = new HashMap<>();
map.put('a', "Hello");
map.put('b', "World");
String text = "abab";
String result = text.chars()
.mapToObj(i -> map.get((char) i))
.collect(Collectors.joining(" "));
System.out.println(result);
// prints Hello World Hello World
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.