我刚试过编程计算器。它可以计算窦和其他东西(工作正常),但我有一个等号按钮的问题。在我输入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)
以下是我的完整Applet的代码:
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;
}
}
如果你能帮助我,我将非常感激。
答案 0 :(得分:2)
首先是一些基本的编码问题:
现在提供具体信息
答案 1 :(得分:0)
如果您想使用扫描仪,则不应每次都重新启动。
您没有说出textField1
包含的内容。如果它包含这种格式的文本,请尝试此操作:“1.0 \ n + \ n2.0”(如果格式不同,请在某处解释):
Scanner sca = new Scanner(textField1.getText());
double x = sca.nextDouble();
sca.nextLine();
char operator = sca.nextLine().charAt( 0 );
double y = sca.nextDouble();
这可能只是让您更近一步并传递该异常。请务必在此处阅读其他优秀答案。
答案 2 :(得分:0)
每次重新调整扫描仪时,nextDouble()总会返回相同的值。另外,我有点好奇为什么你从不使用它时接收操作符。