2014-12-03 92 views
0

我做了一個程序來識別正方形,平方根,數字立方體和字符串的反轉我有一些錯誤我沒有一個想法來解決這個問題。rroReturn類型方法丟失,YES_NO_OPTION無法解決一些錯誤

這裏是我的下面

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); 
    } 
} 

代碼是更好地做切換的情況下,或者如果else語句? 錯誤是返回類型方法是缺少靜態掃描儀和YES_NO_OPTION不能解決

+0

確切的錯誤是什麼? – Raptor 2014-12-03 08:59:08

+0

這與您的問題完全無關,但您應該習慣[Java命名約定](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367):class名字應該是LikeThis,像this這樣的變量等等。例如,如果我看到名爲'Inverse'或'Scan'的東西,我希望它們是類,而不是變量。 – AJPerez 2014-12-03 09:15:17

回答

1

這可能是你錯過了一個逗號後,你的「你想再次嗎?」字符串在下面:

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); 

另一件事是我沒有看到你定義的選項anywh ERE。所以,你可能需要聲明和初始化它使用在以下行之前:

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。

+0

謝謝,但是當我這樣做,它仍然有錯誤,它說,選項無法解析爲變量,QUESTION無法解析或不是字段 – anomalyyaboi 2014-12-03 09:11:10

+0

我已修改我的答案。相應地修改並讓我們知道它是如何發生的。 – SMA 2014-12-03 09:16:00

相關問題