2017-01-26 51 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

它現在工作。謝謝您的幫助! –