当前位置: 首页 > 知识库问答 >
问题:

将用户在java中回答“不”的次数保存到变量

邓威
2023-03-14

下午好,我正在学习java,碰巧接到了一个作业,要回答以下问题:

  1. 向扫描仪输入“是否要关闭此应用程序?”

我想在用户回答“否”时计数,它将计数。

import java.util.Scanner;

public class LatihanWhile6 {
    public static void main(String[] args) {

        String inputClose = "TIDAK";
        int jumlah = 0;
        while (inputClose.equals("TIDAK")) {
            System.out.println("Apakah anda ingin menutup aplikasi ini ?");
            Scanner inputKeyboard = new Scanner(System.in);
            inputClose = inputKeyboard.nextLine().toUpperCase();
        }
        System.out.println("User yang menjawab TIDAK adalah : " + jumlah);
    }
}

共有3个答案

淳于慎之
2023-03-14

您可以尝试下面的代码片段;

public static void main(String[] args) throws IOException {

        int yesNumber = 0;
        String inputKeyboard = "yes";

        while (!inputKeyboard.equalsIgnoreCase("no")) {
            System.out.print("Do you want to continue(yes/no) ? ");
            BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in));

            inputKeyboard = reader.readLine();
            if(inputKeyboard.equalsIgnoreCase( "no"))
                break;
            else if (inputKeyboard.equalsIgnoreCase( "yes"))
                yesNumber++;
            else {
                System.out.println("Please answer with only yes or no !!!");
            }

        }
        System.out.println("Number of yes : " + yesNumber);
    }
孟英叡
2023-03-14

你只需要增加你的计数变量Jumlah每次你去扔循环。当使用时循环时,变量应该开始添加-1,因为循环将为yes情况再运行一次。也就是说,我有一个小提示给你:关闭你的输入流,当你完成使用它的良好风格与调用关闭函数

        String inputClose = "TIDAK";
        int jumlah = -1;
        Scanner inputKeyboard = new Scanner(System.in);
        while (inputClose.equals("TIDAK")) {
            System.out.println("Apakah anda ingin menutup aplikasi ini ?");
            inputClose = inputKeyboard.nextLine().toUpperCase();
            jumlah++;
        }
        System.out.println("User yang menjawab TIDAK adalah : " + jumlah);
        inputKeyboard.close();
    }
彭硕
2023-03-14

根据您的问题,您只需将结果保存在“用户回答“否”的次数”上即可。我建议您使用while循环和变量来存储

...
     public static void main(String[] args) {
        String inputClose = "TIDAK";
        int jumlah = 0;
        while (inputClose.equals("TIDAK")) {
            System.out.println("Apakah anda ingin menutup aplikasi ini ?");
            Scanner inputKeyboard = new Scanner(System.in);
            inputClose = inputKeyboard.nextLine().toUpperCase();
            
            // add 'jumlah' value if input is still "TIDAK" after the scanner get the input value
            if (inputClose.equals("TIDAK")) jumlah++
        }

        System.out.println("User yang menjawab TIDAK adalah : " + jumlah);
    } 
...

但是我建议你使用更友好的输入检测,如果你是通过键盘输入的话。使用equalsIgnoreCase可以采用任何其他字符串“TIDAK”格式。这是一个例子

...
     public static void main(String[] args) {
        String inputClose = "TIDAK";
        int jumlah = 0;
        while (inputClose.equalsIgnoreCase("TIDAK")) {
            System.out.println("Apakah anda ingin menutup aplikasi ini ?");
            Scanner inputKeyboard = new Scanner(System.in);
            // no need to reformat to uppercase
            inputClose = inputKeyboard.nextLine();
            
            // add 'jumlah' value if input is still "TIDAK" after the scanner get the input value
            if (inputClose.equalsIgnoreCase("TIDAK")) jumlah++
        }

        System.out.println("User yang menjawab TIDAK adalah : " + jumlah);
    } 
...
 类似资料:
  • 问题内容: 如何在Java中以两位数字格式存储整数?我可以设置 并打印为?另外,我要说,不仅打印,还应该将其值打印为。 问题答案: 我认为这是您要寻找的: 或者,更简单地说: 一个int仅存储一个数量,而01和1表示相同的数量,因此它们以相同的方式存储。 DecimalFormat构建一个以特定格式表示数量的字符串。

  • 我对node js和expressjs框架很陌生。我正在开发一个web应用程序,我被卡住了。我已经设置了所有的数据库要求,所有的工作都很好,现在我想把一些数据保存到数据库中,我把数据存储在一个变量中,但不幸的是,存储在变量(MyID)中的数据没有保存。这是我的密码 var=MyID app.post(“/AddContact”,function(req,res){ }

  • 问题内容: 我使用getJSON从我的网站请求JSON。效果很好,但是我需要将输出保存到另一个变量中,如下所示: 我需要将结果保存到其中,但似乎此语法不正确。有任何想法吗? 问题答案: 仅在响应后才能调用,无法获得价值。

  • 应用数据。当应用程序运行时,将应用程序数据保存在类中的静态变量中是不好的吗?目前,我将数据存储在类中的一个实例变量中。然后,需要数据的类可以从.获得数据 等等,在静态字段中存储(例如对或的引用)是不是很糟糕?这可以用于需要或资源的类中。目前,我正在将传递给需要它们作为参数的方法。

  • 收藏一个回答 取消收藏一个回答 回答收藏列表 收藏一个回答 POST /api/v2/user/question-answer/collections/:answer 响应 Header 201 Created { "message": [ "操作成功" ] } 取消收藏一个回答 DELETE /api/v2/user/question-answer/collections/:

  • 喜欢 喜欢一个回答 取消喜欢一个回答 一个回答的喜欢列表 喜欢一个回答 POST /api/v2/question-answers/:answer/likes 响应 Header 201 Created { "message": [ "操作成功" ] } 取消喜欢一个回答 DELETE /api/v2/question-answers/:answer/likes 响应 Hea