2017-01-26 33 views
0

我试图建立的逻辑是,当用户收集四个总分会,控制台将打印他们已经达到四,但他们仍然可以继续,如果他们选择。相同的字符串在多个while循环未解决

如果他们决定停在四位,他们应该输入“退出”。这个问题随着if else语句在第二个while循环中上升。我收到错误“aff无法解决”。

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; 
      } 
     } 

    } 

} 
+5

这是超出范围。你需要在循环外声明'aff'。 – shmosel

回答

0

你应该只从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; } 
      } 
    } 
+0

谢谢你的帮助。我输入了更正,但现在它给了我错误“局部变量aff可能未被初始化”。这只显示最后while循环中的if else语句。 –

+1

是的,显然有人建议编辑我的答案并更改了代码。当你实例化'aff'时,初始化它:'String aff =“”'...答案应该现在修复! –

+0

它现在工作。谢谢您的帮助! –