简体   繁体   English

当用户输入“结束”时如何结束这个程序?

[英]How to end this program when user inputs “end”?

So basically I am trying to figure out how to stop this program using Integer.valueOf(String s) or Integer.parseInt(String s) by typing "end" for the user input所以基本上我试图通过为用户输入键入“end”来弄清楚如何使用Integer.valueOf(String s)Integer.parseInt(String s)来停止这个程序

import java.util.Scanner;

public class monkey1{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = 0;
        int lol = 1;
        do { 
            System.out.print("Enter a number: ");
            lol = s.nextInt();
            sum += lol;
            if (lol > 0) {
                System.out.println("Sum is now: " + sum);
            }
        } while (lol>0);
    }
}

First - you need to change lol=sc.nextInt() to String tmp = s.nextLine()首先-您需要将lol=sc.nextInt()更改为String tmp = s.nextLine()

Second - do第二——做

try {
   if (tmp.equals("END")) {
       break;  // This will exit do-while loop
   }
   lol = Integer.parseInt(tmp);
} catch (NumberFormatException e) {
  e.printStackTrace();
  // Here you can also exit loop by adding break. Or just ask user to enter new text.
}

you should learn how to use try/catch.你应该学习如何使用 try/catch。 Here we go:这里我们 go:

String text = "";
    int lol = 1;
    do {
        System.out.print("Enter a number: ");
        text = s.nextLine();
        try {
            lol = Integer.valueOf(text);
        }catch (Exception e){
            System.out.println("Exit");
            System.exit(0);
        }

Here is your code这是你的代码

package Phone;

import java.util.Scanner;

public class monkey1{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = 0;
        int hi = 1;
        do { 
            System.out.print("Enter a number: ");
            String lol = s.nextLine();
            if(lol.equals("end")) {
                break;
        }

        hi = Integer.parseInt(lol);

        sum += hi;
        if (hi > 0) {
            System.out.println("Sum is now: " + sum);
        }
    } while (hi > 0);
}

What I did here is I changed lol to hi for adding and took String input so that you can input end...我在这里所做的是我将 lol 更改为 hi 以添加并接受 String 输入,以便您可以输入 end ...

I would suggest checking if s.equals("end") before converting to int so something like this:我建议在转换为 int 之前检查 if s.equals("end") ,如下所示:

do { 
        System.out.print("Enter a number: ");
        String userValue = s.nextLine();
        if(userValue.equals("end")
            break;
        lol = Integer.parseInt(userValue);
        sum += lol;
        if (lol > 0){
            System.out.println("Sum is now: " + sum);
        }
    }while (lol>0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在循环中捕获重复出现的用户输入,然后在程序结束时全部显示它们? - How can I capture recurring user inputs in a loop and then display them all at the end of the program? 如何设置ScheduledExecutorService在用户退出程序时结束? - How to set ScheduledExecutorService to end when user quits program? 如何在Java中对整数输入求和,并在程序结束时在数组中显示结果? - How to sum integer inputs in Java, and display the results in an array at the end of the program? 如何结束我的计划 - How to End my Program 如何结束 java 程序 - How to end java program 如何检查输入是否为 integer? 当用户输入 7 程序结束时,我对 output 有问题 - How do I check if the input is an integer? I have issue with the output when the user enter 7 the program end 当Java程序遍历数千个数据时,如何显示对最终用户可见的进度条? - How to display progress bar visible to end user when Java program is looping through thousands of data? 最终用户如何以及何时使用序列化对象 - How and When the Serialized Objects are used by the end user 如何结束程序,并阻止用户被提示? - How do I end a program, and stop the user from being prompted? 如何为最终用户方便地启动Java GUI程序 - How to start Java GUI program conveniently for the end user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM