否则如果扫描文件时无法正常工作

Else if not working correctly when scanning file

import java.io.FileReader;
import java.util.Scanner;
import java.io.FileNotFoundException;

public class Coursework2 {
    
    public static void main(String[] args)
    throws FileNotFoundException { {

        Scanner reader = new Scanner(new FileReader("seats.txt"));
        Scanner scan = new Scanner (System.in);
        double defaultdiscount = 17.5, discount = 16.97;
        boolean random = true;
        while (random) {
        System.out.println("Please enter your Surname:");
        String name = scan.nextLine();
        System.out.println("Good morning, Manager "  + name + " Would you like to apply a specific discount rate?");
        String decision = scan.nextLine();


        if (decision.equalsIgnoreCase("yes") || decision.equalsIgnoreCase("y")) {
            System.out.println("That's great, Mananger " + name + ".");
            System.out.println("How much discount would you like? (1-100)");
            break;
        } else if (decision.equalsIgnoreCase("no") || decision.equalsIgnoreCase("n")) {
            System.out.println("Alright, " + name + " Here's all the seats without discounts:");
            //break;
        }
        System.out.println("Invalid Input, please try again. Restarting.");
        }
        
        discount = scan.nextDouble();
        defaultdiscount = discount;
        while (reader.hasNext()) {
        String table = reader.next();
        double price = reader.nextDouble();
        int bookings = reader.nextInt();
        
        double newdiscount = (((price/100.00*discount)*bookings));
        double newincome = (price*bookings-(((price/100.00*discount)*bookings)));
        System.out.printf("Seat type: %s, Price: £%.2f, Bookings: %d %n",table, price, bookings );
        System.out.printf("Discount: £%.2f, Income: £%.2f %n", newdiscount, newincome);
        
        String decision;
        do {
            System.out.printf("Seat type: %s, Price: £%.2f, Bookings: %d %n",table, price, bookings );
            //decision = scan.nextLine();
            decision = scan.next();
        }
        while (decision.equalsIgnoreCase("No"));
        System.out.printf("Seat type: %s, Price: £%.2f, Bookings: %d %n",table, price, bookings );
        }
        
        reader.close();
        scan.close();
        }
    
    }
}

我知道代码 运行 是按顺序执行的,但是当涉及到尝试 运行 通过 else if (No) 读取文件时,它不能超过 while reader 或在 else if 内部,否则它不会看到文本文件中的信息,而在外部时,它看不到变量。我只是有点困惑。感觉好像有什么东西放错了地方,我无法理解它。

我试了一下,使用了正确的比较,并在 while 内声明了字符串。但我仍然遇到一些错误。我在想也许将字符串设置为“null”并不是正确的重置方式,因为我之前已经声明它有一个 scan.nextLine?

尝试像这样执行 if 语句

while(random){
    if (decision.equalsIgnoreCase("yes") || decision.equalsIgnoreCase("y")) {
        System.out.println("That's great, Mananger " + name + ".");
        System.out.println("How much discount would you like? (1-100)");
           random = false;
   }
    else if (decision.equalsIgnoreCase("no") || decision.equalsIgnoreCase("n")) {
        System.out.println("Alright, " + name + " Here's all the seats without discounts:");
        random = false;
               }
    else
        System.out.println("Invalid Input, please try again. Restarting.");
}

如果您从不将“随机”布尔值更改为 false,则不会循环它。另外,在这种情况下尽量不要使用 break。 另外,尝试在循环外创建变量“name”和“decision”,这是一种很好的做法。您还应该将“随机”重命名为“isBadAnswer”