首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在输入特定字符串后停止java中的while循环?

如何在输入特定字符串后停止java中的while循环?
EN

Stack Overflow用户
提问于 2020-05-07 02:18:32
回答 3查看 99关注 0票数 0

在控制台中输入"End“后,我试图停止一个接受整数的循环,但我似乎找不到一种方法来这样做。

代码语言:javascript
复制
Scanner scan = new Scanner(System.in);
        int bottles = Integer.parseInt(scan.nextLine()) * 750;
        int cnt = 1;
        int platesTotal;
        int potsTotal;
        int nrPlates = 0;
        int nrPots = 0;
        while(true){
            int plates = scan.nextInt();
            platesTotal = plates * 5;
            if(cnt%3==0) {
                int pots = scan.nextInt();
                nrPots = nrPots + pots;
                nrPlates = nrPlates + pots;
                potsTotal = pots * 15;

                if (bottles < potsTotal + platesTotal) {
                    System.out.println("Not enough detergent, " + (potsTotal + platesTotal - bottles) + " ml. more necessary!");
                    break;
                }
                else
                    if(bottles >= potsTotal + platesTotal) {
                        String enough = scan.nextLine();
                        if (enough.equals("End")) {
                            if (bottles >= potsTotal + platesTotal) {
                                System.out.println("Detergent was enough!");
                                System.out.println(nrPlates + " dishes and " + nrPots + "pots were washed.");
                                System.out.printf("Leftover detergent %d ml.", bottles - potsTotal - platesTotal);
                                break;
                            }
                        }
                    }
            }
            cnt++;
        }

输入字符串("End")后,它需要向我显示碟子和罐子的总数,以及它还剩下多少洗涤剂,如果所需的洗涤剂数量超过可用数量,它需要显示还需要多少洗涤剂。

EN

回答 3

Stack Overflow用户

发布于 2020-05-07 02:41:29

尝试使用此String enough = scan.next();而不是scan.nextLine();

票数 0
EN

Stack Overflow用户

发布于 2020-05-07 02:55:52

我建议将消息打印到控制台,以便向用户确认您想要的输入。否则,它只会显示一个空白窗口。我还建议您定义一个布尔变量来转义while循环,同时将足够的值切换为.next而不是.nextLine。这样,您可以将布尔值重新定义为false,以中断while循环,同时还可以获得所需的输出,以便更好地控制程序输出。这是用所有条目1编译的:

代码语言:javascript
复制
package com.climatedev.test;

import java.util.Scanner;

public class Test {


    public static void main(String[] args) {
        System.out.println("How many bottles?");
        Scanner scan = new Scanner(System.in);
        int bottles = scan.nextInt() * 750;
        int cnt = 1;
        int platesTotal;
        int potsTotal;
        int nrPlates = 0;
        int nrPots = 0;
        boolean run = true;
        while(run){
             System.out.println("How many plates?");
             int plates = scan.nextInt();
             platesTotal = plates * 5;
             if(cnt%3==0) {
                 int pots = scan.nextInt();
                  nrPots = nrPots + pots;
                  nrPlates = nrPlates + pots;
                  potsTotal = pots * 15;

              if (bottles < potsTotal + platesTotal) {
                   System.out.println("Not enough detergent, " + (potsTotal + platesTotal - bottles) + " ml. more necessary!");
                   break;
               }
              else
                  if(bottles >= potsTotal + platesTotal) {
                      System.out.println("Would you like to end the program? (Enter End)");
                      String enough = scan.next();
                  if (enough.equals("End")) {
                      if (bottles >= potsTotal + platesTotal) {
                          System.out.println("Detergent was enough!");
                          System.out.println(nrPlates + " dishes and " + nrPots + "pots were washed.");
                          System.out.printf("Leftover detergent %d ml.", bottles - potsTotal - platesTotal);
                          run = false;
                          System.out.println("Goodbye");
                          break;
                        }
                    }
                }
            }
        cnt++;
         }
    }
}

我还稍微更改了你的瓶子,使用nextInt,但这取决于你的使用。如果您认为最终用户可能会键入类似“瓶子的数量是...”之类的内容这样会更好,但是为什么不使用更简单的nextInt调用呢?

票数 0
EN

Stack Overflow用户

发布于 2020-05-07 04:09:54

如果要将scanner对象同时用于字符串和数字,则必须解析数字:

代码语言:javascript
复制
 Scanner scanner = new Scanner(System.in);
 String str = scanner.nextLine();
 int num = Integer.parseInt(scanner.nextLine());

或者您可以有两个扫描器(一个用于字符串,另一个用于数字):

代码语言:javascript
复制
 Scanner strScanner = new Scanner(System.in);
 Scanner numScanner = new Scanner(System.in);
 String str = strScanner.nextLine();
 int num  = numScanner.nextInt();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61642406

复制
相关文章

相似问题

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