提问人:Hernan Razo 提问时间:1/26/2017 最后编辑:Hernan Razo 更新时间:2/12/2018 访问量:75
多个 while 循环中的同一字符串未解析
same string in multiple while loops not resolved
问:
我试图构建的逻辑是,当用户总共收集了四个会员时,控制台将打印他们已经达到四个,但如果他们愿意,他们仍然可以继续。
如果他们决定在四点停止,他们应该输入“退出”。问题在第二个 while 循环中使用 if else 语句出现。我收到错误“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;
}
}
}
}
答:
0赞
Aria Pahlavan
1/26/2017
#1
您应该只创建一次 from 的实例,并在需要从用户那里获取输入的任何地方使用相同的实例: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; }
}
}
评论
0赞
Hernan Razo
1/26/2017
谢谢你的帮助。我输入了更正,但现在它给了我错误“局部变量 aff 可能尚未初始化”。这只显示在最后一个 while 循环中的 if else 语句中。
1赞
Aria Pahlavan
1/26/2017
是的,显然有人建议对我的答案进行编辑并更改代码。当您实例化时,请初始化它:...答案现在应该解决了!!aff
String aff = ""
评论
aff