新Java开发人员 - 简单Q.

时间:2014-06-03 13:57:35

标签: java loops input

我刚刚开始编码并且非常适合初学者。我正在尝试制作一个简单要求用户输入“密码”的程序,具体取决于密码是否正确;增加一个柜台。并且,一旦计数器达到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");
    }
}
}

3 个答案:

答案 0 :(得分:2)

你的课程什么时候结束?你描述它的方式,它可以永远持续下去。

如果是这种情况,您只需将其括在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");
        }
    }
}

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

答案 1 :(得分:0)

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

示例代码可能会有所帮助

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();
    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;
        }
    }
}

}

答案 2 :(得分:0)

我会给你一段时间和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);