Java-菜单循环和数学计算
我正在开发一个程序,它会给用户一个菜单,允许他们选择一个选项,然后要求不同的数字根据他们的菜单选择进行计算。我已经从程序的基本大纲开始,但是我需要帮助实现循环并使其工作 以下是我目前的代码:Java-菜单循环和数学计算,java,Java,我正在开发一个程序,它会给用户一个菜单,允许他们选择一个选项,然后要求不同的数字根据他们的菜单选择进行计算。我已经从程序的基本大纲开始,但是我需要帮助实现循环并使其工作 以下是我目前的代码: import java.util.Scanner; public class LB04Alexander { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.o
import java.util.Scanner;
public class LB04Alexander {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This program will perform several types of calculations.");
System.out.println();
System.out.println("-MENU-");
System.out.println("Select from the following menu options:");
System.out.println("1 - Compute integer powers of a number");
System.out.println("2 - Compute a factorial");
System.out.println("3 - Compute a square root");
System.out.println("4 - Compute a cube root");
System.out.println("5 - Compute an exponential");
System.out.println("6 - Compute the sine of an angle");
System.out.println("7 - Compute the cosine of an angle");
System.out.println("8 - Convert a positive integer to binary");
System.out.println("9 - Exit");
System.out.println("Please enter the number corresponding to your choice:");
int menuChoice = input.nextInt();
while (menuChoice >= 1 && menuChoice <= 9); {
switch (menuChoice) {
case 1:
System.out.println("You have chosen to compute integer powers of a number.");
System.out.println("Please enter any number for the base:");
double base1 = input.nextDouble();
System.out.println("Now, enter a non-negative integer for the exponent:");
int exponent1 = input.nextInt();
if (exponent1 < 0)
System.out.println("Error: exponent is negitive.");
else
System.out.println(base1 + "^" + exponent1 + " = " + "");
break;
case 2:
System.out.println("You have chosen to compute a factorial (n!) of a non-negative integer.");
System.out.println("Please enter a non-negitive integer for n:");
long fact = input.nextLong();
int result = 1;
if (fact < 0)
System.out.println("Error: integer is negitive.");
else if (fact >= 127)
System.out.println("Error: Answer too large to calculate");
else
for (int i = 1; i <= fact; i++) {
result = result * i;
}
System.out.println(fact + "! = " + result );
break;
case 3:
System.out.println("You have chosen to compute a square root.");
break;
case 4:
System.out.println("You have chosen to compute a cube root.");
break;
case 5:
System.out.println("You have chosen to compute an exponential.");
break;
case 6:
System.out.println("You have chosen to compute the sine of an angle.");
break;
case 7:
System.out.println("You have chosen to compute the cosine of an angle.");
break;
case 8:
System.out.println("You have chosen to convert a positive integer to binary");
break;
case 9:
System.out.println("You have chosen to exit");
System.out.println("Program LB04Alexander is now terminating...");
input.close();
System.out.println("The program has ended. Goodbye.");
break;
default:
System.out.println("Invalid menu choice, please try again.");
break;
} }
}
}
import java.util.Scanner;
公共类LB041{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“此程序将执行多种类型的计算。”);
System.out.println();
System.out.println(“-MENU-”);
System.out.println(“从以下菜单选项中选择:”);
System.out.println(“1-计算数字的整数幂”);
System.out.println(“2-计算阶乘”);
System.out.println(“3-计算平方根”);
System.out.println(“4-计算立方根”);
System.out.println(“5-计算指数”);
System.out.println(“6-计算角度的正弦”);
System.out.println(“7-计算角度的余弦”);
System.out.println(“8-将正整数转换为二进制”);
System.out.println(“9-退出”);
System.out.println(“请输入您选择的号码:”);
int menuChoice=input.nextInt();
而(menuChoice>=1&&menuChoice=127)
System.out.println(“错误:答案太大,无法计算”);
其他的
对于(int i=1;i而(menuChoice>=1&&menuChoice而(menuChoice>=1&&menuChoice而(menuChoice>=1&&menuChoice而(menuChoice>=1&&menuChoice试试看,它比您的模块化程度要高一些,但大致相同
import java.util.Scanner;
public class LB04Alexander {
public static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int choice;
showMenu();
while((choice = getInt("Please enter the number corresponding to your choice:")) != 9) {
doCalculation(choice);
showMenu();
}
}
public static void showMenu() {
System.out.println("This program will preform several types on calculations.");
System.out.println();
System.out.println("===============MENU=====================");
System.out.println("Select from the following menu options:");
System.out.println("1 - Compute integer powers of a number");
System.out.println("2 - Compute a factorial");
System.out.println("3 - Compute a square root");
System.out.println("4 - Compute a cube root");
System.out.println("5 - Compute an exponential");
System.out.println("6 - Compute the sine of an angle");
System.out.println("7 - Compute the cosine of an angle");
System.out.println("8 - Convert a positive integer to binary");
System.out.println("9 - Exit");
System.out.println("========================================");
}
private static void doCalculation(int choice) {
switch (choice) {
case 1:
System.out.println("\nYou have chosen to compute integer powers of a number.");
double base1 = getDouble("Please enter the number corresponding to your choice:");
int exponent1 = getInt("Now, enter a non-negative integer for the exponent:");
System.out.println(base1 + "^" + exponent1 + " = " + "");
break;
case 2:
System.out.println("You have chosen to compute a factorial (n!) of a non-negative integer.");
long fact = getLong("Please enter a non-negitive integer for n:");
int result = 1;
if (fact < 0)
System.out.println("Error: integer is negitive.");
else if (fact >= 127)
System.out.println("Error: Answer too large to calculate");
else
for (int i = 1; i <= fact; i++) {
result = result * i;
}
System.out.println(fact + "! = " + result );
break;
case 3:
System.out.println("You have chosen to compute a square root.");
break;
case 4:
System.out.println("You have chosen to compute a cube root.");
break;
case 5:
System.out.println("You have chosen to compute an exponential.");
break;
case 6:
System.out.println("You have chosen to compute the sine of an angle.");
break;
case 7:
System.out.println("You have chosen to compute the cosine of an angle.");
break;
case 8:
System.out.println("You have chosen to convert a positive integer to binary");
break;
default:
System.out.println("Invalid menu choice, please try again.");
break;
}
}
private static int getInt(String prompt) {
boolean unassigned = true;
int value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextInt();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
private static double getDouble(String prompt) {
boolean unassigned = true;
double value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextDouble();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
private static long getLong(String prompt) {
boolean unassigned = true;
long value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextLong();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
}
import java.util.Scanner;
公共类LB041{
公共静态最终扫描仪=新扫描仪(System.in);
公共静态void main(字符串[]args){
智力选择;
showMenu();
while((choice=getInt(“请输入与您的选择相对应的数字:”)!=9){
计算(选择);
showMenu();
}
}
公共静态无效显示菜单(){
System.out.println(“此程序将在计算中执行多种类型。”);
System.out.println();
System.out.println(“=================================================================================================”);
System.out.println(“从以下菜单选项中选择:”);
System.out.println(“1-计算数字的整数幂”);
System.out.println(“2-计算阶乘”);
System.out.println(“3-计算平方根”);
System.out.println(“4-计算立方根”);
System.out.println(“5-计算指数”);
System.out.println(“6-计算角度的正弦”);
System.out.println(“7-计算角度的余弦”);
System.out.println(“8-将正整数转换为二进制”);
System.out.println(“9-退出”);
System.out.println(“=============================================================”);
}
专用静态无效数据计算(整数选择){
开关(选择){
案例1:
System.out.println(“\n您已选择计算数字的整数幂。”);
double base1=getDouble(“请输入您选择的号码:”);
int-exponent1=getInt(“现在,为指数输入一个非负整数:”);
System.out.println(base1+“^”+指数1+”=“+”);
打破
案例2:
println(“您已经选择计算非负整数的阶乘(n!)”;
long fact=getLong(“请为n:”输入一个非负整数);
int结果=1;
如果(事实<0)
System.out.println(“错误:整数是负的。”);
否则如果(事实>=127)
System.out.println(“错误:答案太大,无法计算”);
其他的
对于(inti=1;i试试这个,它比你的模块化程度高一些,但大致相同
import java.util.Scanner;
public class LB04Alexander {
public static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int choice;
showMenu();
while((choice = getInt("Please enter the number corresponding to your choice:")) != 9) {
doCalculation(choice);
showMenu();
}
}
public static void showMenu() {
System.out.println("This program will preform several types on calculations.");
System.out.println();
System.out.println("===============MENU=====================");
System.out.println("Select from the following menu options:");
System.out.println("1 - Compute integer powers of a number");
System.out.println("2 - Compute a factorial");
System.out.println("3 - Compute a square root");
System.out.println("4 - Compute a cube root");
System.out.println("5 - Compute an exponential");
System.out.println("6 - Compute the sine of an angle");
System.out.println("7 - Compute the cosine of an angle");
System.out.println("8 - Convert a positive integer to binary");
System.out.println("9 - Exit");
System.out.println("========================================");
}
private static void doCalculation(int choice) {
switch (choice) {
case 1:
System.out.println("\nYou have chosen to compute integer powers of a number.");
double base1 = getDouble("Please enter the number corresponding to your choice:");
int exponent1 = getInt("Now, enter a non-negative integer for the exponent:");
System.out.println(base1 + "^" + exponent1 + " = " + "");
break;
case 2:
System.out.println("You have chosen to compute a factorial (n!) of a non-negative integer.");
long fact = getLong("Please enter a non-negitive integer for n:");
int result = 1;
if (fact < 0)
System.out.println("Error: integer is negitive.");
else if (fact >= 127)
System.out.println("Error: Answer too large to calculate");
else
for (int i = 1; i <= fact; i++) {
result = result * i;
}
System.out.println(fact + "! = " + result );
break;
case 3:
System.out.println("You have chosen to compute a square root.");
break;
case 4:
System.out.println("You have chosen to compute a cube root.");
break;
case 5:
System.out.println("You have chosen to compute an exponential.");
break;
case 6:
System.out.println("You have chosen to compute the sine of an angle.");
break;
case 7:
System.out.println("You have chosen to compute the cosine of an angle.");
break;
case 8:
System.out.println("You have chosen to convert a positive integer to binary");
break;
default:
System.out.println("Invalid menu choice, please try again.");
break;
}
}
private static int getInt(String prompt) {
boolean unassigned = true;
int value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextInt();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
private static double getDouble(String prompt) {
boolean unassigned = true;
double value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextDouble();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
private static long getLong(String prompt) {
boolean unassigned = true;
long value = 0;
while(unassigned) {
try {
System.out.println(prompt);
value = SCANNER.nextLong();
unassigned = false;
}
catch (IllegalArgumentException e) {}
}
System.out.println("[" + value + "]");
return value;
}
}
import java.util.Scanner;
公共类LB041{
公共静态最终扫描仪=新扫描仪(System.in);
公共静态void main(字符串[]args){
智力选择;
showMenu();
while((choice=getInt(“请输入与您的选择相对应的数字:”)!=9){
计算(选择);
showMenu();
}
}
公共静态无效显示菜单(){
System.out.println(“此程序将在计算中执行多种类型。”);
System.out.println();
System.out.println(“=================================================================================================”);
System.out.println(“从以下菜单选项中选择:”);
System.out.println(“1-计算数字的整数幂”);
System.out.println(“2-计算阶乘”);
System.out.println(“3-计算平方根”);
System.out.println(“4-计算立方根”);
System.out.println(“5-计算指数”);
System.out.println(“6-计算角度的正弦”);
System.out.println(“7-计算角度的余弦”);
System.out.println(“8-将正整数转换为二进制”);
System.out.println(“9-退出”);
System.out.println(“=============================================================”);
}
专用静态无效数据计算(整数选择){
开关(选择){
案例1:
System.out.println(“\n您已选择计算数字的整数幂。”);
double base1=getDouble(“请输入您选择的号码:”);
int-exponent1=getInt(“现在,为指数输入一个非负整数:”);
System.out.println(base1+“^”+指数1+”=“+”);
打破
案例2:
println(“您已经选择计算非负整数的阶乘(n!)”;
long fact=getLong(“请在