首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java扫描器忽略答案

Java扫描器忽略答案
EN

Stack Overflow用户
提问于 2015-08-30 21:51:00
回答 2查看 53关注 0票数 0

嗨,这是我的第一篇帖子,我有个问题:

我有一个程序,在某个时候,询问用户他是否想分享星星,如果他想分享,这个程序会回到收集它们,过了一段时间,他是否想再次分享它们。

问题是,当用户返回到这个点时,程序会忽略您给出的任何答案,而转到"else块“。

看起来是这样的:

代码语言:javascript
复制
"Do you want to share your stars?

yes

Please answer with yes or no"

代码:

代码语言:javascript
复制
import java.util.Random;
import java.util.Scanner;

public class App {
static Scanner skan = new Scanner(System.in);
static int starCount = 0;
static Random random = new Random();

public static void main(String[] args) {
    starReaching();

}

static void starReaching() {
    boolean starCollected = false;
    int i = 0;
    int j = random.nextInt(101);

    while (i < j) {
        i++;
        System.out.println("Stars are out of reach");
    }
    if (i > j || i == j) {
        starCollected = true;
    }
    if (starCollected == true) {
        starCollector();
    }
}

static void starCollector() {
    System.out.println("You caught a star !");
    starCount++;
    if (starCount == 10) {
        System.out.println("You have 10 stars ! :)");
        System.out.println("Do you want to share your stars?");
        String line = skan.nextLine();
        if (line.equals("yes")) {
            skan.reset();
            starGiver();
        } else if (line.equals("no")) {
            wishMaker();
        } else {
            System.out.println("Please answer with yes or no");
            System.exit(0); 
        }
    } else {
        starReaching();
    }
}

static void starGiver() {
    System.out.println("How many do you want to share?");
    int starsToShare = skan.nextInt();
    if (starsToShare < 10 || starsToShare == 10 && starsToShare > 0) {
        starCount = starCount - starsToShare;
        System.out.println("Stars shared !");
        System.out.println("You now have " + starCount + " stars");
        System.out.println("Go collect them again!");
        starReaching();
    } else if (starsToShare > 10) {
        System.out.println("You don't have enough stars to share that much!");
        starGiver();
    } else {
        System.out.println("That's not a valid option");
        starGiver();
    }
}

static void wishMaker() {

}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-30 21:54:16

每次调用skan.nextLine()时,都会读取一条新行并提前扫描程序的指针,这就是您的代码失败的原因:您调用它太频繁了。

而不是

代码语言:javascript
复制
if (skan.nextLine().equals("yes")) {
    skan.reset();
    starGiver();
} else if (skan.nextLine().equals("no")) {
    wishMaker();
} else {

做:

代码语言:javascript
复制
String line = scan.nextLine(); // read from Scanner **once** only
if (line.equals("yes")) {
    skan.reset();
    starGiver();
} else if (line.equals("no")) {
    wishMaker();
} else {
票数 1
EN

Stack Overflow用户

发布于 2015-08-30 22:08:57

好吧,我找到了那个小混蛋,我用的是skan.nextInt,之后不用skan.nextLine,谢谢你的快速帮助,非常爱

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32301502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档