当输入为 0 时,如何结束循环?

huangapple go评论127阅读模式
英文:

How do I end loop when 0 is entered?

问题

import javax.swing.JOptionPane;

public class MaxMinAvg {
    public static void main(String args[]) {
        String inString = " ";
        int count = 0;

        inString = JOptionPane.showInputDialog("Enter a String");

        while (inString.length() != 0) {
            count++;
            System.out.println("record " + count + " is " + inString);

            inString = JOptionPane.showInputDialog("Enter a String");
        }

        System.out.println("All done");
    }
}
英文:

I'm writing a program where the user will keep entering numbers until 0 is entered using loops. It will then display the largest number entered, the smallest number entered, and the average of all the numbers. This is what I have to start with:

import javax.swing.JOptionPane;													   

public class MaxMinAvg
{
   public static void main(String args[])
   {
	   	String inString = " ";
	   	int count = 0;

       																			
		inString = JOptionPane.showInputDialog("Enter a String");

        																		//perform until the end of file
		while(inString.length() != 0)
        {
			count++;
            System.out.println("record " + count + " is " + inString);

                                                                             // second read
            inString = JOptionPane.showInputDialog("Enter a String");

        } 																	// end while loop

	    System.out.println("All done");

   }
}

答案1

得分: 1

尝试将输入字符串与退出条件进行比较
```java
import javax.swing.JOptionPane;

public class MaxMinAvg
{
   public static void main(String args[])
   {
	   	String inString = " ";
	   	int count = 0;

		inString = JOptionPane.showInputDialog("请输入一个字符串");

		while(inString.length() != 0 && !inString.equals("0"))
        {
			count++;
            System.out.println("记录 " + count + " 为 " + inString);

            inString = JOptionPane.showInputDialog("请输入一个字符串");
        }

	    System.out.println("全部完成");
   }
}
英文:

try to compare the input string with your exit condition:

import javax.swing.JOptionPane;													   

public class MaxMinAvg
{
   public static void main(String args[])
   {
	   	String inString = " ";
	   	int count = 0;

       																			
		inString = JOptionPane.showInputDialog("Enter a String");

        																		//perform until the end of file
		while(inString.length() != 0 && !inString.equal("0"))
        {
			count++;
            System.out.println("record " + count + " is " + inString);

                                                                             // second read
            inString = JOptionPane.showInputDialog("Enter a String");

        } 																	// end while loop

	    System.out.println("All done");

   }
}

答案2

得分: 0

首先,您不需要两次使用 JOptionPane.showInputDialog("Enter a String");。将其放在 while 循环内部调用一次即可。

其次,定义一个无限的 while 循环 - 使用 while(true),因为您不知道用户会提供多少次输入。

第三,要在输入为 0 时停止循环,将逻辑放在循环内部。

  • 使用 trim() 去除输入中的空格,因为用户可能会在数字前后插入空格。
  • 将输入转换为 int 类型。
  • 使用 == 检查用户给定的输入是否等于 0
  • 使用 break 停止循环。
    public static void main(String args[]) {

        String inString = "";

        int count = 0;

        while(true) {

            inString = JOptionPane.showInputDialog("Enter a String");

            count++;

            System.out.println("record " + count + " is " + inString);

            if (Integer.parseInt(inString.trim()) == 0) {
                break;
            }
        }
        
        System.out.println("All done");
    }
英文:

First, you don't need JOptionPane.showInputDialog("Enter a String"); twice. Call it once inside the while loop.

Second, define an infinite while loop - while(true) since you don't know how many times user will give input.

Third, as to stop the loop based on input 0, put the logic inside the loop.

  • trim() the input, user could insert a blank space before or after the unmber
  • convert the input into int
  • check equality == of user given input with 0
  • break the loop
    public static void main(String args[]) {

        String inString = "";

        int count = 0;

        while(true) {

            inString = JOptionPane.showInputDialog("Enter a String");

            count++;

            System.out.println("record " + count + " is " + inString);

            if (Integer.parseInt(inString.trim()) == 0) {
                break;
            }
        }
        
        System.out.println("All done");
    }

huangapple
  • 本文由 发表于 2020年4月5日 11:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61037613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定