私が構築しようとしているロジックは、ユーザーが合計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;
}
}
}
}
それは適用範囲外です。ループの外側に 'aff 'を宣言する必要があります。 – shmosel