java中的菜单方法
我想用指令方法打印菜单,但程序什么也不执行。有人能告诉我如何解决这个问题吗 这里是类中的方法声明。java中的菜单方法,java,methods,Java,Methods,我想用指令方法打印菜单,但程序什么也不执行。有人能告诉我如何解决这个问题吗 这里是类中的方法声明。 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
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
}
这里是从阶乘类调用指令方法的main。
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();
}
}
类定义以大写字母开头。尝试:
Factorial myfactorial = new Factorial();
myfactorial.instructions();
类定义以大写字母开头。尝试:
Factorial myfactorial = new Factorial();
myfactorial.instructions();
您使用的是printf,第一个参数实际上是一个格式字符串,它将根据该格式打印下一个参数 因此,即使忽略阶乘和阶乘之间的类名错误,您的代码也应该只打印
“输入您的选择:\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");
请注意,此函数只有一个参数,此处使用字符串连接分隔,以便于阅读。您使用的是printf,第一个参数实际上是一个格式字符串,它将根据该格式打印下一个参数 因此,即使忽略阶乘和阶乘之间的类名错误,您的代码也应该只打印
“输入您的选择:\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");
请注意,此函数只有一个参数,此处使用字符串连接分隔,以便于阅读。我不知道它是否键入错误,但
factorial
应为factorial
。如果你的代码按照现在的方式编译,这可能是世界末日的标志。问题是什么,创建一个阶乘对象,调用指令方法,每当你想打印指令菜单时,你的代码会编译吗?您的类是F
actorial而不是F
actorial。它不会打印菜单。程序在myfactorial.instructions()之后继续运行@贝扎特:你确定你在写Factorial
?使用F
而不是F
?我不知道这是否是打字错误,但是factorial
应该是factorial
。如果你的代码按照现在的方式编译,这可能是世界末日的标志。问题是什么,创建一个阶乘对象,调用指令方法,每当你想打印指令菜单时,你的代码会编译吗?您的类是F
actorial而不是F
actorial。它不会打印菜单。程序在myfactorial.instructions()之后继续运行@贝扎特:你确定你在写Factorial
?使用F
而不是F
?谢谢。我也做了修改,但它只打印“输入您的选择:”它不打印菜单号。您使用printf是错误的。在这种情况下,看起来应该只使用println或print@SirDarius为您的案例发布了正确的打印用法。注意“+”将字符串连接在一起;编译器警告我它不适用于参数。对不起,您的答案在哪里。。我是此站点的新用户,抱歉..:(谢谢。我也修改了,但它只打印“输入您的选择:”它不打印菜单号。您使用的printf是错误的。在这种情况下,您应该只使用println或print@SirDarius为您的案例发布了正确的print用法。请注意“+”以将字符串连接在一起。我刚刚使用了println和print;编译器警告我它不适用于参数。对不起,您在哪里答:我是这个网站的新手,对不起:(是的,我也没有注意到印刷品哈。是的,我也没有注意到印刷品哈。