Wie man mathematische Operationen mit Blattelementen ausführtJava

Java-Forum
Anonymous
 Wie man mathematische Operationen mit Blattelementen ausführt

Post by Anonymous »

Ich habe drei Listen, List1 enthält den ersten Wert, List2 den zweiten Wert und der dritte enthält Zeichen. Wie mache ich mathematische Operationen basierend auf den dort gespeicherten Zeichen. Arbeiten Sie mit den Nummern aus List1 und List2.Da, wenn das Zeichen +ist, müssen Sie ein Element der Liste 1 und List2 hinzufügen. Die Listen wurden mit Multithreading gefüllt, und ich weiß nicht, ob dies einen Effekt hat oder nicht. Ich brauche all dies, um einen Konsolenrechner zu implementieren. Hilfe bitte, schauen Sie sich in meinem Betriebscode an: < /p>

Code: Select all

import java.util.ArrayList;
import java.util.List;

public class Calculate {

public static List converter(List list) {
List doubleList = new ArrayList();
for (String s : list) {
doubleList.add(Double.parseDouble(s));
}
return doubleList;
}

public static Double addition(Double a, Double b) {
return a + b;
}

public static Double subtraction(Double a, Double b) {
return a - b;
}

public static Double multiplication(Double a, Double b) {
return a * b;
}

public static Double division(Double a, Double b) {
return a / b;
}

public static Double operation(List doubles1, List doubles2, List symbols, Double result) {
Double a = 0.0;
Double b = 0.0;
for (int i = 0; i < doubles1.size(); i++) {
a = doubles1.get(i);
for (int j = 0; j < doubles2.size(); j++) {
b = doubles2.get(j);
if (symbols.get(i).equals("+")) {
result = Calculate.addition(a, b);
} else if (symbols.get(i).equals("-")) {
result = Calculate.subtraction(a, b);
}
}
}
return result;
}

public static void main(String[] args) {
Double result = 0.0;
List doubles = new ArrayList();
List doubles2 = new ArrayList();
doubles.add(33.0);
doubles2.add(17.0);
doubles.add(50.0);
doubles2.add(50.0);
doubles.add(50.0);

List symbols = new ArrayList();
symbols.add("+");
symbols.add("+");
symbols.add("-");
symbols.add("-");
System.out.println(operation(doubles, doubles2, symbols, result));
}
}
< /code>
Schauen Sie nach meiner Klasse zum Hinzufügen von Liste: < /p>

Java.util. ; [url=viewtopic.php?t=18708]Importieren[/url] Sie java.util.concurrent.private static final List firstValues = Collections.synchronizedList(new ArrayList());
private static final List symbols = Collections.synchronizedList(new ArrayList());
private static final List secondValues = Collections.synchronizedList(new ArrayList());

private static final BlockingQueue q1 = new LinkedBlockingQueue();
private static final BlockingQueue qs = new LinkedBlockingQueue();
private static final BlockingQueue q2 = new LinkedBlockingQueue();

private static volatile boolean running = true;

public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);

// Reader thread: routes inputs to correct queues
Thread reader = new Thread(() -> {
while (running) {
String input = scanner.nextLine();
if (input.equals("=")) {
running = false;
q1.offer("STOP");
qs.offer("STOP");
q2.offer("STOP");
break;
}

if (input.startsWith("1:")) {
q1.offer(input.substring(2));
} else if (input.startsWith("s:")) {
qs.offer(input.substring(2));
} else if (input.startsWith("2:")) {
q2.offer(input.substring(2));
} else {
System.out.println("Invalid input.  Use 1:, s:, or 2");
}
}
});

// Each worker reads only from its own queue
Thread worker1 = new Thread(() -> process(q1, firstValues, "1st"));
Thread workerS = new Thread(() -> process(qs, symbols, "Symbol"));
Thread worker2 = new Thread(() -> process(q2, secondValues, "2nd"));

reader.start();
worker1.start();
workerS.start();
worker2.start();

reader.join();
worker1.join();
workerS.join();
worker2.join();
}

private static void process(BlockingQueue queue, List list, String name) {
while (running || !queue.isEmpty()) {
try {
String value = queue.poll(100, TimeUnit.MILLISECONDS);
if (value == null || value.equals("STOP")) continue;
list.add(value);
System.out.println("[" + name + "] Added: " + value);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
} }

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post