如何更改 java 中文件中的整数?

How to change ints in file in java?

我正在 java 制作一个简单的游戏作为我的第一个项目,但我 运行 遇到了问题。

在classUndead_Menu中,我一直在努力做到每次用户在while(menuRun)循环中选择第一个动作时,食物和水都会消失将下降一个并覆盖文本文件中的内容 Undead_save.txt .

这是 class Undead_Menu 的代码:

    import java.util.Random;
    import java.util.Scanner;
    import java.lang.Object;
    import java.io.File;
    import javax.swing.JComponent;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;


public class Undead_Menu
{
public static void main () throws java.io.FileNotFoundException
{

  //System code
  Scanner in = new Scanner(System.in);
  Scanner sc = new Scanner(new File("Undead_save.txt"));
  Random rand = new Random();
  Undead_Menu a=new Undead_Menu();
  Undead_Combat b=new Undead_Combat();
  boolean menuRun = true;
  int food =sc.nextInt();
  int water =sc.nextInt();

  while(menuRun)
  {
  if(food > 0 && water > 0)
  {
  System.out.println("---------------------------------------------");
  System.out.println("# You see many Undead ahead...Limited supply...#");
  System.out.println("\t# You have " + food + " food.#");
  System.out.println("\t# You have " + water + " water.#");
  System.out.println("\n\tAction?");
  System.out.println("\n\t1.Fight one");
  System.out.println("\n\t2.Hide");

   String input = in.nextLine();
   if (input.equals("1"))
   {
    PrintWriter pwInput = new PrintWriter("Undead_save.txt");
    pwInput.println(food--);
    pwInput.println(water--); 
    b.combat();
   }

   if (input.equals("2"))
   {
     System.out.println("You hide,hoping they all pass by...");
     System.exit(0);
   }
}
   else
   {
System.out.println("You ran out of supplies...you will soon die...");
System.exit(0);
   }
  }
 }
}

Undead_save.txt 包含

    3
    4

我希望文件包含

    2
    3

在下次调用 main 方法之前,它将导入 ints food 和 water 作为 2 和 3。

这里是 class Undead_Combat 的代码:

        import java.util.Random;
import java.util.Scanner;

public class Undead_Combat
{ public void combat () throws java.io.FileNotFoundException
{
    //System objects
 Scanner in = new Scanner(System.in);
 Random rand = new Random();
 Undead_Menu a=new Undead_Menu();

 //Game variables
 String[] enemies = { "Skeleton","Zombie","Vampire", "Ghost"};
 int maxEnemyHealth = 100;
 int enemyAttackDamage = 25;

 //Player Variables
 int health = 100;
 int attackDamage = 50;
 int numHealthPotions = 3;
 int healthPotionHealAmount = 50;
 int healthPotionDropChance = 50; //Percentage

 boolean running = true;

 System.out.println("The dead have risen...");

 GAME:
 while(running)
 {
  System.out.println("---------------------------------------------");

  int enemyHealth = rand.nextInt(maxEnemyHealth);
  String enemy = enemies[rand.nextInt(enemies.length)];
  System.out.println("\t# " + enemy + " has appeared... #\n");

  while(enemyHealth > 0)
  {
  System.out.println("\tYour hp: " + health);
  System.out.println("\t" + enemy + "'s hp: " + enemyHealth);
  System.out.println("\tYou have " + numHealthPotions + " potions");
  System.out.println("\n\tWhat is your command?");
  System.out.println("\t1. Attack");
  System.out.println("\t2. Use Potion");
  System.out.println("\t3. Run");

  String input = in.nextLine();
   if (input.equals("1"))
   {
   int damageDealt = rand.nextInt(attackDamage);
   int damageTaken = rand.nextInt(enemyAttackDamage);

   enemyHealth -= damageDealt;
   health -= damageTaken;

   System.out.println("\t You strike the " + enemy + " for " + damageDealt + " damage!");
   System.out.println("\t The " + enemy + " hit you for " + damageTaken + " damage...");

   if (health < 1)
   {
   System.out.println("\tYou succumb to your injuries...");
   break;
   }

   }
   else if(input.equals("2"))
   {
    if (numHealthPotions > 0)
    {
    health += healthPotionHealAmount;
    numHealthPotions--;
    System.out.println("\tThe potion restored " + healthPotionHealAmount + " hp, " + "\n\tYou now have " + numHealthPotions + " left.");
    }
    else
    {
    System.out.println("\tYou have no potions to use left...");
    }
    }


   else if(input.equals("3"))
   {
    System.out.println("\tYou have ran...");
    a.main();
   }
   else
   {
   System.out.println("\tInvalid command...");
   }

 }

  if(health < 1)
  {
  System.out.println("\tYour corpse joins the assault...");
  break;
  }
  System.out.println("---------------------------------------------");
  System.out.println("You killed one " + enemy );
  if(rand.nextInt(100) < healthPotionDropChance)
  {
  numHealthPotions++;
   System.out.println("\tThe " + enemy + " droped a potion!");
  }

  System.out.println("---------------------------------------------");
  System.out.println("Whats is your next course of action?");
  System.out.println("1. Face one more.");
  System.out.println("2. hide.");

  String input = in.nextLine();

  while(!input.equals("1") && !input.equals("2"))
  {
   System.out.println("Invalid Command...");
   input = in.nextLine();
  }

 if (input.equals("1"))
 {
 System.out.println("You look for next target...");
 continue GAME;
 }

 if (input.equals("2"))
 {
 System.out.println("You decide to hide until less of them are present...");
 a.main();
 }
 System.out.println("Will you fight again?");
 }
}
}

欢迎来到Stack Overflow Noctifer

在使用PrintWriterclass时,您需要确保flushclose 它释放所有资源,因为它不会自动刷新。现在在你的代码中,因为它没有关闭,所以没有保存更改。该文件只是被截断了。

如果你不想刷新和关闭它,你可以使用另一种方法:

PrintWriter pw = new PrintWriter(fileWriter,true); 

传递给构造函数的true会在每次调用println方法后将输出刷新到文件。

此外,您使用的减量运算符是 post decrementfood--水--。因此更改目前不会反映在文件中。它会先写入文件,然后递减。 所以改成前置自减运算符。

这是您需要在 Undead_Menu class

中更改的内容
 PrintWriter pwInput = new PrintWriter("Undead_save.txt");
 pwInput.println(--food);   //change food-- to --food
 pwInput.println(--water);  //change water-- to --water
 pwInput.flush();           //flushing the PrintWriter
 pwInput.close();           //closing the PrintWriter
 b.combat();