多个while循环中的相同字符串无法解析

埃尔南·拉佐(Hernan Razo)

我要构建的逻辑是,当用户收集到四个会员时,控制台将打印出他们已经达到四个会员,但是如果愿意的话,他们仍然可以继续进行下去。

如果他们决定在四点停止,则应输入“ 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;
            }
        }

    }

}
阿里亚·帕拉万(Aria Pahlavan)

你应该只创建的一个实例ScannerSystem.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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在bash while循环中解析和回显字符串

来自分类Dev

Java While循环中的字符串条件

来自分类Dev

在'Do While'循环中的float上复制字符串

来自分类Dev

Bash(Ubuntu)-while循环中的字符串?

来自分类Dev

如何在循环中将相同的字符串变量附加到字符串

来自分类Dev

无法在for循环中成功调用类字符串

来自分类Dev

如何在 for 循环中将多个旧字符串替换为新字符串?

来自分类Dev

用相同的代码解析多个字符串

来自分类Dev

解析字符串时的Perl while循环

来自分类Dev

Matlab:比较for循环中的字符串

来自分类Dev

在.each循环中串联字符串

来自分类Dev

bash循环中的字符串长度

来自分类Dev

字符串与数组在循环中的比较

来自分类Dev

嵌套的for循环中的字符串匹配

来自分类Dev

嵌套 for 循环中的字符串操作

来自分类Dev

for 循环中的字符串编辑

来自分类Dev

在循环中添加输出/字符串

来自分类Dev

返回在 for 循环中创建的字符串

来自分类Dev

如何在for(或while)循环中用字符填充字符串,C ++

来自分类Dev

While 循环。如何循环字符串

来自分类Dev

尝试验证字符串输入时卡在while循环中

来自分类Dev

输入不匹配的字符串时,scanf在while循环中不起作用

来自分类Dev

将字符串转换为双精度,然后在While循环中将其推入向量

来自分类Dev

如何使用Perl在while循环中从文件切换不同的字符串

来自分类Dev

词法分析器的构造,While循环中的字符串索引超出范围,

来自分类Dev

从while循环中更改f字符串局部变量?

来自分类Dev

在while循环中返回一个字符串

来自分类Dev

空字符串作为while循环中的命令:空/空命令

来自分类Dev

从while循环中的字符串中删除最后一个逗号

Related 相关文章

热门标签

归档