我编写了一个程序来识别字符串的平方、平方根、数字的立方和反转,但我有一些错误,我不知道如何解决这个问题。
下面是我的代码
import javax.swing.JOptionPane;
import java.util.Scanner;
public class FLABWORK3_ABUEL
{
static Scanner Scan new = Scanner (System.in);
public static void main(String[] args)
{
String choice;
String num1;
String string;
String Inverse;
int choicee, num2, response, length;
double squareroot;
do {
choice = JOptionPane.showInputDialog("Main Menu" +
"\n 1. Square of a number" +
"\n 2. Square root a number" +
"\n 3. Cube of a number" +
"\n 4. Length of number" +
"\n 5. Inverse of a string");
choicee = Integer.parseInt(choice);
while (choicee > 5)
{
choice = JOptionPane.showInputDialog("Enter only 1-5!");
}
switch (choicee)
{
case 1:
num1 = JOptionPane.showInputDialog("Enter an number.");
num2 = Integer.parseInt(num1);
num2 = num2*num2 ;
JOptionPane.showMessageDialog(null, "The square of the number: " + num2);
break;
case 2:
num1 = JOptionPane.showInputDialog("Enter a number.");
squareroot = Integer.parseInt(num1);
squareroot = Math.sqrt(squareroot);
JOptionPane.showMessageDialog(null, "Square root is: " + squareroot);
break;
case 3:
num1 = JOptionPane.showInputDialog("Enter a number.");
num2 = Integer.parseInt(num1);
num2 = num2*(num2*num2);
JOptionPane.showMessageDialog(null, "The cube is: " + num2);
break;
case 4:
string = JOptionPane.showInputDialog("Enter a sentence or a word.");
length = string.length();
JOptionPane.showMessageDialog(null, "The length : " + "\n" + length + "\n\n" +
"is:" + string);
break;
case 5:
string = JOptionPane.showInputDialog("Enter a word.");
length = string.length();
for (int i = length - 1; i >= 0; i--)
Inverse = Inverse + string.charAt(i);
JOptionPane.showInputDialog(null, "Would you like to ry again?"
JOptionPane.YES_NO_OPTION,
JOptionPane.Question_Message, null, options, options [0]);
}
}
while (response == JOptionPane.YES_OPTION);
}
}
使用 switch case 还是 if else 语句更好? 错误为静态扫描仪缺少返回类型方法,并且无法解决 YES_NO_OPTION
最佳答案
这可能是您在“Would you like to ry Again?”之后漏掉了一个逗号。下面的字符串:
JOptionPane.showInputDialog(null, "Would you like to ry again?"
JOptionPane.YES_NO_OPTION,
JOptionPane.Question_Message, null, options, options [0]);
应该是:
JOptionPane.showInputDialog(null, "Would you like to ry again?",
JOptionPane.YES_NO_OPTION,
JOptionPane.Question_Message, null, options, options [0]);
您还定义了扫描仪,如下所示:
static Scanner Scan new = Scanner (System.in);
应该是
static Scanner Scan = new Scanner (System.in);
另一件事是我没有在任何地方看到你定义的选项。因此,您可能需要在以下行中使用之前声明并初始化它:
JOptionPane
.showInputDialog(null, "Would you like to ry again?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
注意:Java 区分大小写,因此 Question_Message 与 QUESTION_MESSAGE 不同。
关于java - rroReturn 类型方法缺失,YES_NO_OPTION 无法解决某些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27267345/