2013-03-26 60 views
0

我想通過指令方法打印菜單,但程序不執行任何操作。任何人都可以告訴我如何解決這個問題。java中的菜單方法

這裏是類方法聲明..

public class Factorial 
{ 

    public void instructions() 
    { 
     System.out.printf("Enter your choice:\n", 
     " 1 to calculate a factorial value of an integer.\n", 
     " 2 to calculate mathematical constant e.\n", 
     " 3 to calculate e^x.\n", 
     " 4 to end.\n");   
    } // End of instructions 
} 

這裏是調用從階乘的課堂教學方法爲主。

import java.util.Scanner; // Program uses scanner. 

public class behzat 
{ 

    private static Scanner input; 

    public static void main(String[] args) 
    { 

     Factorial myfactorial = new Factorial(); 
     myfactorial.instructions(); 

    }  

} 
+0

我不知道它是否是錯字,但'factorial'應該是'Factorial'。如果你的代碼按照現在的方式進行編譯,那麼它可能就是世界末日的標誌。 – Maroun 2013-03-26 22:04:59

+0

什麼問題,創建一個階乘對象,調用指令方法,只要你想打印指令菜單 – 2013-03-26 22:06:04

+1

你的代碼是否編譯?你的課是'F'actorial而不是'f'actorial。 – A4L 2013-03-26 22:06:58

回答

2

您使用的printf,爲此,第一個參數實際上是一個格式字符串,將根據該格式打印下一個參數。

因此,即使忽略factorial和factorial之間的類名稱錯誤,您的代碼應該只打印"Enter your choice:\n"

您需要使用打印代替:

System.out.print("Enter your choice:\n" + 
    " 1 to calculate a factorial value of an integer.\n" + 
    " 2 to calculate mathematical constant e.\n" + 
    " 3 to calculate e^x.\n" + 
    " 4 to end.\n"); 

需要注意的是,只有一個參數此功能,通過使用字符串連接,方便閱讀在這裏分開。

+0

+1,很好趕上:) – Maroun 2013-03-26 22:12:16

+0

是的,我沒有注意到printf要麼哈。 – RyPope 2013-03-26 22:15:26

+0

非常感謝.. :) – Behzat 2013-03-26 22:18:38

3

類定義以大寫字母開頭。嘗試:

Factorial myfactorial = new Factorial(); 
myfactorial.instructions(); 
+0

Thanks.I也進行了修改,但它僅打印「輸入您的選擇:」它不打印菜單編號。 – Behzat 2013-03-26 22:10:26

+1

您正在使用printf錯誤。在這種情況下,您應該只使用println或打印 – RyPope 2013-03-26 22:11:32

+0

@SirDarius爲您的案例發佈了正確的打印用法。注意'+'將字符串連接在一起。 – RyPope 2013-03-26 22:12:23