不使用方法返回java程序的主菜单
我目前是一名编程新手。我们的任务是评估用于两个矩阵的行和列是否可以执行加法操作。如果无效,我们将显示适当的消息,并通过询问用户是否重复该过程来重复该过程。如果答案为否,则控件应返回主菜单。我们当时不允许使用方法,所以我对此很有疑问不使用方法返回java程序的主菜单,java,loops,variables,Java,Loops,Variables,我目前是一名编程新手。我们的任务是评估用于两个矩阵的行和列是否可以执行加法操作。如果无效,我们将显示适当的消息,并通过询问用户是否重复该过程来重复该过程。如果答案为否,则控件应返回主菜单。我们当时不允许使用方法,所以我对此很有疑问 import java.util.Scanner; public class matrixTransposition { public static void main(String[] args) { Scanner src = new Scanner(Sy
import java.util.Scanner;
public class matrixTransposition {
public static void main(String[] args) {
Scanner src = new Scanner(System.in);
int menu_choice;
char case_choice;
char case_choice2;
boolean menu_repeat = true;
boolean case_repeat = true;
boolean check = true;
boolean inCheck = true;
int rows_A, columns_A;
int rows_A2, columns_A2;
int rows_S, columns_S;
int rows_M, columns_M;
do{
//main menu selection here
System.out.print("\nEnter your choice: ");
menu_choice = src.next().charAt(0);
switch(menu_choice){
case '1':
do{
System.out.println("-------------------------------------------------");
System.out.println("\t\tMatrix Addition");
//First Matrix/Array
System.out.print("\nFirst Matrix: Please enter number of rows: "); //asks the user to enter number of rows
rows_A = src.nextInt();
System.out.print("\nFirst Matrix: Please enter number of columns: "); //asks the user to enter number of columns
columns_A = src.nextInt();
int [][] firstMatrix_A = new int [rows_A][columns_A];
int [][] secondMatrix_A = new int [rows_A][columns_A]; //declaring the arrays to be used
int [][] sumMatrix_A = new int [rows_A][columns_A];
//asks the user to enter values
System.out.println("\nPlease enter values for first matrix: ");
for(int i=0; i<rows_A; i++)
for(int j=0; j<columns_A; j++)
firstMatrix_A[i][j]=src.nextInt();
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//Second Matrix/Array
do{
System.out.println("------------------------------------");
System.out.print("Second Matrix: Please enter number of rows: "); // asks the user to enter rows
rows_A2 = src.nextInt();
System.out.print("\nSecond Matrix: Please enter number of columns: "); //asks the user to enter columns
columns_A2 = src.nextInt();
//validating if the 2nd matrix has the same length as the first matrix
if(rows_A2 != rows_A || columns_A2 != columns_A){
System.out.println("------------------------------------");
System.out.println("Error! Input is not the same length as the First Matrix!");
System.out.print("Do you want to input again? [Y][N]: ");
case_choice2 = src.next().charAt(0);
switch(case_choice2){
case 'Y':
check = true; //if the user entered Y, the user would be able
break; //re-enter rows and columns for the 2nd matrix
case 'N':
check = false; //if the user entered N, the user would be immediately brought
menu_repeat = true; //back to the main menu selection
break;
case 'y':
check = true; //if the user entered Y, the user would be able
break; //re-enter rows and columns for the 2nd matrix
case 'n':
menu_repeat = true; //if the user entered N, the user would be immediately brought
check = false; //back to the main menu selection
break;
default: //if the user did not enter Y or N, the user would be ask to
//enter again.
System.out.println("---------------------------------------------");
System.out.println("Invalid Input Please Try Again....");
while(case_repeat){
System.out.println("---------------------------------------------");
System.out.print("Try Again? [Y][N]: ");
case_choice2 = src.next().charAt(0);
switch(case_choice2){
case 'Y':
check = true; //if the user entered Y, the user would be able
case_repeat = false; //re-enter rows and columns for the 2nd matrix
break;
case 'N':
check = false; //if the user entered N, the user would be immediately brought
case_repeat = false; //back to the main menu selection
menu_repeat = true;
break;
case 'y':
check = true; //if the user entered y, the user would be able
case_repeat = false; //re-enter rows and columns for the 2nd matrix
break;
case 'n':
check = false; //if the user entered n, the user would be immediately brought
case_repeat = false; //back to the main menu selection
menu_repeat = true;
break;
default: //if the user did not enter Y or N, the user would be ask to
//enter again.
System.out.println("---------------------------------------------");
System.out.println("Invalid Input Please Try Again....");
case_repeat = true;
}
}
}
}
//else if all inputs are valid the program would proceed to the next step
else{
//asks the user to enter values
System.out.println("\nPlease enter values for second matrix: ");
for(int i=0; i<rows_A2; i++)
for(int j=0; j<columns_A2; j++)
secondMatrix_A[i][j]=src.nextInt();
check = false;
}
}while(check);
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
for(int i=0; i<rows_A; i++)
for(int j=0; j<columns_A; j++)
sumMatrix_A[i][j] = firstMatrix_A[i][j] + secondMatrix_A[i][j]; //adds the two matrices
System.out.println("------------------------------------"); //displays the first matrix
System.out.println("First Matrix");
for(int i=0; i<rows_A; i++){
for(int j=0; j<columns_A; j++){
System.out.print(firstMatrix_A[i][j] + " ");
}
System.out.println();
}
System.out.println("------------------------------------"); //displays the second matrix
System.out.println("Second Matrix");
for(int x=0; x<rows_A2; x++){
for(int y=0; y<columns_A2; y++){
System.out.print(secondMatrix_A[x][y] + " ");
}
System.out.println();
}
System.out.println("------------------------------------"); //displays the sum of the two matrices
System.out.println("Matrix Sum");
for(int i=0; i<rows_A; i++){
for(int j=0; j<columns_A; j++){
System.out.print(sumMatrix_A[i][j] + " ");
}
System.out.println();
}
System.out.print("Do you want to try again? [Y][N]: "); //asks the user to re-run the operation
case_choice = src.next().charAt(0);
}while(case_choice == 'Y' || case_choice == 'y'); //if yes the operation would re-run
if(case_choice == 'N' || case_choice == 'n'){ //if no, the user would be brought back to the
menu_repeat = true; //main menu selection
}
break;
}
}while(menu_repeat);
}
}
import java.util.Scanner;
公共类矩阵传输{
公共静态void main(字符串[]args){
扫描仪src=新扫描仪(System.in);
int菜单选项;
char case_选择;
char case_choice2;
布尔菜单_repeat=true;
布尔case_repeat=true;
布尔检查=真;
布尔inCheck=true;
int行_A,列_A;
int行_A2,列_A2;
int行、列;
int行_M,列_M;
做{
//主菜单选择在这里
System.out.print(“\n输入您的选择:”);
menu_choice=src.next().charAt(0);
开关(菜单选择){
案例“1”:
做{
System.out.println(“-------------------------------------------------------------”;
System.out.println(“\t\t矩阵添加”);
//第一矩阵/阵列
System.out.print(“\n第一个矩阵:请输入行数:”);//要求用户输入行数
行A=src.nextInt();
System.out.print(“\n第一个矩阵:请输入列数:”);//要求用户输入列数
columns_A=src.nextInt();
int[]firstMatrix\u A=新int[行\u A][列\u A];
int[]secondMatrix_A=new int[rows_A][columns_A];//声明要使用的数组
int[]sumMatrix_A=新int[行_A][列_A];
//要求用户输入值
System.out.println(“\n请为第一个矩阵输入值:”);
对于(int i=0;i而言,问题在于当用户输入N时,您正在跳出内部循环。您想要的是跳出显示菜单选项的循环,即最外层的循环
您的代码结构如下所示:
do{
//main menu selection here
switch (menu_choice) {
case '1':
do {
//First Matrix/Array
//Second Matrix/Array
do {
//compare matrix and prompt for input if rows and col dont match
case_choice2 = src.next().charAt(0);
switch (case_choice2) {
case 'N':
check = false;
menu_repeat = true;
break;
.
.
.
}
}
}
}
}
您的break语句从内部循环中断。而您希望中断到最外层的循环
为此,应使用标签:
do{
//main menu selection here
//define label here
main_menu:
switch (menu_choice) {
.
.
.
switch (case_choice2) {
case 'N':
check = false;
menu_repeat = true;
//label used here
break main_menu;
.
.
.
这将跳转到您选择的循环。如果要将控件传递回循环,也可以使用“继续”
请注意:你可能想重新考虑CaseYORY变量的用法,因为它将不再总是在循环外访问,因为你正在破解它。编译器肯定会给你同样的错误。
你在哪里被卡住了,你有什么错误吗?我得到一个错误。当用户输入字符N时,用户必须返回主菜单。但在我的情况下,它仍然会继续执行代码,并给我一个错误Hi@Ivy,你能用这些信息编辑帖子以使你的帖子更清晰吗?@Sergio,我在这里添加了一些注释来解释每个函数。我得到的错误在swit内部在验证第二个矩阵长度时发现ch语句。如果用户输入N或N,则不会返回主菜单选择。相反,它会继续代码并返回错误