【问题标题】:New Java developer - Simple Q新的 Java 开发人员 - Simple Q
【发布时间】:2014-06-03 13:57:34
【问题描述】:

我最近才开始编码,并且是初学者。我正在尝试制作一个程序,该程序仅要求用户输入“密码”,并取决于密码是否正确;增加一个计数器。并且,一旦计数器达到 10,打印出一条消息。 基本上我想做的就像一张“剪贴卡”,就像你可以在咖啡店买到的那种(每 10 杯咖啡免费)。

所以,这就是我现在所拥有的。我只需要知道如何在输入密码后让程序继续运行,并跟踪输入。

哦,还有...如果不清楚,请说出来,我会尽力澄清。

这是我必须要做的……

import java.util.Scanner;

public class Coffee {

public static void main(String [] args){


    int count = 0;
    String pass = "hey";

    System.out.println("Enter password: ");

    Scanner key = new Scanner(System.in);

    String moves = key.nextLine();

    if(moves.compareTo(pass) == 0){

        count++;
        System.out.println("You're one step closer to a free coffe! You have so far bought " + count + " coffe(s)");
    }
    if(count % 10 == 0 && count != 0){
        System.out.println("You've got a free coffe!");
    }
    if(moves.compareTo(pass) != 0){
    System.out.println("Wrong password! Try again.\n");
    }
}
}

【问题讨论】:

  • 你熟悉循环吗?更具体的 while 循环?
  • 请将您的标题与您遇到的编程问题联系起来。例如:“如何在输入密码后使程序继续运行,同时跟踪输入”(较短的标题会更吸引人,但至少要说明您遇到的实际问题或问题或想要回答的问题)跨度>

标签: java loops input


【解决方案1】:

您的计划什么时候结束?按照你的描述,它可以永远持续下去。

如果是这样,您只需将其包含在 while 循环中:

public static void main(String [] args) {
    Scanner key;
    String moves;
    int count = 0;
    String pass = "hey";
    while(true) {
        System.out.println("Enter password: ");
        key = new Scanner(System.in);
        moves = key.nextLine();
        if(moves.compareTo(pass) == 0){
            count++;
            System.out.println("You're one step closer to a free coffe! You have so far bought " + count + " coffe(s)");
        }
        if(count % 10 == 0 && count != 0){
            System.out.println("You've got a free coffe!");
        }
        if(moves.compareTo(pass) != 0){
            System.out.println("Wrong password! Try again.\n");
        }
    }
}

但你真的应该有一个打破条件。

【讨论】:

  • 嗯,我希望我的程序能像剪贴卡一样工作,每购买第 n: 杯咖啡,您就可以免费获得一杯。所以,我实际上需要在每次运行时将其恢复到最后一个位置(这意味着,如果一个人购买了 7 杯咖啡,程序需要知道在下次运行时从 7 点开始)。关于断裂条件,是的,我需要一个。只是一个中断程序并让它知道下次运行时从哪里开始的程序。谢谢!
【解决方案2】:

你需要循环(while、do-while 或 for 循环)来做这种重复的事情。

示例代码可能会有所帮助

导入 java.util.Scanner;

公开课咖啡{

public static void main(String[] args) {

    int count = 0;
    String pass = "hey";

    System.out.println("Enter password: ");

    Scanner key = new Scanner(System.in);

    String moves = key.nextLine();
    boolean flag = true;
    while (flag) {

        if (moves.compareTo(pass) == 0) {

            count++;
            System.out
                    .println("You're one step closer to a free coffe! You have so far bought "
                            + count + " coffe(s)");
        }
        if (count == 10 && count != 0) {
            System.out.println("You've got a free coffe!");
            count=0;
        }
        if (moves.compareTo(pass) != 0) {
            System.out.println("Wrong password! Try again.\n");
        }
        System.out.println("Do you want to continue ..(y/n)");
        String choice = key.nextLine();
        if (choice.equals("n")) {
            flag = false;
        } else {
            flag = true;
        }
    }
}

}

【讨论】:

    【解决方案3】:

    我会给你一个 while 和 do-while 循环供你选择

    While循环

    Scanner key;
    String moves;
    int count = 0;
    String pass = "hey";
    boolean condition=true;
    while(condition) {
        System.out.println("Enter password: ");
        key = new Scanner(System.in);
        moves = key.nextLine();
        if(moves.compareTo(pass) == 0){
            count++;
            System.out.println("You're one step closer to a free coffe! You have so far bought " + count + " coffe(s)");
    
            if(count % 10 == 0 && count != 0){
            System.out.println("You've got a free coffe!");
            condition=false;
            }
        }
        else if(moves.compareTo(pass) != 0){
            System.out.println("Wrong password! Try again.\n");
        }
    }
    

    Do-While 循环

    Scanner key;
    String moves;
    int count = 0;
    String pass = "hey";
    boolean condition=true;
    do{
        System.out.println("Enter password: ");
        key = new Scanner(System.in);
        moves = key.nextLine();
        if(moves.compareTo(pass) == 0){
            count++;
            System.out.println("You're one step closer to a free coffe! You have so far bought " + count + " coffe(s)");
    
            if(count % 10 == 0 && count != 0){
            System.out.println("You've got a free coffe!");
            condition=false;
            }
        }
        else if(moves.compareTo(pass) != 0){
            System.out.println("Wrong password! Try again.\n");
        }
    }while(condition);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-07
      • 2010-12-25
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 2013-10-28
      • 2010-10-05
      相关资源
      最近更新 更多