我需要以下代码的帮助:
case 1:
System.out.println("Actualizar Puntos");
int registro = 1;
// Nuevos puntos (entrada del usuario)
punts = 40;
// Posición donde comienza el registro
int pos = 48 * (registro - 1);
// Mover a la posición donde empieza el dato
pos += 4 + 20 + 20;
raf.seek(pos);
raf.writeInt(punts);
break;
在屏幕上打印数据时,我观察到它会将所有结果更新为 40:
ID: 50
Nombre Instituto: inst1
Nombre Equipo: equi1
Puntos: 40
ID: 600
Nombre Instituto: inst2
Nombre Equipo: equip2
Puntos: 40
ID: 6000
Nombre Instituto: inst3
Nombre Equipo: equip3
Puntos: 40
每条记录应该有不同的点数。例如:前40分,第二60分,第三30分等。
问题是当它应该逐条记录时,所有寄存器都更新为 40。不是一下子。
我究竟做错了什么?
应用户的要求,我附上完整的代码以供审查:
第 1 部分(在二进制文件中输入数据的位置)
/**
* OPCIO 1 ACTIVITAT 2
*/
package activitat2;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
* ACTIVITAT 2 EAC 6
* @author Montse
*/
public class Opcio1{
/**
* En aquesta activitat la número 2 farem:
* Introduir instituts
* Listar instituts introduïts
* Sortir
* Tornar al menú principal
* @throws FileNotFoundException
* @throws IOException
*/
static void IntroduirInstituts() throws FileNotFoundException, IOException{
RandomAccessFile raf;
raf = new RandomAccessFile("Partides.dat", "rw");
System.out.println("HAS ESCOLLIT OPCIÓ 1");
System.out.println("--------------------------------");
System.out.println("Institutos que participarán en infoguardians");
System.out.println("1-Registrar los datos de los institutos ");
System.out.println("2-Listar todos los institutos");
System.out.println("3-Volver al menú principal");
System.out.println("4- Salir");
System.out.println("Escoje una opción");
Scanner lector = new Scanner(System.in);
int punts = 0;
int enterLlegit = 0;
boolean llegit = false;
while(!llegit){
llegit = lector.hasNextInt();
if (llegit){
enterLlegit = lector.nextInt();
switch (enterLlegit){
case 1:
System.out.println("Registra los datos de los institutos:");
System.out.println("Quantos institutos participan en el concurso?");
boolean leido = false;
while(!leido){
leido = lector.hasNextInt();
if (leido){
int enterLlegit2 = lector.nextInt();
System.out.println("Numero total de centros: "+enterLlegit2);
System.out.println("El fitxer s'ha creat. Accés directe");
for (int j = 0, pos = 0; j < enterLlegit2; j++){
int i = 0;
i = enterLlegit2;
if(enterLlegit2 > 0){
System.out.println("-------------------------------------");
System.out.println("Introduce la info sobre el instituto:");
System.out.println("-------------------------------------");
System.out.println("Id?");
int idInst = lector.nextInt();
System.out.println("Nom Institut:");
String nomInst = lector.next();
System.out.println("Nom Equip:");
String nomEqui = lector.next();
System.out.println();
System.out.println("------------------------------------------");
System.out.println("::INFORMACIÓN SOBRE CENTRO INSCRITO::");
System.out.println("------------------------------------------");
System.out.println("Id: " +idInst);
System.out.println("Nom Institut: "+nomInst);
System.out.println("Equip: "+nomEqui);
System.out.println("Punts: "+punts);
System.out.println();
raf.seek(pos);
raf.writeInt(idInst);
pos +=4;
raf.seek(pos);
raf.writeUTF(nomInst);
pos +=20;
raf.seek(pos);
raf.writeUTF(nomEqui);
pos+=20;
raf.seek(pos);
raf.writeInt(punts);
pos +=4;
}
}
}else {
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
break;
case 2:
System.out.println("------------------------------------");
System.out.println("HAS ESCOGIDO LISTAR LOS INSTITUTOS");
System.out.println("------------------------------------");
for (int pos = 0;;){
try
{
raf.seek(pos);
System.out.println("ID: " +raf.readInt());
pos += 4;
raf.seek(pos);
System.out.println("Nombre Instituto: "+raf.readUTF());
pos += 20;
raf.seek(24);
System.out.println("Nombre Equipo: "+raf.readUTF());
pos += 20;
raf.seek(44);
System.out.println("Puntos: "+raf.readInt());
pos += 4;
System.out.println();
}
catch (EOFException e)
{
// Fin de archivo
break;
}
}
case 3:
System.out.println("TORNAR AL MENÚ PRINCIPAL::::");
MenuPrincipal.EscollirOpcio();
case 4:
System.out.println("Hasta la próxima!");
break;
default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
break;
}
}else {
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
lector.nextLine();
}
}
我要修改的代码的第 2 部分:
/**
* OPCIO 2 ACTIVITAT 3
*/
package activitat2;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
*
* @author Montse
*/
public class Opcio2 {
/**
* En aquesta clase farem les demandes de l'activitat 2
* @throws FileNotFoundException
*/
static void ActualitzarInstituts() throws FileNotFoundException, IOException {
RandomAccessFile raf;
raf = new RandomAccessFile("Partides.dat", "rw");
System.out.println("HAS ESCOLLIT OPCIÓ 2");
System.out.println("--------------------------------");
System.out.println("Institutos que participarán en infoguardians");
System.out.println("1-Actualizar Puntos");
System.out.println("2- Salir");
System.out.println("3- Volver al menú principal");
System.out.println("Escoje una opción");
Scanner lector = new Scanner(System.in);
int punts = 0;
int enterLlegit = 0;
boolean llegit = false;
while(!llegit) {
llegit = lector.hasNextInt();
if (llegit) {
enterLlegit = lector.nextInt();
switch (enterLlegit) {
case 1:
System.out.println("Actualizar Puntos");
// Primero enseñaremos los intitutos que tenemos registrados
System.out.println("LOS CENTROS INTRODUCIDOS SÓN:::::::");
System.out.println("-----------------------------------");
for (int pos = 0;;){
try
{
raf.seek(pos);
System.out.println("ID: " +raf.readInt());
pos += 4;
raf.seek(pos);
System.out.println("Nombre Instituto: "+raf.readUTF());
pos += 20;
raf.seek(24);
System.out.println("Nombre Equipo: "+raf.readUTF());
pos += 20;
raf.seek(44);
System.out.println("Puntos: "+raf.readInt());
pos += 4;
System.out.println();
}
catch (EOFException e)
{
// Fin de archivo
}
System.out.println("Actualizar Puntos");
int registro = 1;
// Nuevos puntos (entrada del usuario)
punts = 40;
// Posición donde comienza el registro
pos = 48 * (registro - 1);
// Mover a la posición donde empieza el dato
pos += 4 + 20 + 20;
raf.seek(pos);
raf.writeInt(punts);
break;
}
case 2:
System.out.println("Hasta la próxima!");
break;
default:
System.out.println("No has escrito una opción válida");
System.out.println("Fin del programa");
break;
}
}else {
System.out.println("No has escrito un entero, vuelve a intentarlo");
lector.next();
}
}
lector.nextLine();
}
}
.
我不知道这是否是您正在寻找的真正答案,但我会告诉您我所看到的,因为我理解您的要求:
你说
-> 在屏幕上打印数据时,我观察到它会将所有结果更新为 40:
我在任何地方都看不到您为变量 punts 分配了另一个不是 40 的值。什么与您稍后评论的内容相关联
-> 每条记录应该有不同的点数。例如:前40分,第二60分,第三30分等。
我不明白第一个 40、第二个 60、第三个 30 是从哪里得到的,也不明白为什么在 punts = 40 时必须存储这些值。
这部分也有些混乱(对我来说)
-> ...何时应该逐条记录。
我想你想要做的是从一个位置跳到另一个位置,它具有相同的内部结构来更改该位置的位/内容。如果是这样,为什么寄存器总是等于1?它不应该改变在每次迭代中前进。
更新
添加来自@Paul Vargas 的代码后,按预期工作,出现错误,在查询它们时没有显示所有记录,但如果它们与 Paul 的代码很好地存储在一起,原来错误就在这里:
不得不改变 raf.seek(pos);
由于每条记录是 48 字节长,其中文件中有几个,那么要修改每个记录中的点(当然每个都有不同的分数),可以使用循环
for
。Cualquier operación de lectura (por ejemplo,
raf.readInt()
) después del final del archivo lanza la excepciónEOFException
, debido a que no se puede leer después de final del archivo (aunque sí escribir). Puede utilizarse esto para identificar el final de archivo y romper el ciclofor
.