Я только что попытался запрограммировать калькулятор. Он может рассчитать синус и другие вещи (которые работают нормально), но у меня есть проблема с кнопкой равных. Например, после ввода 5 + 5 и нажатия кнопки = я получаю эту ошибку:
Exception in thread "AWT-EventQueue-1" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at Taschenrechner2.button15_ActionPerformed(Taschenrechner2.java:349)
at Taschenrechner2$15.actionPerformed(Taschenrechner2.java:172)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Вот код для моего полного апплета:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Locale.*;
import java.util.Scanner.*;
public class Taschenrechner2 extends JApplet {
// Anfang Attribute
private TextField textField1 = new TextField();
private Button button1 = new Button();
private Button button2 = new Button();
private Button button3 = new Button();
private Button button4 = new Button();
private Button button5 = new Button();
private Button button6 = new Button();
private Button button7 = new Button();
private Button button8 = new Button();
private Button button9 = new Button();
private Button button10 = new Button();
private Button button11 = new Button();
private Button button12 = new Button();
private Button button13 = new Button();
private Button button14 = new Button();
private Button button15 = new Button();
private TextField textField2 = new TextField();
public void button15_ActionPerformed(ActionEvent evt) {
double x = new java.util.Scanner( textField1.getText() ).nextDouble();
char operator = new java.util.Scanner( textField1.getText() ).nextLine().charAt( 0 );
double y = new java.util.Scanner( textField1.getText() ).nextDouble();
switch ( operator )
{
case '+':
double c=x+y;
String s=String.valueOf(c);
textField1.setText(s);
break;
case '-':
double d=x-y;
String g=String.valueOf(d);
textField1.setText(g);
break;
case '*':
double e=x*y;
String h=String.valueOf(e);
textField1.setText(h);
break;
case '/':
double f=x/y;
String i=String.valueOf(f);
textField1.setText(i);
break;
}
}
Я был бы очень признателен, если бы вы могли мне помочь.