public void saveTheGameToFile(int index){ if(index<1||index>3){ System.out.println("Invalid save index:"+index); return; } String filename="save"+index+".dat";//存档的文件名 try(ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream(filename))){ output.writeObject(board);//棋盘状态 output.writeInt();//玩家分数 output.writeInt(saveCount);//存档次数 output.writeChars();//玩家名字 output.writeInt();//玩家顺序 System.out.println("Successfully saved to file "+filename); }catch (IOException e){ System.out.println("Failed to save to file "+ filename); System.out.println("Sorry! Something went wrong: "+ e.getMessage()); } }解释以下这段代码的逻辑
时间: 2023-06-05 20:06:40 浏览: 122
这段代码定义了一个名为 "saveTheGameToFile" 的公共方法,该方法接收一个整数参数 index,用于指定要保存的游戏存档的索引。方法首先检查 index 是否有效,即在 1 到 3 之间,如果无效,则打印错误消息并返回。如果 index 有效,则使用该索引构造文件名,文件名的格式为 "save" + index + ".dat"。随后,使用 ObjectOutputStream 将棋盘状态、玩家分数、存档次数、玩家名称和玩家顺序写入到该文件中。如果保存成功,则输出成功消息,否则输出错误消息和异常信息。
相关问题
Write java code: Copy the files, small_weapons.txt, and large_weapons.txt from the assignment folder on Blackboard and save them to your folder. For testing purposes, you should use the small file. Use the large file when you think the application works correctly. To see what is in the files use a text editor. Nilesh is currently enjoying the action RPG game Torchlight 2 which is an awesome game and totally blows Auction House Simulator 3, oh sorry, that should be Diablo 3, out of the water. He has got a file containing info on some of the unique weapons in the game. The transaction file contains the following information: Weapon Name (string) Weapon Type (string) Damage (int) Weapon Speed (double) … To tell if one weapon is better than another you need to know the Damage Per Second (DPS) the weapon does, since weapons have a different attack speed. DPS is calculated by taking the damage value and dividing it by the attack speed.a) You will write a program that will allow Nilesh to load the file and display the weapon info and DPS of every weapon in the input file. When the user chooses to open a weapon file, they are required to type in the filename. It will then read the 4 values about a particular weapon and then display the 4 values to the console window on one line neatly padded along with the DPS of the weapon. This is repeated until the end of the file. b) Modify your code from a) so that the weapon information written to the console window is also written to a text file. The user should be able to specify the name of the file. Add a header to the beginning of the file which has column headers for each column. At the end of the text file display the total number of weapons in the file. c) Document your code appropriately and add your name and id number as comments at the top of your code. Please also submit this text file you have created. Enter the file you want to search end with .txt: large_weapons.txt 1 Blackfang Bludgeon Great Hammer 489 1.44 339.58333333333337 2 Bones 2 Boneshredder Great Axe 256 0.84 304.76190476190476 3 Comet's Tail Great Sword 872 1.2 726.6666666666667 4 Decapitator Great Sword 188 1.08 174.07407407407408 5 Demolisher Great Hammer 887 1.32 671.9696969696969
```
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class WeaponInfo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the file you want to search end with .txt: ");
String fileName = scanner.nextLine();
File file = new File(fileName);
try {
Scanner fileScanner = new Scanner(file);
FileWriter writer = new FileWriter(new File("weapon_info_output.txt"));
writer.write("Weapon Name\tWeapon Type\tDamage\tWeapon Speed\tDPS\n");
int count = 0;
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] tokens = line.split(" ");
String weaponName = tokens[0];
String weaponType = tokens[1];
int damage = Integer.parseInt(tokens[2]);
double weaponSpeed = Double.parseDouble(tokens[3]);
double dps = damage / weaponSpeed;
System.out.printf("%-20s%-20s%-10d%-15.2f%.2f\n", weaponName, weaponType, damage, weaponSpeed, dps);
writer.write(weaponName + "\t" + weaponType + "\t" + damage + "\t" + weaponSpeed + "\t" + dps + "\n");
count++;
}
System.out.println("Total number of weapons: " + count);
writer.write("Total number of weapons: " + count);
writer.close();
fileScanner.close();
} catch (IOException e) {
System.out.println("Unable to read file: " + fileName);
} catch (NumberFormatException e) {
System.out.println("Invalid file format: " + fileName);
}
scanner.close();
}
}
```
用双向链表作数据结构,用java语言编写一个通讯录管理系统,本系统应完成一下几方面的功能: 1)输入信息——enter(); 2)显示信息———display( ); 3)查找以姓名作为关键字 ———search( ); 4)删除信息———delete( ); 5)存盘———save ( ); 6)装入———load( ) ; 设计要求: 1)每条信息至包含 :姓名(NAME )街道(STREET)城市(CITY)邮编(EIP)国家(STATE)几项 2)作为一个完整的系统,应具有友好的界面和较强的容错能力,上机能正常运行 3)具有可视化界面
好的,我已经记录下来了你的问题。对于你的问题,我可以给出以下回答:
实现双向链表可以使用Java的LinkedList类,该类实现了List接口,同时也是一个双向链表。通讯录管理系统的实现可以借助该类来完成。
下面是一个简单的通讯录管理系统的代码示例:
```
import java.io.*;
import java.util.*;
class Contact {
String name;
String street;
String city;
int zip;
String state;
}
class ContactList {
LinkedList<Contact> contacts = new LinkedList<Contact>();
public void enter() {
Contact contact = new Contact();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter name: ");
contact.name = scanner.nextLine();
System.out.print("Enter street: ");
contact.street = scanner.nextLine();
System.out.print("Enter city: ");
contact.city = scanner.nextLine();
System.out.print("Enter zip: ");
contact.zip = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter state: ");
contact.state = scanner.nextLine();
contacts.add(contact);
}
public void display() {
for (Contact contact : contacts) {
System.out.println("Name: " + contact.name);
System.out.println("Street: " + contact.street);
System.out.println("City: " + contact.city);
System.out.println("Zip: " + contact.zip);
System.out.println("State: " + contact.state);
}
}
public void search(String name) {
boolean found = false;
for (Contact contact : contacts) {
if (contact.name.equals(name)) {
System.out.println("Name: " + contact.name);
System.out.println("Street: " + contact.street);
System.out.println("City: " + contact.city);
System.out.println("Zip: " + contact.zip);
System.out.println("State: " + contact.state);
found = true;
break;
}
}
if (!found) {
System.out.println("Contact not found.");
}
}
public void delete(String name) {
Iterator<Contact> iterator = contacts.iterator();
while (iterator.hasNext()) {
Contact contact = iterator.next();
if (contact.name.equals(name)) {
iterator.remove();
System.out.println("Contact deleted.");
return;
}
}
System.out.println("Contact not found.");
}
public void save() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("contacts.dat"));
outputStream.writeObject(contacts);
outputStream.close();
System.out.println("Contacts saved.");
} catch (IOException e) {
System.out.println("Error saving contacts.");
}
}
public void load() {
try {
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("contacts.dat"));
contacts = (LinkedList<Contact>)inputStream.readObject();
inputStream.close();
System.out.println("Contacts loaded.");
} catch (IOException e) {
System.out.println("Error loading contacts.");
} catch (ClassNotFoundException e) {
System.out.println("Error loading contacts.");
}
}
}
public class ContactManager {
public static void main(String[] args) {
ContactList contactList = new ContactList();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Enter contact");
System.out.println("2. Display contacts");
System.out.println("3. Search contacts");
System.out.println("4. Delete contact");
System.out.println("5. Save contacts");
System.out.println("6. Load contacts");
System.out.println("7. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
contactList.enter();
break;
case 2:
contactList.display();
break;
case 3:
System.out.print("Enter name to search: ");
String name = scanner.nextLine();
contactList.search(name);
break;
case 4:
System.out.print("Enter name to delete: ");
name = scanner.nextLine();
contactList.delete(name);
break;
case 5:
contactList.save();
break;
case 6:
contactList.load();
break;
case 7:
System.exit(0);
break;
default:
System.out.println("Invalid choice.");
break;
}
}
}
}
```
该代码示例使用了一个Contact类来表示每个联系人,该类中包含了姓名、街道、城市、邮编和国家等信息。ContactList类则是一个管理联系人的类,它使用LinkedList来存储联系人列表,并提供了enter()、display()、search()、delete()、save()和load()等方法来进行联系人的添加、显示、查找、删除、存储和加载等操作。最后,ContactManager类是一个简单的控制台程序,它通过一个循环来接受用户的输入,并调用ContactList类的相应方法来完成操作。
阅读全文