我要构建的逻辑是,当用户收集到四个会员时,控制台将打印出他们已经达到四个会员,但是如果愿意的话,他们仍然可以继续进行下去。
如果他们决定在四点停止,则应输入“ quit”。使用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;
}
}
}
}
你应该只创建的一个实例Scanner
从System.in
一次使用同一实例的任何地方,你需要得到来自用户的输入:
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; }
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句