Java中方法前崩溃
我在大学的活动中工作,我试图做一个带有选项的菜单,但当我把选项“a”放在介绍用户和通过验证用户并登录系统时,程序没有移动,也没有询问用户。就在system.out.Blaba选项A或B之前。 我只是想知道键盘输入是否与用户和密码数组中的某个元素相关Java中方法前崩溃,java,loops,crash,Java,Loops,Crash,我在大学的活动中工作,我试图做一个带有选项的菜单,但当我把选项“a”放在介绍用户和通过验证用户并登录系统时,程序没有移动,也没有询问用户。就在system.out.Blaba选项A或B之前。 我只是想知道键盘输入是否与用户和密码数组中的某个元素相关 package com.app; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Main { /
package com.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
//cmd_in
static Scanner entradaConsola = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
int cantMax = 10;
String[] nombresVendedores = new String[cantMax];
String[] passVendedores = new String[cantMax];
String[] codPeliculas = new String[cantMax];
String[] nombrePeliculas = new String[cantMax];
String[] salaPeliculas = new String[cantMax];
int[] dispAsientos = new int[cantMax];
int[] precioEntrada = new int[cantMax];
String opcion = "1";
int cantVendedores = leerArchivos("vendedores.txt", opcion,nombresVendedores,passVendedores,codPeliculas,nombrePeliculas,
salaPeliculas,dispAsientos,precioEntrada);
opcion = "2";
int cantPeliculas = leerArchivos("peliculas.txt", opcion,nombresVendedores,passVendedores,codPeliculas,nombrePeliculas,
salaPeliculas,dispAsientos,precioEntrada);
while (true){
System.out.println("=== WELCOME TO CINE HOYTS ===");
System.out.println("Choose one option: ");
System.out.println("A) Login");
System.out.println("B) Exit");
String eleccion = entradaConsola.nextLine();
if (eleccion.equalsIgnoreCase("a")){
String user = entradaConsola.nextLine();
String pass = entradaConsola.nextLine();
if (verificarUsuario(nombresVendedores,passVendedores,cantVendedores,user,pass)){
System.out.println("Sucesfull Login");
System.out.println(cantPeliculas);
}else
System.out.println("User/Password incorrect.");
}else if (eleccion.equalsIgnoreCase("b")){
System.out.println("Good Bye. EXITING!");
break;
}else
System.out.println("Wrong options");
}
}
// read file
public static int leerArchivos(String nombreArchivo, String opcion, String[] nombresVendedores, String[] passVendedores,
String[] codPeliculas, String[] nombrePeliculas, String[] salaPeliculas, int[] dispAsientos,
int[] precioEntradas) throws FileNotFoundException {
File archivo = new File(nombreArchivo);
Scanner lector = new Scanner(archivo);
int cant = 0;
switch (opcion){
case "1" -> {
while (lector.hasNextLine()){
String linea = lector.nextLine();
String[] partes = linea.split(",");
nombresVendedores[cant] = partes[0];
passVendedores[cant] = partes[1];
cant++;
}
}
case "2" -> {
while (lector.hasNextLine()){
String linea = lector.nextLine();
String[] partes = linea.split(",");
codPeliculas[cant] = partes[0];
nombrePeliculas[cant] = partes[1];
salaPeliculas[cant] = partes[2];
dispAsientos[cant] = Integer.parseInt(partes[3]);
precioEntradas[cant] = Integer.parseInt(partes[4]);
cant++;
}
}
}
return cant;
}
// Verify if user exists
public static boolean verificarUsuario(String[] nombresVendedores, String[] passVendedores, int cantVendedores,String user,String pass){
for (int i = 0; i < cantVendedores; i++){
if (nombresVendedores[i].equalsIgnoreCase(user)){
if (passVendedores[i].equalsIgnoreCase(pass)){
return true;
}
}
}
return false;
}
package.com.app;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
公共班机{
//cmd_in
静态扫描仪entradaConsola=新扫描仪(System.in);
公共静态void main(字符串[]args)引发FileNotFoundException{
int cantMax=10;
字符串[]nombresVendedores=新字符串[cantMax];
字符串[]passVendedores=新字符串[cantMax];
字符串[]codPeliculas=新字符串[cantMax];
字符串[]nombrepellulas=新字符串[cantMax];
字符串[]Salapelliculas=新字符串[cantMax];
int[]dispAsientos=新int[cantMax];
int[]precioEntrada=新int[cantMax];
字符串opcion=“1”;
int cantVendedores=leerArchivos(“vendedores.txt”),opcion,nombresVendedores,passVendedores,codPeliculas,nombrespeliculas,
Salapelliculas、dispAsientos、precioEntrada);
opcion=“2”;
int cantpellulas=leerArchivos(“peliculas.txt”),opcion,nombresVendedores,passVendedores,codPeliculas,nombrespeliculas,
Salapelliculas、dispAsientos、precioEntrada);
while(true){
System.out.println(“==欢迎来到电影HOYTS==”;
System.out.println(“选择一个选项:”);
System.out.println(“A)登录);
系统输出打印项次(“B)退出”);
String eleccion=entradaConsola.nextLine();
if(电气等信号情况(“a”)){
字符串user=entradaConsola.nextLine();
String pass=entradaConsola.nextLine();
if(验证使用(名称、密码、cantVendedores、用户、密码)){
System.out.println(“成功登录”);
系统输出打印LN(小腕);
}否则
System.out.println(“用户/密码不正确”);
}else if(电气同等信号情况(“b”)){
System.out.println(“再见,退出!”);
打破
}否则
System.out.println(“错误选项”);
}
}
//读取文件
公共静态int-leerArchivos(字符串nombreArchivo、字符串opcion、字符串[]nombresVendedores、字符串[]passVendedores、,
字符串[]codPeliculas,字符串[]nombreliculas,字符串[]salaPeliculas,int[]dispAsientos,
int[]precioEntradas)引发FileNotFoundException{
文件archivo=新文件(nombreArchivo);
扫描仪选择器=新扫描仪(archivo);
int-cant=0;
交换机(opcion){
案例“1”->{
while(lector.hasNextLine()){
字符串linea=lector.nextLine();
String[]partes=linea.split(“,”);
nombresVendedores[cant]=双方[0];
passVendedores[cant]=partes[1];
cant++;
}
}
案例“2”->{
while(lector.hasNextLine()){
字符串linea=lector.nextLine();
String[]partes=linea.split(“,”);
Codpelliculas[cant]=分片[0];
nombrePeliculas[cant]=双方[1];
Salapelliculas[cant]=两个部分[2];
dispAsientos[cant]=Integer.parseInt(partes[3]);
precioEntradas[cant]=Integer.parseInt(partes[4]);
cant++;
}
}
}
回程超高;
}
//验证用户是否存在
公共静态布尔校验(字符串[]nombresVendedores,字符串[]passVendedores,int canvendedores,字符串用户,字符串pass){
for(int i=0;i
}我想你的程序已经开始运行了 只需打印一个文本,然后接受用户并通过。那就更清楚了
System.out.println("Enter username:");
String user = entradaConsola.nextLine();
System.out.println("Enter password:");
String pass = entradaConsola.nextLine();
你说的“撞车”是什么意思?它是否输出堆栈跟踪?如果是,请将其添加到问题中。