我不知道它是否在任何地方,但我只是想知道是否有任何解决方法,int无法解除引用错误。这就是我到目前为止所拥有的
int ch;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation"); // Shows what numbers will do what operation when input
ch = Integer.parseInt (JOptionPane.showInputDialog("Please Choose Operation")); // Prompts user to input a number 1-4 for the operation they want to do
switch (ch) {
case 1:
JOptionPane.showMessageDialog(null,"You Chose Additon"); // Adds up all ten integers from above
JOptionPane.showMessageDialog(null, one + two + three + four + five + six + seven + eight + nine + ten);
break;
case 2:
JOptionPane.showMessageDialog(null,"You Chose Subtraction"); // Subtracts all 10 numbers, resulting answer can be negative
JOptionPane.showMessageDialog(null, one - two - three - four - five - six - seven - eight - nine - ten);
break;
case 3:
JOptionPane.showMessageDialog(null, "You Chose Multiplication Do not use zeroes"); // Multiplies all 10 integers, cannot use 0's since answer would be 0
JOptionPane.showMessageDialog(null, one * two * three * four * five * six * seven * eight * nine * ten);
break;
case 4:
JOptionPane.showMessageDialog(null,"You Chose Division Can't Divide by 0"); // Divdes all numbers, cannot use 0's because no dividing by 0
JOptionPane.showMessageDialog(null, one / two / three / four / five / six / seven / eight / nine / ten);
break;
default:
JOptionPane.showMessageDialog(null,"You have to choose an operation"); // If no operation is chosen, Program ceases and closes
break;
我想知道是否有任何办法可以强制你输入操作。我试着做了
boolean validInput2 = false;
while (!validInput){
if (ch.endswith(" "));
但是这看起来不起作用(我知道这不是整个代码,我只是一直陷入取消引用错误)是否有什么我可以做的来解决这个错误并仍然有代码工作?
答案 0 :(得分:3)
它为我提供了一些小修复。
int ch;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation"); // Shows what numbers will do what operation when input
ch = (int)JOptionPane.showInputDialog("Please Choose Operation").charAt(0); // Prompts user to input a number 1-4 for the operation they want to do
switch (ch) {
case 1:
JOptionPane.showMessageDialog(null,"You Chose Additon"); // Adds up all ten integers from above
JOptionPane.showMessageDialog(null, one + two + three + four + five + six + seven + eight + nine + ten);
break;
case 45:
JOptionPane.showMessageDialog(null,"You Chose Subtraction"); // Subtracts all 10 numbers, resulting answer can be negative
JOptionPane.showMessageDialog(null, one - two - three - four - five - six - seven - eight - nine - ten);
break;
case 3:
JOptionPane.showMessageDialog(null, "You Chose Multiplication Do not use zeroes"); // Multiplies all 10 integers, cannot use 0's since answer would be 0
JOptionPane.showMessageDialog(null, one * two * three * four * five * six * seven * eight * nine * ten);
break;
case 4:
JOptionPane.showMessageDialog(null,"You Chose Division Can't Divide by 0"); // Divdes all numbers, cannot use 0's because no dividing by 0
JOptionPane.showMessageDialog(null, one / two / three / four / five / six / seven / eight / nine / ten);
break;
default:
JOptionPane.showMessageDialog(null,"You have to choose an operation"); // If no operation is chosen, Program ceases and closes
break;
}
如果你想用int切换,你需要切换字符的ascii代码,就像我对' - '字符所做的那样。你也可以像这样切换字符本身:
char ch = JOptionPane.showInputDialog("Please Choose Operation").charAt(0);
然后像这样切换
switch (ch) {
case '+':
有了这个,你必须输入字符+ - * /才能使程序正常工作。我希望我能告诉你的是可以理解的,我希望你能找到它。
你可以强制用户输入这样的字符:
char ch = 0;
JOptionPane.showMessageDialog (null, "1=+ 2=- 3=x 4=/ Please Choose Operation");
while(ch!='+'&&ch!='-'&&ch!='*'&&ch!='/'){
ch = JOptionPane.showInputDialog("Please Choose Operation").charAt(0);
}
- > switch-statement追逐while-loop
答案 1 :(得分:0)
另一种选择是创建从操作到消息的映射,然后从地图中立即生成结果。
HashMap<Integer, String> map = new HashMap<>();
map.put(43, "You chose Addition" + (1+2+4));
map.put(45, "You chose Subtraction" + (1-2-4));
map.put(47, "You chose Division" + (1/2));
map.put(42, "You chose Multiplication" + (1*2*4));
String operations =Arrays.toString(map.keySet().stream().map(k -> (char)(int)k).toArray());
int val = JOptionPane.showInputDialog(null,"enter operation: "+ operations).charAt(0);
JOptionPane.showMessageDialog(null, map.get(val));
更新 为了避免计算结果,我们可以使用Callable:
HashMap<Integer, Callable<String>> map = new HashMap<>();
map.put(43, () -> {
System.out.println("hello");
return "You choose Addition" + (1+2+3);
});
map.put(45, () -> "You chose Subtraction" + (1-2-4));
map.put(47, ()->"You chose Division" + (1/2));
map.put(42, () -> "You chose Multiplication" + (1*2*4));
String operations =Arrays.toString(map.keySet().stream().map(k -> (char)(int)k).toArray());
int val = JOptionPane.showInputDialog(null,"enter operation: "+ operations).charAt(0);
JOptionPane.showMessageDialog(null, map.get(val).call());