2017-01-26 15 views
0

私が構築しようとしているロジックは、ユーザーが合計4つのアフィリエイトを収集したときに、コンソールが4つに達したと印刷します。複数のwhileループで同じ文字列が解決されない

4人で停止する場合は、「quit」と入力する必要があります。この問題は、if elseステートメントで2番目の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

それは今働いた。助けてくれてありがとう! –

関連する問題