多个 while 循环中的相同字符串未解析

same string in multiple while loops not resolved

我试图构建的逻辑是,当用户总共收集到四个联属会员时,控制台将打印出他们已经达到四个,但如果他们愿意,他们仍然可以继续。

如果他们决定在四点停止,他们应该输入 "quit"。问题出现在带有 if else 语句的第二个 while 循环中。我收到错误 "aff cannot be resolved".

import java.util.Scanner;

public class Seption {

    public static void main(String[] args) {

        System.out.println("Welcome back!");    

        Scanner userName = new Scanner(System.in);
        System.out.println("Username: ");
        String username = userName.next();

        Scanner passWord = new Scanner(System.in);
        System.out.println("Password: ");
        String password = passWord.next();

        int affCount = 0;

        while (affCount <= 4) {

            Scanner newAff = new Scanner(System.in);
            System.out.println("enter new affiliate");
            String aff = newAff.nextLine();

            System.out.println("Alright, " + aff + " is now your new affiliate!");

            affCount = affCount + 1;
            System.out.println("You now have " + affCount + " affiliates");

            if (affCount == 4) {

                System.out.println("Congratulations! You've accumulated 4 affiliates!"
                    + " any new affiliates added to this branch will be extra earnings"
                    + " You can also make a new branch and start over"
                    + " To quit this branch, Type 'quit'");

            continue;
            }
        }

        while (affCount > 4) {

            if (aff.equals("quit") == false) {

                Scanner newAff = new Scanner(System.in);
                System.out.println("enter new affiliate");
                String aff = newAff.nextLine();

                System.out.println("Alright, " + aff + " is now your new affiliate!");

                affCount = affCount + 1;
                System.out.println("You now have " + affCount + " affiliates");

            }

            else if (aff.equals("quit")) {

                System.out.println("This branch is now over");
                break;
            }
        }

    }

}

您应该只从 System.in 创建 Scanner 的实例一次,然后在需要从用户那里获取输入的任何地方使用同一个实例:

public static void main(String[] args){
System.out.println("Welcome back!");    
Scanner scanner = new Scanner(System.in); 
System.out.println("Username: "); String   
username = scanner.next();  
System.out.println("Password: "); 
String password = scanner.next(); 
int affCount = 0; 
String aff = "";
while (affCount <= 4) { 
    System.out.println("enter new affiliate");     
     aff = scanner.nextLine(); 
    System.out.println("Alright, " + aff + " is now your new affiliate!"); 
     affCount = affCount + 1; 
     System.out.println("You now have " + affCount + " affiliates"); 
     if (affCount == 4) 
        { 
         System.out.println("Congratulations! You've accumulated 4 affiliates!" + " any new affiliates added to this branch will be extra earnings" + " You can also make a new branch and start over" + " To quit this branch, Type 'quit'");
        continue; 
        }
    } 

    while (affCount > 4) { 
         if (aff.equals("quit") == false) 
             { 
              System.out.println("enter new affiliate"); 
              aff = scanner.nextLine(); 
              System.out.println("Alright, " + aff + " is now your new affiliate!"); 
              affCount = affCount + 1; 
              System.out.println("You now have " + affCount + " affiliates"); 
               } 
           else if (aff.equals("quit")) 
                   { 
                   System.out.println("This branch is now over");
                   break; }
           } 
   }